/** * Utility method that allows for convenient conversion of * <a href="http://www.cafeconleche.org/XOM/apidocs/index.html">XOM</a> elements to * CML elements if necessary. * always makes a copy * * @param xml a valid CMLElement * @return the {@link CMLElement} */ public static CMLElement ensureCML(Element xml) { if (xml == null) { throw new RuntimeException("null cml"); } if (xml instanceof CMLElement) { return (CMLElement) xml.copy(); } else { Document doc = new Document((Element) xml.copy()); try { Document doc2 = new CMLBuilder().build(doc.toXML(), doc.getBaseURI()); CMLElement cmlElement = (CMLElement) doc2.getRootElement(); return cmlElement; } catch (Exception e) { CMLUtil.debug(xml, "ensureCMLProblem "+e); throw new RuntimeException("ensureCMLProblem", e); } } }
private void writeDocument(Document doc) throws IOException { if (DEBUG) System.err.println("writing document"); writeXMLDeclaration(doc.getBaseURI()); for (int i = 0; i < doc.getChildCount(); i++) { Node node = doc.getChild(i); if (node instanceof Element) { writeElement((Element) node); } else if (node instanceof Comment) { writeComment((Comment) node); } else if (node instanceof ProcessingInstruction) { writeProcessingInstruction((ProcessingInstruction) node); } else if (node instanceof DocType) { writeDocType((DocType) node); } else { throw new IllegalAddException("Cannot write node type: " + node); } } writeEndDocument(); if (DEBUG) System.err.println("finished writing document"); }
private void writeDocument(Document doc) throws IOException { if (DEBUG) System.err.println("writing document"); writeXMLDeclaration(doc.getBaseURI()); for (int i = 0; i < doc.getChildCount(); i++) { Node node = doc.getChild(i); if (node instanceof Element) { writeElement((Element) node); } else if (node instanceof Comment) { writeComment((Comment) node); } else if (node instanceof ProcessingInstruction) { writeProcessingInstruction((ProcessingInstruction) node); } else if (node instanceof DocType) { writeDocType((DocType) node); } else { throw new IllegalAddException("Cannot write node type: " + node); } } writeEndDocument(); if (DEBUG) System.err.println("finished writing document"); }