List of usage examples for org.dom4j Document addElement
Element addElement(String name);
Element
node with the given name to this branch and returns a reference to the new node. From source file:org.craftercms.cstudio.impl.repository.alfresco.AlfrescoContentRepository.java
License:Open Source License
@Override public InputStream getMetadataStream(String site, String path) { PersistenceManagerService persistenceManagerService = _servicesManager .getService(PersistenceManagerService.class); NodeRef nodeRef = persistenceManagerService .getNodeRef(SITE_REPO_ROOT_PATTERN.replaceAll(SITE_REPLACEMENT_PATTERN, site), path); Map<QName, Serializable> contentProperties = persistenceManagerService.getProperties(nodeRef); Document metadataDoc = DocumentHelper.createDocument(); Element root = metadataDoc.addElement("metadata"); for (Map.Entry<QName, Serializable> property : contentProperties.entrySet()) { Element elem = root.addElement(property.getKey().getLocalName()); elem.addText(String.valueOf(property.getValue())); }//from w ww.j a v a2s. com return IOUtils.toInputStream(metadataDoc.asXML()); }
From source file:org.dentaku.gentaku.tools.cgen.xmi.XMIGenTask.java
License:Apache License
private Branch createXMIDocument(Document document) { Element root = document.addElement("XMI").addAttribute("xmi.version", "1.2") .addAttribute("timestamp", GregorianCalendar.getInstance().getTime().toString()) .addNamespace("UML", "omg.org/UML/1.4"); // xmi.addDocType("XMI", "SYSTEM", "uml14xmi12.dtd"); Element header = root.addElement("XMI.header"); header.addElement("XMI.documentation").addElement("XMI.exporter").setText("Gentaku Generator Generator"); header.addElement("XMI.metamodel").addAttribute("xmi.name", "UML").addAttribute("xmi.version", "1.4"); Element content = root.addElement("XMI.content"); return content; }
From source file:org.dom4j.samples.CreateXMLDemo.java
License:Open Source License
protected Document createDocument() throws Exception { Document document = DocumentHelper.createDocument(); Element root = document.addElement("system"); Properties properties = System.getProperties(); for (Enumeration elements = properties.propertyNames(); elements.hasMoreElements();) { String name = (String) elements.nextElement(); String value = properties.getProperty(name); Element element = root.addElement("property"); element.addAttribute("name", name); element.addText(value);/* ww w. ja v a2s .c om*/ } return document; }
From source file:org.dtolabs.rundeck.resources.format.XmlGenerator.java
License:Apache License
public void generate(final RundeckNodesRep nodesRep, final OutputStream out) throws IOException { //create dom// w w w . java 2s . c o m final Document document = DocumentFactory.getInstance().createDocument(); final Element root = document.addElement("project"); for (final Map<RundeckNodeMeta, String> map : nodesRep.getContent()) { final Element element = root.addElement("node"); element.addAttribute("type", "Node"); for (final RundeckNodeMeta s1 : map.keySet()) { element.addAttribute(attrMapping.get(s1), map.get(s1)); } } writeDocument(document, out); }
From source file:org.ecocean.servlet.ScanResultsServlet.java
License:Open Source License
public boolean writeXML(HttpServletRequest request, Vector results, String num, String newEncDate, String newEncShark, String newEncSize) { String context = "context0"; context = ServletUtilities.getContext(request); try {/*from w ww .j a v a 2 s. c o m*/ System.out.println("Prepping to write XML file for encounter " + num); //now setup the XML write for the encounter int resultsSize = results.size(); MatchObject[] matches = new MatchObject[resultsSize]; for (int a = 0; a < resultsSize; a++) { matches[a] = (MatchObject) results.get(a); } Arrays.sort(matches, new MatchComparator()); StringBuffer resultsXML = new StringBuffer(); Document document = DocumentHelper.createDocument(); Element root = document.addElement("matchSet"); root.addAttribute("scanDate", (new java.util.Date()).toString()); root.addAttribute("R", request.getParameter("R")); root.addAttribute("epsilon", request.getParameter("epsilon")); root.addAttribute("Sizelim", request.getParameter("Sizelim")); root.addAttribute("maxTriangleRotation", request.getParameter("maxTriangleRotation")); root.addAttribute("C", request.getParameter("C")); for (int i = 0; i < matches.length; i++) { MatchObject mo = matches[i]; Element match = root.addElement("match"); match.addAttribute("points", (new Double(mo.matchValue)).toString()); match.addAttribute("adjustedpoints", (new Double(mo.adjustedMatchValue)).toString()); match.addAttribute("pointBreakdown", mo.pointBreakdown); String finalscore = (new Double(mo.matchValue * mo.adjustedMatchValue)).toString(); if (finalscore.length() > 7) { finalscore = finalscore.substring(0, 6); } match.addAttribute("finalscore", finalscore); //check if logM is very small... try { match.addAttribute("logMStdDev", (new Double(mo.getLogMStdDev())).toString()); } catch (java.lang.NumberFormatException nfe) { match.addAttribute("logMStdDev", "<0.01"); } match.addAttribute("evaluation", mo.getEvaluation()); Element enc = match.addElement("encounter"); enc.addAttribute("number", mo.encounterNumber); enc.addAttribute("date", mo.date); enc.addAttribute("sex", mo.catalogSex); enc.addAttribute("assignedToShark", mo.getIndividualName()); enc.addAttribute("size", ((new Double(mo.size)).toString() + " meters")); for (int k = 0; k < mo.scores.size(); k++) { Element spot = enc.addElement("spot"); spot.addAttribute("x", (new Double(((VertexPointMatch) mo.scores.get(k)).oldX)).toString()); spot.addAttribute("y", (new Double(((VertexPointMatch) mo.scores.get(k)).oldY)).toString()); } Element enc2 = match.addElement("encounter"); enc2.addAttribute("number", num); enc2.addAttribute("date", newEncDate); enc2.addAttribute("sex", mo.newSex); enc2.addAttribute("assignedToShark", newEncShark); enc2.addAttribute("size", (newEncSize + " meters")); for (int j = 0; j < mo.scores.size(); j++) { Element spot = enc2.addElement("spot"); spot.addAttribute("x", (new Double(((VertexPointMatch) mo.scores.get(j)).newX)).toString()); spot.addAttribute("y", (new Double(((VertexPointMatch) mo.scores.get(j)).newY)).toString()); } } //prep for writing out the XML //setup data dir String rootWebappPath = getServletContext().getRealPath("/"); File webappsDir = new File(rootWebappPath).getParentFile(); File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context)); //if(!shepherdDataDir.exists()){shepherdDataDir.mkdirs();} //File encountersDir=new File(shepherdDataDir.getAbsolutePath()+"/encounters"); //if(!encountersDir.exists()){encountersDir.mkdirs();} //in case this is a right-side scan, change file name to save to String fileAddition = ""; if ((request.getParameter("rightSide") != null) && (request.getParameter("rightSide").equals("true"))) { fileAddition = "Right"; } //File file=new File((new File(".")).getCanonicalPath()+File.separator+"webapps"+File.separator+"ROOT"+File.separator+"encounters"+File.separator+num+File.separator+"lastFull"+fileAddition+"Scan.xml"); //File file = new File(encountersDir.getAbsoluteFile()+"/"+ num + "/lastFull" + fileAddition + "Scan.xml"); File file = new File(Encounter.dir(shepherdDataDir, num) + "/lastFull" + fileAddition + "Scan.xml"); FileWriter mywriter = new FileWriter(file); org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint(); format.setLineSeparator(System.getProperty("line.separator")); org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(mywriter, format); writer.write(document); writer.close(); System.out.println("Successful write."); return true; } catch (Exception e) { System.out.println("Encountered an error trying to write back XML results!"); e.printStackTrace(); return false; } }
From source file:org.ecocean.servlet.WriteOutScanTask.java
License:Open Source License
public boolean writeResult(MatchObject[] swirs, String num, String R, String epsilon, String Sizelim, String maxTriangleRotation, String C, String newEncDate, String newEncShark, String newEncSize, boolean rightSide, double cutoff, Shepherd myShepherd, String context) { try {/* w w w . j a va 2s . c om*/ //System.out.println("Prepping to write XML file for encounter "+num); //now setup the XML write for the encounter int resultsSize = swirs.length; MatchObject[] matches = swirs; Arrays.sort(matches, new MatchComparator()); StringBuffer resultsXML = new StringBuffer(); Document document = DocumentHelper.createDocument(); Element root = document.addElement("matchSet"); root.addAttribute("scanDate", (new java.util.Date()).toString()); root.addAttribute("R", R); root.addAttribute("epsilon", epsilon); root.addAttribute("Sizelim", Sizelim); root.addAttribute("maxTriangleRotation", maxTriangleRotation); root.addAttribute("C", C); int numMatches = matches.length; //hard limit this to 100 matches...no human really goes beyond this... if (numMatches > 100) numMatches = 100; for (int i = 0; i < numMatches; i++) { try { MatchObject mo = matches[i]; if ((mo.getMatchValue() > 0) && ((mo.getMatchValue() * mo.getAdjustedMatchValue()) > 2)) { Element match = root.addElement("match"); match.addAttribute("points", (new Double(mo.getMatchValue())).toString()); match.addAttribute("adjustedpoints", (new Double(mo.getAdjustedMatchValue())).toString()); match.addAttribute("pointBreakdown", mo.getPointBreakdown()); String finalscore = (new Double(mo.getMatchValue() * mo.getAdjustedMatchValue())) .toString(); if (finalscore.length() > 7) { finalscore = finalscore.substring(0, 6); } match.addAttribute("finalscore", finalscore); //check if logM is very small... try { match.addAttribute("logMStdDev", (new Double(mo.getLogMStdDev())).toString()); } catch (java.lang.NumberFormatException nfe) { match.addAttribute("logMStdDev", "<0.01"); } match.addAttribute("evaluation", mo.getEvaluation()); Encounter firstEnc = myShepherd.getEncounter(mo.getEncounterNumber()); Element enc = match.addElement("encounter"); enc.addAttribute("number", firstEnc.getEncounterNumber()); enc.addAttribute("date", firstEnc.getDate()); if (firstEnc.getSex() != null) { enc.addAttribute("sex", firstEnc.getSex()); } else { enc.addAttribute("sex", "unknown"); } enc.addAttribute("assignedToShark", ServletUtilities.handleNullString(firstEnc.getIndividualID())); if (firstEnc.getSizeAsDouble() != null) { enc.addAttribute("size", (firstEnc.getSize() + " meters")); } enc.addAttribute("location", firstEnc.getLocation()); enc.addAttribute("locationID", firstEnc.getLocationID()); VertexPointMatch[] firstScores = mo.getScores(); try { for (int k = 0; k < firstScores.length; k++) { Element spot = enc.addElement("spot"); spot.addAttribute("x", (new Double(firstScores[k].getOldX())).toString()); spot.addAttribute("y", (new Double(firstScores[k].getOldY())).toString()); } } catch (NullPointerException npe) { } Element enc2 = match.addElement("encounter"); Encounter secondEnc = myShepherd.getEncounter(num); enc2.addAttribute("number", num); enc2.addAttribute("date", secondEnc.getDate()); //enc2.addAttribute("sex", secondEnc.getSex()); if (secondEnc.getSex() != null) { enc2.addAttribute("sex", secondEnc.getSex()); } else { enc2.addAttribute("sex", "unknown"); } enc2.addAttribute("assignedToShark", ServletUtilities.handleNullString(secondEnc.getIndividualID())); if (secondEnc.getSizeAsDouble() != null) { enc2.addAttribute("size", (secondEnc.getSize() + " meters")); } else { enc2.addAttribute("size", "unknown"); } enc2.addAttribute("location", secondEnc.getLocation()); enc2.addAttribute("locationID", secondEnc.getLocationID()); try { for (int j = 0; j < firstScores.length; j++) { Element spot = enc2.addElement("spot"); spot.addAttribute("x", (new Double(firstScores[j].getNewX())).toString()); spot.addAttribute("y", (new Double(firstScores[j].getNewY())).toString()); } } catch (NullPointerException npe) { } //let's find the keywords in common List<String> keywords = myShepherd.getKeywordsInCommon(mo.getEncounterNumber(), num); int keywordsSize = keywords.size(); if (keywordsSize > 0) { Element kws = match.addElement("keywords"); for (int y = 0; y < keywordsSize; y++) { Element keyword = kws.addElement("keyword"); keyword.addAttribute("name", ((String) keywords.get(y))); } } } //end if } catch (Exception finale) { finale.printStackTrace(); } } //end for //prep for writing out the XML //in case this is a right-side scan, change file name to save to String fileAddition = ""; if (rightSide) { fileAddition = "Right"; } //setup data dir String rootWebappPath = getServletContext().getRealPath("/"); File webappsDir = new File(rootWebappPath).getParentFile(); File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context)); //if(!shepherdDataDir.exists()){shepherdDataDir.mkdirs();} File encountersDir = new File(shepherdDataDir.getAbsolutePath() + "/encounters"); //if(!encountersDir.exists()){encountersDir.mkdirs();} String thisEncDirString = Encounter.dir(shepherdDataDir, num); File thisEncounterDir = new File(thisEncDirString); if (!thisEncounterDir.exists()) { thisEncounterDir.mkdirs(); System.out.println("I am making the encDir: " + thisEncDirString); } //File file=new File((new File(".")).getCanonicalPath()+File.separator+"webapps"+File.separator+"ROOT"+File.separator+"encounters"+File.separator+num+File.separator+"lastFull"+fileAddition+"Scan.xml"); File file = new File(Encounter.dir(shepherdDataDir, num) + "/lastFull" + fileAddition + "Scan.xml"); System.out.println("Writing scanTask XML file to: " + file.getAbsolutePath()); FileWriter mywriter = new FileWriter(file); org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint(); format.setLineSeparator(System.getProperty("line.separator")); org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(mywriter, format); writer.write(document); writer.close(); System.out.println("Successful write."); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:org.ecocean.servlet.WriteOutScanTask.java
License:Open Source License
public boolean i3sWriteThis(Shepherd myShepherd, MatchObject[] matches, String num, String newEncDate, String newEncShark, String newEncSize, boolean rightSide, double cutoff, String context) { try {//from w w w .j a v a 2s . c o m System.out.println("scanWorkItemResultsHandler: Prepping to write I3S XML file for encounter " + num); //now setup the XML write for the encounter //int resultsSize=results.size(); Arrays.sort(matches, new NewI3SMatchComparator()); StringBuffer resultsXML = new StringBuffer(); Document document = DocumentHelper.createDocument(); Element root = document.addElement("matchSet"); root.addAttribute("scanDate", (new java.util.Date()).toString()); //System.out.println("Total num matches for I3S printing: "+matches.length); int numMatches = matches.length; //hard limit this to 100 matches...no human really goes beyond this... if (numMatches > 100) numMatches = 100; for (int i = 0; i < numMatches; i++) { try { //System.out.println(); MatchObject mo = matches[i]; //System.out.println("I3S match value: "+mo.getI3SMatchValue()); if ((mo.getI3SMatchValue() > 0.001) && (mo.getI3SMatchValue() <= 2.0)) { Element match = root.addElement("match"); String finalscore = (new Double(mo.getI3SMatchValue())).toString(); if (finalscore.length() > 7) { finalscore = finalscore.substring(0, 6); } match.addAttribute("finalscore", finalscore); match.addAttribute("evaluation", mo.getEvaluation()); Element enc = match.addElement("encounter"); enc.addAttribute("number", mo.getEncounterNumber()); enc.addAttribute("date", mo.getDate()); if (mo.getSex() != null) { enc.addAttribute("sex", mo.getSex()); } else { enc.addAttribute("sex", "unknown"); } enc.addAttribute("assignedToShark", mo.getIndividualName()); enc.addAttribute("size", (new Double(mo.getSize())).toString()); //get the Map Vector map = mo.getMap2(); int mapSize = map.size(); Encounter e1 = myShepherd.getEncounter(mo.getEncounterNumber()); for (int f = 0; f < mapSize; f++) { Pair tempPair = (com.reijns.I3S.Pair) map.get(f); int M1 = tempPair.getM1(); ArrayList<SuperSpot> spts = new ArrayList<SuperSpot>(); if (rightSide) { spts = e1.getRightSpots(); } else { spts = e1.getSpots(); } //System.out.println("scanWorkItemResultsHandler: I3S spots: "+spts.size()+" vs mapSize: "+mapSize); Element spot = enc.addElement("spot"); spot.addAttribute("x", (new Double(spts.get(M1).getTheSpot().getCentroidX())).toString()); spot.addAttribute("y", (new Double(spts.get(M1).getTheSpot().getCentroidY())).toString()); } Element enc2 = match.addElement("encounter"); enc2.addAttribute("number", num); enc2.addAttribute("date", newEncDate); enc2.addAttribute("sex", mo.getNewSex()); enc2.addAttribute("assignedToShark", newEncShark); enc2.addAttribute("size", newEncSize); //reset the Iterator Encounter e2 = myShepherd.getEncounter(num); for (int g = 0; g < mapSize; g++) { Pair tempPair = (com.reijns.I3S.Pair) map.get(g); int M2 = tempPair.getM2(); ArrayList<SuperSpot> spts = new ArrayList<SuperSpot>(); if (rightSide) { spts = e2.getRightSpots(); } else { spts = e2.getSpots(); } Element spot = enc2.addElement("spot"); //System.out.println("scanWorkItemResultsHandler: I3S next spots: "+spts.size()+" vs mapSize: "+mapSize); spot.addAttribute("x", (new Double(spts.get(M2).getTheSpot().getCentroidX())).toString()); spot.addAttribute("y", (new Double(spts.get(M2).getTheSpot().getCentroidY())).toString()); } } } catch (NullPointerException npe) { npe.printStackTrace(); } } //prep for writing out the XML //in case this is a right-side scan, change file name to save to String fileAddition = ""; if (rightSide) { fileAddition = "Right"; } //setup data dir String rootWebappPath = getServletContext().getRealPath("/"); File webappsDir = new File(rootWebappPath).getParentFile(); File shepherdDataDir = new File(webappsDir, CommonConfiguration.getDataDirectoryName(context)); //if(!shepherdDataDir.exists()){shepherdDataDir.mkdirs();} File encountersDir = new File(shepherdDataDir.getAbsolutePath() + "/encounters"); //if(!encountersDir.exists()){encountersDir.mkdirs();} //File file=new File((new File(".")).getCanonicalPath()+File.separator+"webapps"+File.separator+"ROOT"+File.separator+"encounters"+File.separator+num+File.separator+"lastFull"+fileAddition+"I3SScan.xml"); File file = new File(Encounter.dir(shepherdDataDir, num) + "/lastFull" + fileAddition + "I3SScan.xml"); FileWriter mywriter = new FileWriter(file); org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint(); format.setLineSeparator(System.getProperty("line.separator")); org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(mywriter, format); writer.write(document); writer.close(); System.out.println("writeOutScanTask: Successful I3S write."); return true; } catch (Exception e) { System.out.println("writeOutScanTask: Failed to write out I3S results!"); e.printStackTrace(); return false; } }
From source file:org.ednovo.gooru.infrastructure.persistence.hibernate.taxonomy.TaxonomyRepositoryHibernate.java
License:Open Source License
@SuppressWarnings("rawtypes") public String makeTree(Code rootCode) { int depth = findMaxDepthInTaxonomy(rootCode, rootCode.getOrganization().getPartyUid()); char[] alphas = new char[depth + 1]; for (int c = 0; c <= depth; c++) alphas[c] = (char) (97 + c); Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("node"); // need to update root.addAttribute("code", rootCode.getCode()); root.addAttribute("type", rootCode.getCodeType().getLabel()); root.addAttribute("depth", "0"); root.addAttribute("codeId", rootCode.getCodeId().toString()); root.addAttribute("codeUId", rootCode.getCodeUid().toString()); root.addAttribute("taxonomyImageUrl", rootCode.getTaxonomyImageUrl()); root.addAttribute("displayCode", rootCode.getdisplayCode()); root.addAttribute("activeFlag", rootCode.getActiveFlag() + ""); String q1 = "select label from taxonomy_level_type where code_id=" + rootCode.getRootNodeId() + " order by depth"; @SuppressWarnings("unchecked") List<String> labels = this.getJdbcTemplate().query(q1, new RowMapper() { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { return rs.getString("label"); }/*from www . j ava 2 s . co m*/ }); for (int i = 1; i <= depth; i++) { StringBuilder query = new StringBuilder("SELECT "); for (int j = 0; j <= i; j++) { query.append(alphas[j] + ".display_code, " + alphas[j] + ".code_image, " + alphas[j] + ".code_uid, " + alphas[j] + ".code_id, " + alphas[j] + ".display_order, " + alphas[j] + ".depth, " + alphas[j] + ".label as '" + labels.get(j) + "', " + alphas[j] + ".code as '" + labels.get(j) + "' "); if (j != i) { query.append(","); } } query.append(" from "); for (int j = 0; j <= i; j++) { query.append(" code " + alphas[j]); if (j != i) { query.append(","); } } query.append(" where "); for (int j = 0; j <= i; j++) { query.append(alphas[j] + ".depth=" + j); // if(j!=i) query.append(" and "); } for (int j = 0; j < i; j++) { query.append(alphas[j + 1] + ".parent_id=" + alphas[j] + ".code_id"); // if(j!=i-1) query.append(" and " + alphas[j + 1] + ".active_flag=1 and "); } query.append(alphas[0] + ".code_id=" + rootCode.getCodeId()); query.append(" and " + alphas[0] + ".active_flag=1"); @SuppressWarnings("unchecked") List<String[]> labelss = this.getJdbcTemplateReadOnly().query(query.toString(), new RowMapper() { public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException { final ResultSetMetaData metaData = rs.getMetaData(); final int noofcolumns = metaData.getColumnCount(); final String label[] = new String[noofcolumns]; for (int m = 1; m <= noofcolumns; m++) { label[m - 1] = rs.getString(m); } return label; } }); @SuppressWarnings("unchecked") List<Node> nodes = doc.selectNodes("//node"); for (String[] array : labelss) { int length = array.length; for (Node node : nodes) { Element parent = (Element) node; String att = parent.attribute("code").getText(); if (att.equals(array[length - 9])) { // Element child = parent.addElement(labels.get(i)); Element child = parent.addElement("node"); child.addAttribute("type", labels.get(i)); child.addAttribute("code", array[length - 1]); child.addAttribute("label", array[length - 2]); child.addAttribute("depth", array[length - 3]); child.addAttribute("order", array[length - 4]); child.addAttribute("codeId", array[length - 5]); child.addAttribute("codeUId", array[length - 6]); String taxonomyImageUrl = array[length - 7] != null ? rootCode.getAssetURI() + array[length - 7] : ""; child.addAttribute("taxonomyImageUrl", taxonomyImageUrl); String displayCode = array[length - 8]; if (displayCode != null) child.addAttribute("displayCode", array[length - 8]); else child.addAttribute("displayCode", ""); child.addAttribute("activeFlag", rootCode.getActiveFlag() + ""); } } } } return doc.asXML(); }
From source file:org.forzaframework.util.XmlUtils.java
License:Apache License
public static void buildEmptyListDocument(Document doc) { Element root = doc.addElement("items"); root.addAttribute("success", "true"); doc.setRootElement(root);// ww w . j a v a 2 s . c om root.addElement("totalCount").addText("0"); }
From source file:org.forzaframework.util.XmlUtils.java
License:Apache License
public static void buildDocument(Document doc, List<? extends BaseEntity> list, Integer totalCount) { Element root = doc.addElement("items"); root.addAttribute("success", "true"); doc.setRootElement(root);/*w w w. j a va 2 s . com*/ for (BaseEntity object : list) { root.add(object.toXml()); } root.addElement("totalCount").addText(String.valueOf(totalCount)); }