List of usage examples for org.dom4j DocumentFactory createElement
public Element createElement(String name)
From source file:at.jabberwocky.impl.core.io.PacketReader.java
private Document parseDocument() throws DocumentException, IOException, XmlPullParserException { DocumentFactory df = docFactory; Document document = df.createDocument(); Element parent = null;/*from w ww . j av a 2 s.co m*/ XmlPullParser pp = parser; int count = 0; while (true) { int type = -1; type = pp.nextToken(); switch (type) { case XmlPullParser.PROCESSING_INSTRUCTION: { String text = pp.getText(); int loc = text.indexOf(" "); if (loc >= 0) { document.addProcessingInstruction(text.substring(0, loc), text.substring(loc + 1)); } else { document.addProcessingInstruction(text, ""); } break; } case XmlPullParser.COMMENT: { if (parent != null) { parent.addComment(pp.getText()); } else { document.addComment(pp.getText()); } break; } case XmlPullParser.CDSECT: { String text = pp.getText(); if (parent != null) { parent.addCDATA(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have text content outside of the root document"); } } break; } case XmlPullParser.ENTITY_REF: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have an entityref outside of the root document"); } } break; } case XmlPullParser.END_DOCUMENT: { return document; } case XmlPullParser.START_TAG: { QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace()); Element newElement = null; // Do not include the namespace if this is the start tag of a new packet // This avoids including "jabber:client", "jabber:server" or // "jabber:component:accept" if ("jabber:client".equals(qname.getNamespaceURI()) || "jabber:server".equals(qname.getNamespaceURI()) || "jabber:component:accept".equals(qname.getNamespaceURI()) || "http://jabber.org/protocol/httpbind".equals(qname.getNamespaceURI())) { newElement = df.createElement(pp.getName()); } else { newElement = df.createElement(qname); } int nsStart = pp.getNamespaceCount(pp.getDepth() - 1); int nsEnd = pp.getNamespaceCount(pp.getDepth()); for (int i = nsStart; i < nsEnd; i++) { if (pp.getNamespacePrefix(i) != null) { newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i)); } } for (int i = 0; i < pp.getAttributeCount(); i++) { QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i)) : df.createQName(pp.getAttributeName(i), pp.getAttributePrefix(i), pp.getAttributeNamespace(i)); newElement.addAttribute(qa, pp.getAttributeValue(i)); } if (parent != null) { parent.add(newElement); } else { document.add(newElement); } parent = newElement; count++; break; } case XmlPullParser.END_TAG: { if (parent != null) { parent = parent.getParent(); } count--; if (count < 1) { return document; } break; } case XmlPullParser.TEXT: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have text content outside of the root document"); } } break; } default: } } }
From source file:bio.pih.genoogle.io.Output.java
/** * @param searchResult//from w w w.j a v a2 s .com * @return {@link Element} containing the {@link SearchResults} at XML form. */ public static Element searchResultToXML(SearchResults searchResult) { assert searchResult != null; // TODO: Revise this behavor to throws the exception before. if (searchResult.hasFail()) { for (Throwable e : searchResult.getFails()) { e.printStackTrace(System.err); } } DocumentFactory factory = DocumentFactory.getInstance(); Element resultsElement = factory.createElement("results"); resultsElement.add(paramsToXML(searchResult.getParams())); resultsElement.add(hitsToXML(searchResult.getHits())); return resultsElement; }
From source file:bio.pih.genoogle.io.Output.java
/** * @param params//from ww w.ja v a 2 s. c o m * @return {@link Element} containing the {@link SearchParams} at XML form. */ public static Element paramsToXML(SearchParams params) { assert params != null; DocumentFactory factory = DocumentFactory.getInstance(); Element paramsElement = factory.createElement("params"); paramsElement.addAttribute("databank", params.getDatabank()); paramsElement.addAttribute("maxSubSequencesDistance", Integer.toString(params.getMaxSubSequencesDistance())); paramsElement.addAttribute("minHspLength", Integer.toString(params.getMinHspLength())); paramsElement.addAttribute("maxHitsResults", Integer.toString(params.getMaxHitsResults())); paramsElement.addAttribute("sequencesExtendDropoff", Integer.toString(params.getSequencesExtendDropoff())); paramsElement.addAttribute("matchScore", Integer.toString(params.getMatchScore())); paramsElement.addAttribute("mismatchScore", Integer.toString(params.getMismatchScore())); return paramsElement; }
From source file:bio.pih.genoogle.io.Output.java
/** * @param hits//from w w w .j a va 2s . c o m * @return {@link Element} containing the {@link List} of {@link Hit} at XML form. */ public static Element hitsToXML(List<Hit> hits) { assert hits != null; DocumentFactory factory = DocumentFactory.getInstance(); Element hitsElement = factory.createElement("hits"); for (Hit hit : hits) { hitsElement.add(hitToXML(hit)); } return hitsElement; }
From source file:bio.pih.genoogle.io.Output.java
/** * @param hit// w w w .j ava2 s .c o m * @return {@link Element} containing the {@link Hit} at XML form. */ public static Element hitToXML(Hit hit) { assert hit != null; DocumentFactory factory = DocumentFactory.getInstance(); Element hitElement = factory.createElement("hit"); hitElement.addAttribute("id", hit.getId()); hitElement.addAttribute("gi", hit.getGi()); hitElement.addAttribute("description", hit.getDescription()); hitElement.addAttribute("accession", hit.getAccession()); hitElement.addAttribute("length", Integer.toString(hit.getLength())); hitElement.addAttribute("databank", hit.getDatabankName()); hitElement.add(hspsToXML(hit.getHSPs())); return hitElement; }
From source file:bio.pih.genoogle.io.Output.java
/** * @param hsps/*from w ww. j ava 2 s . co m*/ * @return {@link Element} containing the {@link List} of {@link HSP} at XML form. */ public static Element hspsToXML(List<HSP> hsps) { assert hsps != null; DocumentFactory factory = DocumentFactory.getInstance(); Element hspsElement = factory.createElement("hsps"); for (HSP hsp : hsps) { hspsElement.add(hspToXML(hsp)); } return hspsElement; }
From source file:bio.pih.genoogle.io.Output.java
/** * @param hsp//from ww w.j a va2 s .c o m * @return {@link Element} containing the {@link HSP} at XML form. */ public static Element hspToXML(HSP hsp) { assert hsp != null; DocumentFactory factory = DocumentFactory.getInstance(); Element hspElement = factory.createElement("hsp"); hspElement.addAttribute("score", Integer.toString((int) hsp.getScore())); hspElement.addAttribute("normalized-score", Integer.toString((int) hsp.getNormalizedScore())); hspElement.addAttribute("e-value", eValueToString(hsp.getEValue())); hspElement.addAttribute("query-from", Integer.toString(hsp.getQueryFrom())); hspElement.addAttribute("query-to", Integer.toString(hsp.getQueryTo())); hspElement.addAttribute("hit-from", Integer.toString(hsp.getHitFrom())); hspElement.addAttribute("hit-to", Integer.toString(hsp.getHitTo())); hspElement.addAttribute("identity-len", Integer.toString(hsp.getIdentityLength())); hspElement.addAttribute("align-len", Integer.toString(hsp.getAlignLength())); hspElement.addElement("query").addText(hsp.getQuerySeq()); hspElement.addElement("align").addText(hsp.getPathSeq()); hspElement.addElement("targt").addText(hsp.getTargetSeq()); return hspElement; }
From source file:BlastResultXMLsplit.BlastXMLsplit.java
public BlastXMLsplit(String filepath, int seqnumber) throws FileNotFoundException, IOException, ParserConfigurationException { SAXReader reader = new SAXReader(); reader.setValidation(false);/*from ww w . j av a2 s . c om*/ try { System.out.println("Xlmfile reading"); Document document = reader.read(new FileInputStream(filepath));//XMLdocument? System.out.println("Xlmfile read done!"); org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory(); ArrayList str = new ArrayList();//??? String fileoutpath; org.dom4j.Element rootElm = document.getRootElement();// File f = new File(filepath); int count = 1; List<org.dom4j.Element> blastOutput_iterations = rootElm.element("BlastOutput_iterations").elements();//? org.dom4j.Element BlastOutput_program = rootElm.element("BlastOutput_program"); org.dom4j.Element BlastOutput_version = rootElm.element("BlastOutput_version"); org.dom4j.Element BlastOutput_reference = rootElm.element("BlastOutput_reference"); org.dom4j.Element BlastOutput_db = rootElm.element("BlastOutput_db"); //? BlastOutput_program.getParent().remove(BlastOutput_program); BlastOutput_version.getParent().remove(BlastOutput_version); BlastOutput_reference.getParent().remove(BlastOutput_reference); BlastOutput_db.getParent().remove(BlastOutput_db); // org.dom4j.Element BlastOutput_query_def=rootElm.element("BlastOutput_query-def"); //org.dom4j.Element BBlastOutput_query_len=rootElm.element("BlastOutput_query-len"); org.dom4j.Element BlastOutput_param = rootElm.element("BlastOutput_param"); rootElm.remove(BlastOutput_param); String Iteration_queryIDstr, Iteration_queryDefstr, Iteration_query_len; List<org.dom4j.Element> Iterationlist = null; int size = blastOutput_iterations.size(); System.out.println("Your query seqcount is " + size + "\r\n Start dividing your file"); for (int i = 0; i < size; i = i + seqnumber) { //?? fileoutpath = f.getParent() + System.getProperty("file.separator") + count + ".xml"; count++; System.out.println("The " + count + " is located in " + fileoutpath); FileOutputStream fos = new FileOutputStream(fileoutpath); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(fos, format); // XMLWriter writer = new XMLWriter(new FileOutputStream(fileoutpath)); //w?? org.dom4j.Element firstelement = blastOutput_iterations.get(i); Iteration_queryIDstr = firstelement.element("Iteration_query-ID").getText(); Iteration_queryDefstr = firstelement.element("Iteration_query-def").getText(); Iteration_query_len = firstelement.element("Iteration_query-len").getText(); //org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory(); Document document2 = DocumentHelper.createDocument(); ; org.dom4j.Element BlastOutputElement = document2.addElement("BlastOutput"); //doc.setRootElement(BlastOutputElement); //; BlastOutputElement.add(BlastOutput_program); BlastOutputElement.add(BlastOutput_version); BlastOutputElement.add(BlastOutput_reference); BlastOutputElement.add(BlastOutput_db); BlastOutputElement.addElement("BlastOutput_query-ID"); BlastOutputElement.element("BlastOutput_query-ID").setText(Iteration_queryIDstr); BlastOutputElement.addElement("BlastOutput_query-def"); BlastOutputElement.element("BlastOutput_query-def").setText(Iteration_queryDefstr); BlastOutputElement.addElement("BlastOutput_query-len"); BlastOutputElement.element("BlastOutput_query-len").setText(Iteration_query_len); // Element BlastOutput_param_new=DocumentFactory.createElement("BlastOutput_param"); // for (Iterator it = BlastOutput_param.elementIterator(); it.hasNext();) { // Element tempele=(Element) it.next(); // tempele.getParent().remove(tempele); // BlastOutput_param_new.add(tempele); // } BlastOutputElement.add(BlastOutput_param); //BlastOutputElement.add(BlastOutput_param); if (i + seqnumber < blastOutput_iterations.size()) { Iterationlist = blastOutput_iterations.subList(i, i + seqnumber); } else { Iterationlist = blastOutput_iterations.subList(i, blastOutput_iterations.size() - 1); } //System.out.println(Iterationlist.size()); //?query resetIterationlist(Iterationlist); //?BlastOutput_iterations Element BlastOutput_iterations = DocumentFactory.createElement("BlastOutput_iterations"); //BlastOutputElement.addAttribute("BlastOutput_iterations"); //org.dom4j.Element BlastOutput_iterations = new org.dom4j.Element("BlastOutput_iterations"); for (int j = 0; j < Iterationlist.size(); j++) { Iterationlist.get(j).getParent().remove(Iterationlist.get(j)); //System.out.println(j); BlastOutput_iterations.add(Iterationlist.get(j)); } BlastOutputElement.add(BlastOutput_iterations); // writer.write(document2); writer.close(); BlastOutput_program.getParent().remove(BlastOutput_program); BlastOutput_version.getParent().remove(BlastOutput_version); BlastOutput_reference.getParent().remove(BlastOutput_reference); BlastOutput_db.getParent().remove(BlastOutput_db); BlastOutput_param.getParent().remove(BlastOutput_param); } } catch (DocumentException ex) { Logger.getLogger(BlastXMLsplit.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.arc.intro.CompilerSamplesProvider.java
License:Open Source License
public void init(IIntroContentProviderSite site) { if (samplesDoc == null) { File samplesXML = computeSampleXML(); Document doc = null;/* w w w .jav a 2s . co m*/ if (samplesXML.exists()) { SAXReader reader = new SAXReader(); try { doc = reader.read(samplesXML); } catch (Exception e) { ToolchainPlugin.log("Error in reading " + samplesXML, e); } } if (doc == null) { // Create a makeshift version DocumentFactory f = DocumentFactory.getInstance(); Element root = f.createElement(SAMPLES_TAG); root.addAttribute(DEMOS_ATTR, DEFAULT_DEMOS_DIR); for (int i = 0; i < targetNames.length; i += 2) { Element target = root.addElement(TARGET_TAG); target.addAttribute(NAME_ATTR, targetNames[i]); target.addAttribute(DESC_ATTR, targetNames[i + 1]); } Element category = root.addElement(CATEGORY_TAG); category.addAttribute(TITLE_ATTR, "Compiler Samples"); category.addAttribute(WORKSPACE_ATTR, ".*mide_workspace"); category.addAttribute(ID_ATTR, "compiler"); doc = f.createDocument(root); } samplesDoc = doc; } }
From source file:com.arc.mw.util.StateSaver.java
License:Open Source License
/** * Save the state of the root object into the given file. * @param out ascii stream to write to./*from www . jav a 2 s .c o m*/ * @param object object whose state is to be saved. * @exception IOException error occurred in writing file. */ public static void saveState(Writer out, IXMLSavable object) throws IOException { DocumentFactory factory = DocumentFactory.getInstance(); Document document = factory.createDocument(); Element e = factory.createElement("root"); object.saveState(e); document.setRootElement(e); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); writer.write(document); }