/** * Remove comments from XML * * @param document * @throws IOException * @throws DocumentException */ private static void removeComments(Document document) throws IOException, DocumentException { Visitor visitor = new VisitorSupport() { @Override public void visit(Comment comment) { comment.setText(" "); } }; document.accept(visitor); }
public void assertNodesEqual( Node n1, Node n2 ) { int nodeType1 = n1.getNodeType(); int nodeType2 = n2.getNodeType(); assertTrue( "Nodes are of same type: ", nodeType1 == nodeType2 ); switch (nodeType1) { case Node.ELEMENT_NODE: assertNodesEqual((Element) n1, (Element) n2); break; case Node.DOCUMENT_NODE: assertNodesEqual((Document) n1, (Document) n2); break; case Node.ATTRIBUTE_NODE: assertNodesEqual((Attribute) n1, (Attribute) n2); break; case Node.TEXT_NODE: assertNodesEqual((Text) n1, (Text) n2); break; case Node.CDATA_SECTION_NODE: assertNodesEqual((CDATA) n1, (CDATA) n2); break; case Node.ENTITY_REFERENCE_NODE: assertNodesEqual((Entity) n1, (Entity) n2); break; case Node.PROCESSING_INSTRUCTION_NODE: assertNodesEqual((ProcessingInstruction) n1, (ProcessingInstruction) n2); break; case Node.COMMENT_NODE: assertNodesEqual((Comment) n1, (Comment) n2); break; case Node.DOCUMENT_TYPE_NODE: assertNodesEqual((DocumentType) n1, (DocumentType) n2); break; case Node.NAMESPACE_NODE: assertNodesEqual((Namespace) n1, (Namespace) n2); break; default: assertTrue( "Invalid node types. node1: " + n1 + " and node2: " + n2, false ); } }
/** * Print the given element using the given print writer and initial * indention * @param elem the xml element * @param indention the initial indention * @param writer the print writer to use for the output */ @SuppressWarnings("unchecked") public void print(Element elem, String indention, PrintWriter writer) { writer.print(indention + "<" + elem.getName()); Iterator<Attribute> itAtt = elem.attributeIterator(); Iterator<Element> itElem = elem.elementIterator(); if (elem.hasMixedContent() || (elem.hasContent() && !itElem.hasNext())) { Iterator<Node> it = elem.nodeIterator(); while (it.hasNext()) { Node node = it.next(); if (node instanceof CharacterData) { if (!(node instanceof Comment) && node.getText().trim().length() != 0) { throw new IllegalArgumentException("text content not supported: \"" + node.getText() + "\""); } } else if (!(node instanceof Element || node instanceof Attribute)) { throw new IllegalArgumentException("only attributes and elements are supported"); } } } while (itAtt.hasNext()) { Attribute att = itAtt.next(); final String attName = att.getName(); final String attValue = att.getValue(); writer.print(" " + attName + "=\"" + escapeAttributeValue(attValue) + "\""); } if (!itElem.hasNext()) { writer.println("/>"); } else { writer.println(">"); while (itElem.hasNext()) { print(itElem.next(), indention + getIndention(), writer); } writer.println(indention + "</" + elem.getName() + ">"); } writer.flush(); }
public void add(Comment comment) { element.add( comment ); }
public boolean remove(Comment comment) { return element.remove( comment ); }
protected void writeElement(Element element) throws IOException { int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); writePrintln(); indent(); writer.write("<"); writer.write(qualifiedName); boolean textOnly = true; for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Element) { textOnly = false; } else if (node instanceof Comment) { textOnly = false; } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); } else { writer.write(">"); if (textOnly) { // we have at least one text node so lets assume // that its non-empty writeElementContent(element); } else { if (element.attributeCount() > 3) writePrintln(); // we know it's not null or empty from above ++indentLevel; writeElementContent(element); --indentLevel; writePrintln(); indent(); } writer.write("</"); writer.write(qualifiedName); writer.write(">"); } if (element.attributeCount() > 2 && indentLevel > 0) writePrintln(); lastOutputNodeType = Node.ELEMENT_NODE; }
public void add(Comment comment) { getWrapped().add(comment); }
public boolean remove(Comment comment) { return getWrapped().remove(comment); }
protected void writeElement(Element element) throws IOException { int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); writePrintln(); indent(); writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations boolean textOnly = true; for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } else if (node instanceof Element) { textOnly = false; } else if (node instanceof Comment) { textOnly = false; } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); } else { writer.write(">"); if (textOnly) { // we have at least one text node so lets assume // that its non-empty writeElementContent(element); } else { // we know it's not null or empty from above ++indentLevel; writeElementContent(element); --indentLevel; writePrintln(); indent(); } writer.write("</"); writer.write(qualifiedName); writer.write(">"); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; }
public void add(Comment comment) { target().add( comment ); }
public boolean remove(Comment comment) { return target().remove( comment ); }
public boolean isComment(Object obj) { return obj instanceof Comment; }
public String getCommentStringValue(Object obj) { Comment cmt = (Comment) obj; return cmt.getText(); }
/** * Writes the given {@link Comment}. * * @param comment * <code>Comment</code> to output. * * @throws IOException * DOCUMENT ME! */ public void write(Comment comment) throws IOException { writeComment(comment.getText()); if (autoFlush) { flush(); } }