List of usage examples for org.jdom2 Attribute Attribute
public Attribute(final String name, final String value)
Attribute
with the specified (local) name and value, and does not place the attribute in a Namespace
. From source file:parserXML.Parser.java
License:Open Source License
public static void main(String[] args) throws IOException { String fields = null;//from w ww. j a v a 2 s. c om String delimiter = "[,]"; String secondDelimiter = "[:]"; String thirdDelimiter = "\\s+"; // delimiter to parse the line currently read. String[] tokenFields; // contains the fields read from the field.txt file. String[] tokenEntry; // contains the value aaociated with the current field. BufferedReader br = null; String sCurrentLine; // contains the line currently read from the input file. long unixSeconds; // the second for the timestamp conversion. long inputReadLine = 0; // contains the number of read lines. long outputWroteChildren = 0; // contains the number of children wrote. long unknowActivityNumber = 0; // contains the number of unknow activities. try { // Get the fields's name in the file given as parameter. br = new BufferedReader(new FileReader("data/fields.txt")); fields = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } // Split the line read and convert it in a String table containing the fields in // every cell. tokenFields = fields.split(delimiter); System.out.println("XML file is generating..."); try { br = new BufferedReader(new FileReader("data/data.txt")); while ((sCurrentLine = br.readLine()) != null) { inputReadLine++; // first slitting with the commas. tokenEntry = sCurrentLine.split(delimiter); Element data = new Element("entry"); racine.addContent(data); Attribute ligne = new Attribute(tokenFields[0], "e" + tokenEntry[0]); data.setAttribute(ligne); int i = 1; // The field's number (ex: id is the first field, // timestamp is the second field, etc). boolean isNotDescription = true; // Equals true if the field which is reading is not the fifth. // while the fifth field (i.e. activity) is not reached. while (i < tokenFields.length && isNotDescription) { Element element = new Element(tokenFields[i]); data.addContent(element); // The following cases handle the whole value that can be taken by "activity". // The default case handle the unrecognized value of activity. switch (tokenEntry[i]) { case "Outside": isNotDescription = false; Attribute activityOutside = new Attribute("name", "Outside"); element.setAttribute(activityOutside); Element subElemOutside = new Element("description"); // Add a new node element to the current node. element.addContent(subElemOutside); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElemOutside.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Anomaly": isNotDescription = false; Attribute activityAnomaly = new Attribute("name", "Anomaly"); // Set the attribute inside the markups element.setAttribute(activityAnomaly); // Second splitting with the white spaces String[] descriptionFieldAnomaly = tokenEntry[i + 1].split(thirdDelimiter); if (descriptionFieldAnomaly[0].equals("NA")) { Element subElem = new Element("description"); element.addContent(subElem); } else { for (String tokenFromDescriptionFieldAnomaly : descriptionFieldAnomaly) { Element subElem = new Element("description"); element.addContent(subElem); subElem.setText(tokenFromDescriptionFieldAnomaly); } } outputWroteChildren++; break; case "Medical_atention": isNotDescription = false; Attribute activityMedicalAtention = new Attribute("name", "Medical_atention"); element.setAttribute(activityMedicalAtention); Element subElemMedicalAtention = new Element("description"); // Add a new node element to the current node. element.addContent(subElemMedicalAtention); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElemMedicalAtention.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Social_Interactions": isNotDescription = false; Attribute activitySocialInteractions = new Attribute("name", "Social_Interactions"); element.setAttribute(activitySocialInteractions); Element subElemSocialInteractions = new Element("description"); // Add a new node element to the current node. element.addContent(subElemSocialInteractions); // Set the text between the markups. subElemSocialInteractions.setText(tokenEntry[i + 1]); outputWroteChildren++; break; case "Visits": isNotDescription = false; Attribute activityVisits = new Attribute("name", "Visits"); element.setAttribute(activityVisits); Element subElemVisits = new Element("description"); // Add a new node element to the current node. element.addContent(subElemVisits); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElemVisits.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Other": isNotDescription = false; Attribute activityOther = new Attribute("name", "Other"); element.setAttribute(activityOther); Element subElemOther = new Element("description"); // Add a new node element to the current node. element.addContent(subElemOther); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElemOther.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Inside": isNotDescription = false; Attribute activityInside = new Attribute("name", "Inside"); element.setAttribute(activityInside); Element subElemInside = new Element("description"); // Add a new node element to the current node. element.addContent(subElemInside); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElemInside.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Check_Over": isNotDescription = false; Attribute activityCheckOver = new Attribute("name", "Check_Over"); element.setAttribute(activityCheckOver); Element subElemCheckOver = new Element("description"); // Add a new node element to the current node. element.addContent(subElemCheckOver); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElemCheckOver.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Recreation": isNotDescription = false; Attribute activityRecreation = new Attribute("name", "Recreation"); element.setAttribute(activityRecreation); Element subElemRecreation = new Element("description"); // Add a new node element to the current node. element.addContent(subElemRecreation); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElemRecreation.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Mood": isNotDescription = false; Attribute activityMood = new Attribute("name", "Mood"); element.setAttribute(activityMood); Element subElem1 = new Element("description"); // Add a new node element to the current node. element.addContent(subElem1); // Set the text between the markups. if (!tokenEntry[i + 1].equals("NA")) { subElem1.setText(tokenEntry[i + 1]); } outputWroteChildren++; break; case "Toilet": isNotDescription = false; Attribute activity2 = new Attribute("name", "Toilet"); // Set the attribute inside the markups element.setAttribute(activity2); String[] descriptionField2 = tokenEntry[i + 1].split(thirdDelimiter); if (descriptionField2[0].equals("NA")) { Element subElem = new Element("description"); element.addContent(subElem); } else if (descriptionField2[0].contains(":")) { for (int j = 0; j < descriptionField2.length; j++) { String[] descEntry = descriptionField2[j].split(secondDelimiter); Element subElem = new Element(descEntry[0]); // Add a new node element to the current node. element.addContent(subElem); // Set the text between the markups. subElem.setText(descEntry[1]); } } else { for (String tokenFromDescriptionField2 : descriptionField2) { Element subElem = new Element("description"); element.addContent(subElem); subElem.setText(tokenFromDescriptionField2); } } outputWroteChildren++; break; case "Vitals": isNotDescription = false; Attribute activity = new Attribute("activity", "Vitals"); element.setAttribute(activity); String[] descriptionField = tokenEntry[i + 1].split(thirdDelimiter); for (int j = 0; j < descriptionField.length; j++) { String[] descEntry = descriptionField[j].split(secondDelimiter); Element subElem = new Element(descEntry[0]); // Add a new node element to the current node. element.addContent(subElem); if (!descEntry[1].equals("NA")) { // Set the text between the markups. subElem.setText(descEntry[1]); } } outputWroteChildren++; break; case "Hygiene": isNotDescription = false; Attribute activity5 = new Attribute("name", "Hygiene"); // Set the attribute inside the markups element.setAttribute(activity5); String[] descriptionField5 = tokenEntry[i + 1].split(thirdDelimiter); if (descriptionField5[0].contains(":")) { for (int j = 0; j < descriptionField5.length; j++) { String[] descEntry = descriptionField5[j].split(secondDelimiter); Element subElem = new Element(descEntry[0]); // Add a new node element to the current node. element.addContent(subElem); // Set the text between the markups. subElem.setText(descEntry[1]); } } else if (!tokenEntry[i + 1].equals("NA")) { for (String tokenFromDescriptionField5 : descriptionField5) { Element subElem = new Element("hygiene"); element.addContent(subElem); subElem.setText(tokenFromDescriptionField5); } } else { Element subElem = new Element("hygiene"); element.addContent(subElem); } outputWroteChildren++; break; case "Medication": isNotDescription = false; Attribute activity3 = new Attribute("name", "Medication"); element.setAttribute(activity3); String[] descriptionField3 = tokenEntry[i + 1].split(thirdDelimiter); if (descriptionField3[0].equals("NA")) { Element subElem = new Element("medication"); element.addContent(subElem); } else { for (int j = 0; j < descriptionField3.length; j++) { String[] descEntry = descriptionField3[j].split(secondDelimiter); Attribute medic = new Attribute("name", descEntry[0]); Element subElem = new Element("medication"); element.addContent(subElem); subElem.setAttribute(medic); if (!descEntry[1].equals("NA")) { subElem.setText(descEntry[1]); } } } outputWroteChildren++; break; case "Feeding": isNotDescription = false; Attribute activity4 = new Attribute("name", "Feeding"); element.setAttribute(activity4); String[] descriptionField4 = tokenEntry[i + 1].split(thirdDelimiter); for (int j = 0; j < descriptionField4.length; j++) { String[] descEntry = descriptionField4[j].split(secondDelimiter); if (!descEntry[1].equals("NA")) { Element subElem = new Element(descEntry[0]); element.addContent(subElem); subElem.setText(descEntry[1]); } // if the name of the meal is unknow else if (descEntry[0].equals("?")) { Element subElem = new Element("feeding"); element.addContent(subElem); subElem.setText(null); } else { Element subElem = new Element(descEntry[0]); element.addContent(subElem); subElem.setText(null); } } outputWroteChildren++; break; default: // if the current field is "caregiver" it is and equals to "NA" if (tokenFields[i].equals("caregiver")) { //System.out.println("caregiver: "+tokenEntry[i]); if (tokenEntry[i].equals("NA")) { element.setText(null); } else { element.setText(tokenEntry[i]); } } // if the field which is reading is timestamp -> conversion to readable date. else if (tokenFields[i].equals("timestamp")) { unixSeconds = Long.parseLong(tokenEntry[i]); Date date = new Date(unixSeconds * 1000L); // *1000 is to convert seconds to milliseconds SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formatedDate = sdf.format(date); element.setText(formatedDate); } // If neither category has been recognized. // Field number % contains the activity. else if (i == 5) { System.out.println("Error, unknow activity: " + sCurrentLine); } // otherwise the value of the field is added. else { element.setText(tokenEntry[i]); } break; } // enf of switch i++; } } System.out.println(inputReadLine + " lines read."); System.out.println(outputWroteChildren + " children wrote."); System.out.println(unknowActivityNumber + " unknow ativity(ies)."); System.out.println("dataXML.xml has been generated. \n"); } catch (java.io.FileNotFoundException e) { e.printStackTrace(); } //affiche(); enregistre("dataXML/dataXML.xml"); }
From source file:password.pwm.config.stored.StoredConfigurationImpl.java
License:Open Source License
@Override public void writeConfigProperty(final ConfigurationProperty propertyName, final String value) { domModifyLock.writeLock().lock();/*from w w w . j av a 2 s . c o m*/ try { final XPathExpression xp = XPathBuilder.xpathForConfigProperty(propertyName); final List<Element> propertyElements = xp.evaluate(document); for (final Element propertyElement : propertyElements) { propertyElement.detach(); } final Element propertyElement = new Element(XML_ELEMENT_PROPERTY); propertyElement.setAttribute(new Attribute(XML_ATTRIBUTE_KEY, propertyName.getKey())); propertyElement.setContent(new Text(value)); if (null == XPathBuilder.xpathForConfigProperties().evaluateFirst(document)) { final Element configProperties = new Element(XML_ELEMENT_PROPERTIES); configProperties.setAttribute(new Attribute(XML_ATTRIBUTE_TYPE, XML_ATTRIBUTE_VALUE_CONFIG)); document.getRootElement().addContent(configProperties); } final XPathExpression xp2 = XPathBuilder.xpathForConfigProperties(); final Element propertiesElement = (Element) xp2.evaluateFirst(document); propertyElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, JavaHelper.toIsoDate(Instant.now())); propertiesElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, JavaHelper.toIsoDate(Instant.now())); propertiesElement.addContent(propertyElement); } finally { domModifyLock.writeLock().unlock(); } }
From source file:password.pwm.config.StoredConfiguration.java
License:Open Source License
public void writeConfigProperty(final ConfigProperty propertyName, final String value) { domModifyLock.writeLock().lock();/* w w w. j a v a 2s .c o m*/ try { final XPathExpression xp = XPathBuilder.xpathForConfigProperty(propertyName); final List<Element> propertyElements = xp.evaluate(document); for (final Element propertyElement : propertyElements) { propertyElement.detach(); } final Element propertyElement = new Element(XML_ELEMENT_PROPERTY); propertyElement.setAttribute(new Attribute(XML_ATTRIBUTE_KEY, propertyName.getKey())); propertyElement.setContent(new Text(value)); if (null == XPathBuilder.xpathForConfigProperties().evaluateFirst(document)) { Element configProperties = new Element(XML_ELEMENT_PROPERTIES); configProperties.setAttribute(new Attribute(XML_ATTRIBUTE_TYPE, XML_ATTRIBUTE_VALUE_CONFIG)); document.getRootElement().addContent(configProperties); } final XPathExpression xp2 = XPathBuilder.xpathForConfigProperties(); final Element propertiesElement = (Element) xp2.evaluateFirst(document); propertyElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date())); propertiesElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date())); propertiesElement.addContent(propertyElement); } finally { domModifyLock.writeLock().unlock(); } }
From source file:pl.edu.pwr.iiar.zak.thermalKit.util.ReadDesign.java
License:Open Source License
private static Element addElement(int col, int row, String name) { Element element = new Element(name); element.setAttribute(new Attribute("x", Integer.toString(col))); element.setAttribute(new Attribute("y", Integer.toString(row))); element.setAttribute(new Attribute("width", "1")); element.setAttribute(new Attribute("height", "1")); return element; }
From source file:pl.edu.pwr.iiar.zak.thermalKit.util.ReadDesign.java
License:Open Source License
public void showDesign(String filename) throws JDOMException, IOException { try {//from w w w . j a v a2s . c om System.out.println("Converting NCD file to XDL..."); FileConverter.convertNCD2XDL(filename); } catch (NullPointerException e) { System.out.println("Please indicate correct ncd file!"); System.exit(0); } Design design = new Design(); design.loadXDLFile(filename.substring(0, filename.length() - 3) + "xdl"); Device device = design.getDevice(); Document writer = new Document(); Element rootElement = new Element("device"); writer.addContent(rootElement); rootElement.setAttribute(new Attribute("version", "0.2")); rootElement.setAttribute(new Attribute("name", device.getPartName())); Element size = new Element("size"); rootElement.addContent(size); size.setAttribute(new Attribute("cols", String.format("%d", device.getColumns()))); size.setAttribute(new Attribute("rows", String.format("%d", device.getRows()))); Element clbsize = new Element("clbsize"); rootElement.addContent(clbsize); clbsize.setAttribute(new Attribute("width", "0.00025")); clbsize.setAttribute(new Attribute("height", "0.0002")); int old_z = -1; int k = 0; String progress_bar = "|/-\\"; for (int row = 0; row < device.getColumns(); row++) { if (print) { System.out.format("%d\t", row + 1); } for (int col = 0; col < device.getRows(); col++) { System.out.println(String.format("Col: %d, Row: %d", col, row)); System.out.println(design.getDevice().getTile(col, row).getName()); if (design.getDevice().getTile(col, row).getName().contains("CLB")) { try { boolean used = false; for (PrimitiveSite ps : design.getDevice().getTile(col, row).getPrimitiveSites()) { if (design.isPrimitiveSiteUsed(ps)) { used = true; } } if (used) { if (print) { System.out.print("@"); } rootElement.addContent(addElement(row, col, "unit")); } else { if (print) { System.out.print("*"); } } k++; } catch (Exception e) { if (print) { System.out.print("#"); } rootElement.addContent(addElement(row, col, "obstacle")); } } else { rootElement.addContent(addElement(row, col, "obstacle")); if (print) { System.out.print("#"); } } } if (print) { System.out.println(); } else { int z = (int) ((row / 177.0) * 100); if (old_z != z) { System.out.write(progressBar(z, 50).getBytes()); old_z = z; } } } System.out.format("\nNumber of CLB tiles %d\n", k); System.out.format("Number of columns %d; Number of rows %d\n", design.getDevice().getColumns(), design.getDevice().getRows()); System.out.println("Creating XML file with device floorplan..."); XMLOutputter xml = new XMLOutputter(); xml.setFormat(Format.getPrettyFormat()); String path = "floorplan.xml"; if (!print) { PrintWriter xmlWriter = new PrintWriter(path, "UTF-8"); xmlWriter.print(xml.outputString(writer)); xmlWriter.close(); System.out.println("FPGA description saved in floorplan.xml!"); } }
From source file:pt.ist.socialsoftware.edition.export.ExpertEditionTEIExport.java
License:Creative Commons License
private void generateCorpusHeader(Element rootElement) { Element newElement = new Element("teiHeader", this.xmlns); Attribute type = new Attribute("type", "corpus"); newElement.setAttribute(type);//from ww w . j a va 2 s. co m rootElement.addContent(newElement); }
From source file:pt.ist.socialsoftware.edition.export.ExpertEditionTEIExport.java
License:Creative Commons License
private void generateTextHeader(Fragment fragment, Element rootElement) { Element headerElement = new Element("teiHeader", this.xmlns); Attribute type = new Attribute("type", "text"); headerElement.setAttribute(type);// w ww . jav a 2 s. co m rootElement.addContent(headerElement); Element fileDescElement = new Element("fileDesc", this.xmlns); headerElement.addContent(fileDescElement); // titleStmt codification Element titleStmtElement = new Element("titleStmt", this.xmlns); fileDescElement.addContent(titleStmtElement); Element titleElement = new Element("title", this.xmlns); titleStmtElement.addContent(titleElement); titleElement.addContent(fragment.getTitle()); Element authorElement = new Element("author", this.xmlns); titleStmtElement.addContent(authorElement); authorElement.addContent(fragment.getLdoD().getAuthor()); Element respStmtElement = new Element("respStmt", this.xmlns); titleStmtElement.addContent(respStmtElement); Element respElement = new Element("resp", this.xmlns); respStmtElement.addContent(respElement); respElement.addContent("encoding"); // TODO: coder's name Element nameElement = new Element("name", this.xmlns); respStmtElement.addContent(nameElement); // publicationStmt codification Element publicationStmtElement = new Element("publicationStmt", this.xmlns); fileDescElement.addContent(publicationStmtElement); Element publisherElement = new Element("publisher", this.xmlns); publicationStmtElement.addContent(publisherElement); publisherElement.addContent("University of Coimbra"); Element pubPlaceElement = new Element("pubPlace", this.xmlns); publicationStmtElement.addContent(pubPlaceElement); pubPlaceElement.addContent("Coimbra"); Element availabilityElement = new Element("availability", this.xmlns); publicationStmtElement.addContent(availabilityElement); availabilityElement.setAttribute("status", "restricted"); Element licenceElement = new Element("licence", this.xmlns); availabilityElement.addContent(licenceElement); licenceElement.setAttribute("target", "http://creativecommons.org/licenses/by-sa/3.0/"); // TODO: <p>xpto</p> ? Element dateElement = new Element("date", this.xmlns); publicationStmtElement.addContent(dateElement); dateElement.setAttribute("when", "2014"); Element sourceDescElement = new Element("sourceDesc", this.xmlns); fileDescElement.addContent(sourceDescElement); generateSources(fragment, sourceDescElement); generateWitnesses(fragment, sourceDescElement); }
From source file:pt.ist.socialsoftware.edition.visitors.TEIGenerator.java
License:Creative Commons License
private void generateCorpusHeader(Element rootElement) { Element newElement = new Element("teiHeader", xmlns); Attribute type = new Attribute("type", "corpus"); newElement.setAttribute(type);/*w w w .j a va 2 s .com*/ rootElement.addContent(newElement); }
From source file:pt.ist.socialsoftware.edition.visitors.TEIGenerator.java
License:Creative Commons License
private void generateTextHeader(Fragment fragment, Element rootElement) { Element headerElement = new Element("teiHeader", xmlns); Attribute type = new Attribute("type", "text"); headerElement.setAttribute(type);/*from w ww .j a va 2s.c om*/ rootElement.addContent(headerElement); Element fileDescElement = new Element("fileDesc", xmlns); headerElement.addContent(fileDescElement); // titleStmt codification Element titleStmtElement = new Element("titleStmt", xmlns); fileDescElement.addContent(titleStmtElement); Element titleElement = new Element("title", xmlns); titleStmtElement.addContent(titleElement); titleElement.addContent(fragment.getTitle()); Element authorElement = new Element("author", xmlns); titleStmtElement.addContent(authorElement); authorElement.addContent(fragment.getLdoD().getAuthor()); Element respStmtElement = new Element("respStmt", xmlns); titleStmtElement.addContent(respStmtElement); Element respElement = new Element("resp", xmlns); respStmtElement.addContent(respElement); respElement.addContent("encoding"); // TODO: coder's name Element nameElement = new Element("name", xmlns); respStmtElement.addContent(nameElement); // publicationStmt codification Element publicationStmtElement = new Element("publicationStmt", xmlns); fileDescElement.addContent(publicationStmtElement); Element publisherElement = new Element("publisher", xmlns); publicationStmtElement.addContent(publisherElement); publisherElement.addContent("University of Coimbra"); Element pubPlaceElement = new Element("pubPlace", xmlns); publicationStmtElement.addContent(pubPlaceElement); pubPlaceElement.addContent("Coimbra"); Element availabilityElement = new Element("availability", xmlns); publicationStmtElement.addContent(availabilityElement); availabilityElement.setAttribute("status", "restricted"); Element licenceElement = new Element("licence", xmlns); availabilityElement.addContent(licenceElement); licenceElement.setAttribute("target", "http://creativecommons.org/licenses/by-sa/3.0/"); // TODO: <p>xpto</p> ? Element dateElement = new Element("date", xmlns); publicationStmtElement.addContent(dateElement); dateElement.setAttribute("when", "2014"); Element sourceDescElement = new Element("sourceDesc", xmlns); fileDescElement.addContent(sourceDescElement); generateSources(fragment, sourceDescElement); generateWitnesses(fragment, sourceDescElement); }
From source file:se.miun.itm.input.model.mapping.StructuralMapping.java
License:Open Source License
@Override public IMapping clone(String id, Element mergedPriority) throws InPUTException { String constructor = mergedPriority.getAttributeValue(Q.CONSTR_ATTR); if (constructor == null) constructor = constructorSignature; if (mergedPriority.getAttributeValue(Q.GET_ATTR) == null && getGetter() == null) mergedPriority.setAttribute(new Attribute(Q.GET_ATTR, "false")); if (mergedPriority.getAttributeValue(Q.SET_ATTR) == null && getSetter() == null) mergedPriority.setAttribute(new Attribute(Q.SET_ATTR, "false")); return new StructuralMapping(mergedPriority, componentType, constructor); }