List of usage examples for org.w3c.dom Comment getNodeValue
public String getNodeValue() throws DOMException;
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*from w w w . j a v a 2s . co m*/ factory.setExpandEntityReferences(false); Document doc = factory.newDocumentBuilder().parse(new File("filename")); Comment comment = doc.createComment("invalid -- comment"); boolean validComment = comment.getNodeValue().indexOf("--") < 0; }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * // www. j av a 2 s . c om * @param out The <CODE>PrintStream</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(Node) * @see #dump(PrintStream, Node) * @since TFP 1.0 */ private static void doDump(PrintStream out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*from w w w. ja va 2s . c o m*/ * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:mondrian.test.DiffRepository.java
private static void writeNode(Node node, XMLOutput out) { final NodeList childNodes; switch (node.getNodeType()) { case Node.DOCUMENT_NODE: out.print("<?xml version=\"1.0\" ?>" + Util.nl); childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); writeNode(child, out);//from w ww.ja v a2 s .co m } // writeNode(((Document) node).getDocumentElement(), out); break; case Node.ELEMENT_NODE: Element element = (Element) node; final String tagName = element.getTagName(); out.beginBeginTag(tagName); // Attributes. final NamedNodeMap attributeMap = element.getAttributes(); for (int i = 0; i < attributeMap.getLength(); i++) { final Node att = attributeMap.item(i); out.attribute(att.getNodeName(), att.getNodeValue()); } out.endBeginTag(tagName); // Write child nodes, ignoring attributes but including text. childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ATTRIBUTE_NODE) { continue; } writeNode(child, out); } out.endTag(tagName); break; case Node.ATTRIBUTE_NODE: out.attribute(node.getNodeName(), node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: CDATASection cdata = (CDATASection) node; out.cdata(cdata.getNodeValue(), true); break; case Node.TEXT_NODE: Text text = (Text) node; final String wholeText = text.getNodeValue(); if (!isWhitespace(wholeText)) { out.cdata(wholeText, false); } break; case Node.COMMENT_NODE: Comment comment = (Comment) node; out.print("<!--" + comment.getNodeValue() + "-->" + Util.nl); break; default: throw new RuntimeException("unexpected node type: " + node.getNodeType() + " (" + node + ")"); } }
From source file:com.marklogic.xcc.ValueFactory.java
/** * A convenience method to construct an {@link XdmComment} value. {@link XdmComment} objects can be * constructed from an XML {@link String}, a W3C DOM {@link Comment} node or an {@link InputStream} * ./*w ww .ja v a 2 s .c o m*/ * * @param value * An instance of {@link String}, {@link Comment} or {@link InputStream}. * @return An instance of {@link XdmComment}. * @throws IllegalArgumentException * If value is not a {@link String}, {@link Comment} or {@link InputStream}. */ public static XdmComment newCommentNode(Object value) { if (value instanceof String) { return new CommentImpl((String) value); } if (value instanceof InputStream) { return new CommentImpl((InputStream) value); } if (value instanceof Comment) { Comment c = (Comment) value; return new CommentImpl(c.getNodeValue()); } throw new IllegalArgumentException( "String, org.w3c.dom.Text or InputStream value required to construct " + ValueType.TEXT); }
From source file:com.wfreitas.camelsoap.SoapClient.java
private Element getClonePoint(Element element) { Comment comment; // Is it this element... comment = getCommentBefore(element); if (comment != null && comment.getNodeValue().endsWith(SOAPUI_CLONE_COMMENT)) { comment.setNodeValue(comment.getNodeValue() + CLONED_POSTFIX); return element; }//from w w w .ja v a 2 s. co m // Is it the first child element of this element... Element firstChildElement = getFirstChildElement(element); if (firstChildElement != null) { comment = getCommentBefore(firstChildElement); if (comment != null && comment.getNodeValue().endsWith(SOAPUI_CLONE_COMMENT)) { comment.setNodeValue(comment.getNodeValue() + CLONED_POSTFIX); return firstChildElement; } } return null; }
From source file:com.wfreitas.camelsoap.SoapClient.java
private void resetClonePoint(Element clonePoint) { Comment comment = getCommentBefore(clonePoint); if (comment == null) { throw new IllegalStateException("Call to reset a 'clonePoint' that doesn't have a comment before it."); }// w w w.j a v a2 s. c o m String commentText = comment.getNodeValue(); if (!commentText.endsWith(CLONED_POSTFIX)) { throw new IllegalStateException( "Call to reset a 'clonePoint' that doesn't have a proper clone comment before it."); } comment.setNodeValue(commentText.substring(0, commentText.length() - CLONED_POSTFIX.length())); }
From source file:org.infoscoop.dao.model.TabLayout.java
public JSONObject getStaticPanelJsonWithComment() throws Exception { DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document widgetsDoc = docBuilder.parse(new InputSource(new StringReader(super.getWidgets()))); Element widgetsEl = widgetsDoc.getDocumentElement(); NodeList panels = widgetsEl.getElementsByTagName("panel"); Element staticPanel = (Element) panels.item(0); if ("StaticPanel".equals(staticPanel.getAttribute("type"))) { NodeList staticWidgetlist = staticPanel.getChildNodes(); int nodeCount = staticWidgetlist.getLength(); Comment c; String nodeStr;/* ww w .java 2s .c om*/ Document commentDoc; Node commentNode; for (int i = 0; i < nodeCount; i++) { Node node = staticWidgetlist.item(i); if (node.getNodeType() == Element.COMMENT_NODE) { c = (Comment) node; nodeStr = "<" + c.getNodeValue().trim() + ">"; commentDoc = docBuilder.parse(new InputSource(new StringReader(nodeStr))); commentDoc.getDocumentElement().setAttribute("disabled", "true"); commentNode = staticPanel.getOwnerDocument().importNode(commentDoc.getDocumentElement(), true); staticPanel.appendChild(commentNode); } } NodeList list = staticPanel.getElementsByTagName("widget"); return getPanelJson(list); } return null; }