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:org.pentaho.reporting.engine.classic.extensions.datasources.xpath.XPathTableModel.java

private boolean processNode(final Node node, final LinkedHashMap<String, String> results,
        final TypedTableModel typedTableModel) throws ReportDataFactoryException {
    final LinkedHashMap<String, String> innerResults = new LinkedHashMap<String, String>(results);

    boolean isLeaf = true;
    //    System.out.println("<" + node.getQName() + ">");
    final NodeList childList = node.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        final Node nodeIf = childList.item(i);
        final short type = nodeIf.getNodeType();
        if (type == Node.COMMENT_NODE) {
            continue;
        }//from  ww w  .  j  a  v  a2s. c o m

        if (type == Node.ELEMENT_NODE) {
            final NodeList anIf = nodeIf.getChildNodes();
            final int size = anIf.getLength();
            // check if either a empty node or a
            if (size == 0) {
                // a empty node ...
                innerResults.put(nodeIf.getNodeName(), null);
            } else if (size == 1) {
                final Node subNode = anIf.item(0);
                if (subNode.getNodeType() == Node.TEXT_NODE
                        || subNode.getNodeType() == Node.CDATA_SECTION_NODE) {
                    // a single text node ..
                    innerResults.put(nodeIf.getNodeName(), nodeIf.getTextContent());
                } else if (subNode.getNodeType() == Node.ELEMENT_NODE) {
                    isLeaf = false;
                } else {
                    innerResults.put(nodeIf.getNodeName(), nodeIf.getTextContent());
                }
            } else {
                isLeaf = false;
            }
        } else {
            final String content = nodeIf.getTextContent();
            if (StringUtils.isEmpty(content, true) == false) {
                innerResults.put(nodeIf.getNodeName(), content);
            }
        }
    }

    if (isLeaf == false) {
        for (int i = 0; i < childList.getLength(); i++) {
            final Node deepNode = childList.item(i);
            if (deepNode.getNodeType() == Node.ELEMENT_NODE) {
                final NodeList childNodes = deepNode.getChildNodes();
                if (childNodes.getLength() > 1 || (childNodes.getLength() == 1
                        && childNodes.item(0).getNodeType() == Node.ELEMENT_NODE)) {
                    if (processNode(deepNode, innerResults, typedTableModel) == false) {
                        return false;
                    }
                }
            }
        }
        return true;
    } else {
        return addRow(innerResults, typedTableModel);
    }
}

From source file:org.sonar.plugins.xml.checks.IndentCheck.java

/**
 * Collect the indenting whitespace before this node.
 *///from  w  w w.  ja  va  2 s  .  c  o  m
private int collectIndent(Node node) {
    int indent = 0;
    for (Node sibling = node.getPreviousSibling(); sibling != null; sibling = sibling.getPreviousSibling()) {
        switch (sibling.getNodeType()) {
        case Node.COMMENT_NODE:
        case Node.ELEMENT_NODE:
            return indent;
        case Node.TEXT_NODE:
            String text = sibling.getTextContent();
            if (StringUtils.isWhitespace(text)) {
                for (int i = text.length() - 1; i >= 0; i--) {
                    char c = text.charAt(i);
                    switch (c) {
                    case '\n':
                        // newline found, we are done
                        return indent;
                    case '\t':
                        // add tabsize
                        indent += tabSize;
                        break;
                    case ' ':
                        // add one space
                        indent++;
                        break;
                    default:
                        break;
                    }
                }
            } else {
                // non whitespace found, we are done
                return indent;
            }
            break;
        default:
            break;
        }
    }
    return indent;
}

From source file:org.sonar.plugins.xml.checks.IndentCheck.java

/**
 * Validate the indent for this node.//  w  w  w .  j  a  v  a 2 s .c om
 */
private void validateIndent(Node node) {

    int depth = getDepth(node);
    int indent = collectIndent(node);

    if (depth * indentSize != indent) {
        createViolation(SaxParser.getLineNumber(node), "Wrong indentation");
    }

    // check the child elements
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
        case Node.COMMENT_NODE:
            validateIndent(child);
            break;
        default:
            break;
        }
    }
}

From source file:org.sonar.plugins.xml.checks.NewlineCheck.java

/**
 * Validate newlines for node./* w w  w  .j av a2  s. c  o  m*/
 */
private void validateNewline(Node node) {

    // check if we have a newline after the elements and after each childelement.
    boolean newline = false;
    Node lastChild = null;

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        switch (child.getNodeType()) {
        case Node.COMMENT_NODE:
        case Node.ELEMENT_NODE:
            // check if there is a new node before we have had any newlines.
            if (!newline) {
                createViolation(SaxParser.getLineNumber(child), "Node should be on the next line");
            } else {
                newline = false;
            }
            lastChild = child;
            break;
        case Node.TEXT_NODE:
            // newline check is OK if there is non whitespace or the whitespace contains a newline 
            if (!StringUtils.isWhitespace(child.getTextContent()) || child.getTextContent().contains("\n")) {
                newline = true;
            }
            break;
        default:
            break;
        }
    }

    // validate first last child.
    validateLastChild(newline, lastChild);

    // check the child elements
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            validateNewline(child);
        }
    }
}

From source file:org.tizzit.util.XercesHelper.java

public static String nodeList2string(NodeList nl) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nde = nl.item(i);// ww w  .  j av  a 2 s  .  c  om
        String attributes = "";
        if (nde.getNodeType() == Node.TEXT_NODE) {
            sb.append(getHexEncoded(nde.getNodeValue()));
            if (nde.hasChildNodes()) {
                sb.append(nodeList2string(nde.getChildNodes()));
            }
        } else if (nde.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append("<![CDATA[" + nde.getNodeValue() + "]]>");
        } else if (nde.getNodeType() == Node.COMMENT_NODE) {
            sb.append("<!-- -->");
        } else {
            if (nde.hasAttributes()) {
                NamedNodeMap attr = nde.getAttributes();
                for (int j = 0; j < attr.getLength(); j++) {
                    attributes += " " + attr.item(j).getNodeName() + "=\""
                            + getHexEncoded(attr.item(j).getNodeValue()) + "\"";
                }
            }
            sb.append("<" + nde.getNodeName() + attributes);
            if (nde.hasChildNodes()) {
                sb.append(">" + nodeList2string(nde.getChildNodes()) + "</" + nde.getNodeName() + ">");
            } else {
                sb.append("/>");
            }
        }
    }
    return sb.toString();
}

From source file:org.wso2.carbon.apimgt.migration.client._110Specific.ResourceModifierTest.java

private boolean compareXMLDocuments(Document expected, Document actual) {
    Element expectedDocElement = expected.getDocumentElement();
    Element actualDocElement = actual.getDocumentElement();

    if (!expectedDocElement.getTagName().equals(actualDocElement.getTagName())) {
        return false;
    }/*from  w w  w  .java  2s .  c  o m*/

    NodeList expectedNodes = expectedDocElement.getChildNodes();
    NodeList actualNodes = actualDocElement.getChildNodes();

    if (expectedNodes.getLength() != actualNodes.getLength()) {
        return false;
    }

    for (int i = 0; i < expectedNodes.getLength(); ++i) {
        Node expectedNode = expectedNodes.item(i);
        Node actualNode = actualNodes.item(i);

        if (expectedNode.getNodeType() != actualNode.getNodeType()) {
            return false;
        }

        if (expectedNode.getNodeType() == Node.ELEMENT_NODE) {
            if (!((Element) expectedNode).getTagName().equals(((Element) actualNode).getTagName())) {
                return false;
            }
        } else if (expectedNode.getNodeType() == Node.COMMENT_NODE) {
            String expectedText = expectedNode.getTextContent();
            expectedText = expectedText.replaceAll("\\s", "");
            String actualText = actualNode.getTextContent();
            actualText = actualText.replaceAll("\\s", "");

            if (!expectedText.equals(actualText)) {
                return false;
            }
        }

    }

    return true;
}

From source file:test.unit.be.fedict.eid.applet.service.signer.AbstractXmlSignatureServiceTest.java

private boolean isDigested(NodeList nodes, VerifyReference[] references) {
    for (int idx = 0; idx < nodes.getLength(); idx++) {
        Node node = nodes.item(idx);
        LOG.debug("node name: " + node.getLocalName());
        boolean changed = false;
        if (node.getNodeType() == Node.TEXT_NODE) {
            String originalTextValue = node.getNodeValue();
            String changedTextValue = originalTextValue + "foobar";
            node.setNodeValue(changedTextValue);
            changed = false; // need to have impact anyway
            for (int referenceIdx = 0; referenceIdx < references.length; referenceIdx++) {
                VerifyReference reference = references[referenceIdx];
                changed |= reference.hasChanged();
            }/*from  w  ww  .j  a va  2s. com*/
            if (false == changed) {
                return false;
            }
            node.setNodeValue(originalTextValue);
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;

            NamedNodeMap attributes = element.getAttributes();
            for (int attributeIdx = 0; attributeIdx < attributes.getLength(); attributeIdx++) {
                Node attributeNode = attributes.item(attributeIdx);
                String originalAttributeValue = attributeNode.getNodeValue();
                String changedAttributeValue = originalAttributeValue + "foobar";
                attributeNode.setNodeValue(changedAttributeValue);
                for (int referenceIdx = 0; referenceIdx < references.length; referenceIdx++) {
                    VerifyReference reference = references[referenceIdx];
                    changed |= reference.hasChanged();
                }

                attributeNode.setNodeValue(originalAttributeValue);
            }
            changed |= isDigested(element.getChildNodes(), references);
        } else if (node.getNodeType() == Node.COMMENT_NODE) {
            // not always digested
        } else {
            throw new RuntimeException("unsupported node type: " + node.getNodeType());
        }
        if (false == changed) {
            return false;
        }
    }
    return true;
}

From source file:tufts.vue.ds.XMLIngest.java

private static final String getNodeType(int t) {
    if (t == Node.ATTRIBUTE_NODE)
        return "attr";
    if (t == Node.CDATA_SECTION_NODE)
        return "cdata";
    if (t == Node.COMMENT_NODE)
        return "comment";
    if (t == Node.DOCUMENT_NODE)
        return "document";
    if (t == Node.ELEMENT_NODE)
        return "element";
    if (t == Node.ENTITY_NODE)
        return "entity";
    if (t == Node.TEXT_NODE)
        return "text";
    return "" + t;

}