List of usage examples for org.w3c.dom Node COMMENT_NODE
short COMMENT_NODE
To view the source code for org.w3c.dom Node COMMENT_NODE.
Click Source Link
Comment
. From source file:Main.java
/** * based on the following quote from public Java5 javadoc of org.w3c.dom.Node.getTextContent method: * /* www. ja v a 2 s . c om*/ * "concatenation of the textContent attribute value of every child node, excluding COMMENT_NODE and * PROCESSING_INSTRUCTION_NODE nodes. This is the empty string if the node has no children" */ private static String mergeTextContent(NodeList nodes) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); final String text; switch (n.getNodeType()) { case Node.COMMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: text = null; break; default: text = getTextContent(n); break; } if (text != null) { buf.append(text); } } return buf.toString(); }
From source file:Main.java
public String getNodeType(Node node) { String type;/*from ww w . j av a2s . c om*/ switch (node.getNodeType()) { case Node.ELEMENT_NODE: { type = "Element"; break; } case Node.ATTRIBUTE_NODE: { type = "Attribute"; break; } case Node.TEXT_NODE: { type = "Text"; break; } case Node.CDATA_SECTION_NODE: { type = "CData section"; break; } case Node.ENTITY_REFERENCE_NODE: { type = "Entity reference"; break; } case Node.ENTITY_NODE: { type = "Entity"; break; } case Node.PROCESSING_INSTRUCTION_NODE: { type = "Processing instruction"; break; } case Node.COMMENT_NODE: { type = "Comment"; break; } case Node.DOCUMENT_NODE: { type = "Document"; break; } case Node.DOCUMENT_TYPE_NODE: { type = "Document type"; break; } case Node.DOCUMENT_FRAGMENT_NODE: { type = "Document fragment"; break; } case Node.NOTATION_NODE: { type = "Notation"; break; } default: { type = "???"; break; } } return type; }
From source file:Main.java
private static String prettyPrintDom(Node node, String indent, boolean isRoot, boolean escapeStrings) { String ret = ""; switch (node.getNodeType()) { case Node.DOCUMENT_NODE: // recurse on each child NodeList nodes = node.getChildNodes(); if (nodes != null) { for (int i = 0; i < nodes.getLength(); i++) { ret += prettyPrintDom(nodes.item(i), indent, isRoot, escapeStrings); }// ww w. j a v a 2s . c o m } break; case Node.ELEMENT_NODE: String name = node.getNodeName(); ret += indent + "<" + name; NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); ret += " " + current.getNodeName() + "=\"" + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue()) + "\""; } ret += ">"; // recurse on each child NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { String tmp = prettyPrintDom(children.item(i), indent + ((isRoot) ? "" : BI), false, escapeStrings); if (!tmp.replaceAll("[\\s]+", "").equals("")) if (tmp.endsWith("\n")) if (ret.endsWith("\n")) ret += tmp; else ret += "\n" + tmp; else ret += tmp; } } if (ret.endsWith("\n")) ret += indent + "</" + name + ">\n"; else ret += "</" + name + ">\n"; break; case Node.TEXT_NODE: ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue(); break; case Node.COMMENT_NODE: ret += "<!-- " + node.getNodeValue() + " -->"; break; } return ret; }
From source file:Main.java
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *///from w w w . j a v a 2s .c o m public final static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if ((target == null) || (node.getOwnerDocument() == target)) { // same Document return node.cloneNode(deep); } else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) { for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { newNode.appendChild(cloneNode(child, target, true)); } } return newNode; } }
From source file:Main.java
/** * Recursively find the comments within a node, and add them to a supplied (not null) List) * /*from ww w.j a va 2 s .c om*/ * @param node * Node in which to search for comments * @param commentList * List of comments which it will add to. */ private static void getComments(Node node, List commentList) { // if (commentList==null) throw new IllegalArgumentException ("list may not be null"); if (node != null) { if (Node.COMMENT_NODE == node.getNodeType()) { // Comment - add it commentList.add(node); } else { // Ignore } // Now recursively traverse the tree under this node. NodeList children = node.getChildNodes(); if (children != null) { // Now need to do each child in turn. for (int i = 0; i < children.getLength(); i++) { getComments(children.item(i), commentList); } } } }
From source file:Main.java
private static boolean checkNodeTypes(Node childNode) { short nodeType = childNode.getNodeType(); if (nodeType == Node.ELEMENT_NODE) { cleanEmptyTextNodes(childNode); // recurse into subtree }/*from ww w. j a va 2s . c o m*/ if (nodeType == Node.ELEMENT_NODE || nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.COMMENT_NODE) { return true; } else { return false; } }
From source file:Main.java
/** * The method inserts end-of-line+indentation Text nodes where indentation is necessary. * * @param node - node to be pretty formatted * @param identLevel - initial indentation level of the node * @param ident - additional indentation inside the node *//* ww w .jav a2 s .c o m*/ private static void prettyFormat(Node node, String identLevel, String ident) { NodeList nodelist = node.getChildNodes(); int iStart = 0; Node item = nodelist.item(0); if (item != null) { short type = item.getNodeType(); if (type == Node.ELEMENT_NODE || type == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, item); iStart = 1; } } for (int i = iStart; i < nodelist.getLength(); i++) { item = nodelist.item(i); if (item != null) { short type = item.getNodeType(); if (type == Node.TEXT_NODE && item.getNodeValue().trim().length() == 0) { if (i + 1 < nodelist.getLength()) { item.setNodeValue(EOL_XML + identLevel + ident); } else { item.setNodeValue(EOL_XML + identLevel); } } else if (type == Node.ELEMENT_NODE) { prettyFormat(item, identLevel + ident, ident); if (i + 1 < nodelist.getLength()) { Node nextItem = nodelist.item(i + 1); if (nextItem != null) { short nextType = nextItem.getNodeType(); if (nextType == Node.ELEMENT_NODE || nextType == Node.COMMENT_NODE) { Node newChild = node.getOwnerDocument() .createTextNode(EOL_XML + identLevel + ident); node.insertBefore(newChild, nextItem); i++; continue; } } } else { Node newChild = node.getOwnerDocument().createTextNode(EOL_XML + identLevel); node.appendChild(newChild); i++; continue; } } } } }
From source file:Main.java
/** * Clone given Node into target Document. If targe is null, same Document will be used. * If deep is specified, all children below will also be cloned. *//*from w w w . j av a2s . c om*/ public static Node cloneNode(Node node, Document target, boolean deep) throws DOMException { if (target == null || node.getOwnerDocument() == target) // same Document return node.cloneNode(deep); else { //DOM level 2 provides this in Document, so once xalan switches to that, //we can take out all the below and just call target.importNode(node, deep); //For now, we implement based on the javadocs for importNode Node newNode; int nodeType = node.getNodeType(); switch (nodeType) { case Node.ATTRIBUTE_NODE: newNode = target.createAttribute(node.getNodeName()); break; case Node.DOCUMENT_FRAGMENT_NODE: newNode = target.createDocumentFragment(); break; case Node.ELEMENT_NODE: Element newElement = target.createElement(node.getNodeName()); NamedNodeMap nodeAttr = node.getAttributes(); if (nodeAttr != null) for (int i = 0; i < nodeAttr.getLength(); i++) { Attr attr = (Attr) nodeAttr.item(i); if (attr.getSpecified()) { Attr newAttr = (Attr) cloneNode(attr, target, true); newElement.setAttributeNode(newAttr); } } newNode = newElement; break; case Node.ENTITY_REFERENCE_NODE: newNode = target.createEntityReference(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: newNode = target.createProcessingInstruction(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: newNode = target.createTextNode(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: newNode = target.createCDATASection(node.getNodeValue()); break; case Node.COMMENT_NODE: newNode = target.createComment(node.getNodeValue()); break; case Node.NOTATION_NODE: case Node.ENTITY_NODE: case Node.DOCUMENT_TYPE_NODE: case Node.DOCUMENT_NODE: default: throw new IllegalArgumentException("Importing of " + node + " not supported yet"); } if (deep) for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) newNode.appendChild(cloneNode(child, target, true)); return newNode; } }
From source file:TreeDumper2.java
private void dumpLoop(Node node, String indent) { switch (node.getNodeType()) { case Node.ATTRIBUTE_NODE: dumpAttributeNode((Attr) node, indent); break;/*from ww w. ja va 2s.c o m*/ case Node.CDATA_SECTION_NODE: dumpCDATASectionNode((CDATASection) node, indent); break; case Node.COMMENT_NODE: dumpCommentNode((Comment) node, indent); break; case Node.DOCUMENT_NODE: dumpDocument((Document) node, indent); break; case Node.DOCUMENT_FRAGMENT_NODE: dumpDocumentFragment((DocumentFragment) node, indent); break; case Node.DOCUMENT_TYPE_NODE: dumpDocumentType((DocumentType) node, indent); break; case Node.ELEMENT_NODE: dumpElement((Element) node, indent); break; case Node.ENTITY_NODE: dumpEntityNode((Entity) node, indent); break; case Node.ENTITY_REFERENCE_NODE: dumpEntityReferenceNode((EntityReference) node, indent); break; case Node.NOTATION_NODE: dumpNotationNode((Notation) node, indent); break; case Node.PROCESSING_INSTRUCTION_NODE: dumpProcessingInstructionNode((ProcessingInstruction) node, indent); break; case Node.TEXT_NODE: dumpTextNode((Text) node, indent); break; default: System.out.println(indent + "Unknown node"); break; } NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) dumpLoop(list.item(i), indent + " "); }
From source file:Main.java
/** * @param node//from ww w. j av a 2 s . c o m * @throws IOException */ public static void serializeNode(Node node) throws IOException { if (writer == null) writer = new BufferedWriter(new OutputStreamWriter(System.out)); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: Document doc = (Document) node; writer.write("<?xml version=\""); writer.write(doc.getXmlVersion()); writer.write("\" encoding=\"UTF-8\" standalone=\""); if (doc.getXmlStandalone()) writer.write("yes"); else writer.write("no"); writer.write("\"?>\n"); NodeList nodes = node.getChildNodes(); if (nodes != null) for (int i = 0; i < nodes.getLength(); i++) serializeNode(nodes.item(i)); break; case Node.ELEMENT_NODE: String name = node.getNodeName(); writer.write("<" + name); NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node current = attributes.item(i); writer.write(" " + current.getNodeName() + "=\""); print(current.getNodeValue()); writer.write("\""); } writer.write(">"); NodeList children = node.getChildNodes(); if (children != null) { //if ((children.item(0) != null) && (children.item(0).getNodeType() == Node.ELEMENT_NODE)) // writer.write("\n"); for (int i = 0; i < children.getLength(); i++) serializeNode(children.item(i)); if ((children.item(0) != null) && (children.item(children.getLength() - 1).getNodeType() == Node.ELEMENT_NODE)) writer.write(""); } writer.write("</" + name + ">"); break; case Node.TEXT_NODE: print(node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: writer.write("CDATA"); print(node.getNodeValue()); writer.write(""); break; case Node.COMMENT_NODE: writer.write("<!-- " + node.getNodeValue() + " -->\n"); break; case Node.PROCESSING_INSTRUCTION_NODE: writer.write("<?" + node.getNodeName() + " " + node.getNodeValue() + "?>\n"); break; case Node.ENTITY_REFERENCE_NODE: writer.write("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: DocumentType docType = (DocumentType) node; String publicId = docType.getPublicId(); String systemId = docType.getSystemId(); String internalSubset = docType.getInternalSubset(); writer.write("<!DOCTYPE " + docType.getName()); if (publicId != null) writer.write(" PUBLIC \"" + publicId + "\" "); else writer.write(" SYSTEM "); writer.write("\"" + systemId + "\""); if (internalSubset != null) writer.write(" [" + internalSubset + "]"); writer.write(">\n"); break; } writer.flush(); }