Example usage for org.w3c.dom Node COMMENT_NODE

List of usage examples for org.w3c.dom Node COMMENT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node COMMENT_NODE.

Prototype

short COMMENT_NODE

To view the source code for org.w3c.dom Node COMMENT_NODE.

Click Source Link

Document

The node is a Comment.

Usage

From source file:Main.java

/**
 * Get the content of the given element.
 *
 * @param element       The element to get the content for.
 * @param defaultStr    The default to return when there is no content.
 * @return              The content of the element or the default.
 */// w ww  .jav  a2 s.c o m
public static String getElementContent(Element element, String defaultStr) throws Exception {
    if (element == null)
        return defaultStr;

    NodeList children = element.getChildNodes();
    String result = "";
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.TEXT_NODE
                || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
            result += children.item(i).getNodeValue();
        } else if (children.item(i).getNodeType() == Node.COMMENT_NODE) {
            // Ignore comment nodes
        }
    }
    return result.trim();
}

From source file:Main.java

@SuppressWarnings("fallthrough")
static final void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;//  ww  w  .j  av  a2s  . c o  m
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = ((Element) rootNode).getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working - ignore fallthrough warning
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null)
                    return;
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
    return;
}

From source file:Main.java

static final void getSetRec(final Node rootNode, final Set result, final Node exclude, final boolean com) {
    //Set result = new HashSet();
    if (rootNode == exclude) {
        return;/*from   w w  w. j  av  a  2 s.  co m*/
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = ((Element) rootNode).getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null)
                    return;
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
    return;
}

From source file:MainClass.java

private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;/*from   w ww. j  a  va2s  .c o  m*/
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.println(indent + "TEXT_NODE");
        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

public static void assertEquivalent(Node node, Node node2) {
    if (node == null) {
        throw new IllegalArgumentException("the first node to be compared is null");
    }/*from www .  j  a va 2 s.c o  m*/

    if (node2 == null) {
        throw new IllegalArgumentException("the second node to be compared is null");
    }

    if (!node.getNodeName().equals(node2.getNodeName())) {
        throw new IllegalArgumentException("nodes have different node names");
    }

    int attrCount = 0;
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
        attrCount = attrs.getLength();
    }

    int attrCount2 = 0;
    NamedNodeMap attrs2 = node2.getAttributes();
    if (attrs2 != null) {
        attrCount2 = attrs2.getLength();
    }

    if (attrCount != attrCount2) {
        throw new IllegalArgumentException("nodes hava a different number of attributes");
    }

    outer: for (int i = 0; i < attrCount; i++) {
        Node n = attrs.item(i);
        String name = n.getNodeName();
        String value = n.getNodeValue();

        for (int j = 0; j < attrCount; j++) {
            Node n2 = attrs2.item(j);
            String name2 = n2.getNodeName();
            String value2 = n2.getNodeValue();

            if (name.equals(name2) && value.equals(value2)) {
                continue outer;
            }
        }
        throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match");
    }

    boolean hasChildren = node.hasChildNodes();

    if (hasChildren != node2.hasChildNodes()) {
        throw new IllegalArgumentException("one node has children and the other doesn't");
    }

    if (hasChildren) {
        NodeList nl = node.getChildNodes();
        NodeList nl2 = node2.getChildNodes();

        short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE };
        List nodes = filter(nl, toFilter);
        List nodes2 = filter(nl2, toFilter);

        int length = nodes.size();

        if (length != nodes2.size()) {
            throw new IllegalArgumentException("nodes hava a different number of children");
        }

        for (int i = 0; i < length; i++) {
            Node n = (Node) nodes.get(i);
            Node n2 = (Node) nodes2.get(i);
            assertEquivalent(n, n2);
        }
    }
}

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;/*w ww  .  j  ava2 s . co  m*/
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:DOMDump.java

private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;/*from   w  w  w .j  a  va  2s . co m*/
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.println(indent + "TEXT_NODE");
        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:fr.gouv.finances.dgfip.xemelios.utils.XmlUtils.java

public static String getXmlDataSubstituteNode(Node node, String substituteWith, String substituteInWhat) {
    StringBuilder sb = new StringBuilder();
    switch (node.getNodeType()) {
    case Node.COMMENT_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.NOTATION_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;/*from  w  w  w.j  a v a2 s .  c o  m*/
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.ELEMENT_NODE: {
        String nodeName = node.getNodeName();
        if (!substituteInWhat.equals(nodeName)) {
            sb.append("<").append(nodeName);
            StringBuilder attrs = new StringBuilder();
            StringBuilder children = new StringBuilder();
            NamedNodeMap nnm = node.getAttributes();
            if (nnm != null) {
                for (int i = 0; i < nnm.getLength(); i++) {
                    Node attr = nnm.item(i);
                    attrs.append(" ").append(getXmlDataSubstituteNode(attr, substituteWith, substituteInWhat));
                }
            }
            NodeList nl = node.getChildNodes();
            if (nl != null) {
                for (int i = 0; i < nl.getLength(); i++) {
                    Node child = nl.item(i);
                    if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
                        attrs.append(" ")
                                .append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat));
                    } else {
                        children.append(getXmlDataSubstituteNode(child, substituteWith, substituteInWhat));
                    }
                }
            }
            sb.append(attrs.toString());
            if (children.length() > 0) {
                sb.append(">").append(children.toString()).append("</").append(nodeName).append(">");
            } else {
                sb.append("/>");
            }
        } else {
            sb.append(substituteWith);
        }
        break;
    }
    case Node.ATTRIBUTE_NODE: {
        sb.append(node.getNodeName()).append("=\"").append(StringEscapeUtils.escapeXml(node.getNodeValue()))
                .append("\"");
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        sb.append("<![CDATA[").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("]]>");
    }
    case Node.TEXT_NODE: {
        sb.append(StringEscapeUtils.escapeXml(node.getNodeValue()));
    }
    }
    return sb.toString();
}

From source file:DOMEdit.java

private static void outputloop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        outputElement((Element) node, indent);
        break;//from w  ww.  j  a v a 2s.  c  o  m
    case Node.TEXT_NODE:
        outputText((Text) node, indent);
        break;
    case Node.CDATA_SECTION_NODE:
        outputCDATASection((CDATASection) node, indent);
        break;
    case Node.COMMENT_NODE:
        outputComment((Comment) node, indent);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        outputProcessingInstructionNode((ProcessingInstruction) node, indent);
        break;
    default:
        System.out.println("Unknown node type: " + node.getNodeType());
        break;
    }
}

From source file:Main.java

public final static Class<? extends Node> toClass(final short nodeType) {
    switch (nodeType) {
    case Node.ATTRIBUTE_NODE:
        return Attr.class;
    case Node.CDATA_SECTION_NODE:
        return CDATASection.class;
    case Node.COMMENT_NODE:
        return Comment.class;
    case Node.DOCUMENT_FRAGMENT_NODE:
        return DocumentFragment.class;
    case Node.DOCUMENT_NODE:
        return Document.class;
    case Node.DOCUMENT_TYPE_NODE:
        return DocumentType.class;
    case Node.ELEMENT_NODE:
        return Element.class;
    case Node.ENTITY_NODE:
        return Entity.class;
    case Node.ENTITY_REFERENCE_NODE:
        return EntityReference.class;
    case Node.NOTATION_NODE:
        return Notation.class;
    case Node.PROCESSING_INSTRUCTION_NODE:
        return ProcessingInstruction.class;
    case Node.TEXT_NODE:
        return Text.class;
    }//w w  w  . j  a  v a 2  s.c o  m
    throw new RuntimeException("Unrecognized node type " + nodeType);
}