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:bridge.toolkit.commands.S1000DConverter.java

/**
 * Iterate through the DOM tree// w w w .  ja  v  a  2s. c  o m
 * 
 * @param node
 * @param output
 * @throws IOException
 */
public static void writeNode(Node node, Writer output) throws IOException {

    int type = node.getNodeType();

    switch (type) {
    case Node.ATTRIBUTE_NODE:
        output.write(' ');
        output.write(node.getNodeName());
        output.write("=\"");
        writeXMLData(node.getNodeValue(), true, output);
        output.write('"');
        break;
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        writeXMLData(node.getNodeValue(), false, output);
        break;
    case Node.COMMENT_NODE:
        output.write("<!--");
        output.write(((Comment) node).getNodeValue());
        output.write("-->");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ELEMENT_NODE: {
        NamedNodeMap atts = node.getAttributes();

        output.write('<');
        output.write(node.getNodeName());
        if (atts != null) {
            int length = atts.getLength();
            for (int i = 0; i < length; i++)
                writeNode(atts.item(i), output);
        }

        if (node.hasChildNodes()) {
            output.write('>');
            writeNodes(node.getChildNodes(), output);
            output.write("</");
            output.write(node.getNodeName());
            output.write('>');
        } else {
            output.write("/>");
        }
        break;
    }
    case Node.ENTITY_NODE:
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeNodes(node.getChildNodes(), output);
        break;
    case Node.NOTATION_NODE:
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;
    default:
        throw new Error("Unexpected DOM node type: " + type);
    }
}

From source file:com.draagon.meta.loader.xml.XMLFileMetaDataLoader.java

/** 
 * Parse the MetaAttribute Value /*  ww w . j  ava2s.  co m*/
 */
protected void parseMetaAttributeValue(MetaAttribute attr, Element el) {

    ///////////////////////////////////////////////////
    // Get the Node value

    // Get the first node
    Node nv = el.getFirstChild();

    // Loop through and ignore the comments
    while (nv != null && nv.getNodeType() == Node.COMMENT_NODE) {
        nv.getNextSibling();
    }

    // If a valid node exists, then get the data
    if (nv != null) {
        switch (nv.getNodeType()) {
        // If CDATA just set the whole thing
        case Node.CDATA_SECTION_NODE:
            attr.setValueAsString(((CharacterData) nv).getData());
            break;

        // If an Element just pass it in for parsing
        case Node.ELEMENT_NODE:
            attr.setValue(nv);
            break;

        // If just text, then pass it in
        case Node.TEXT_NODE:
            attr.setValueAsString(nv.getNodeValue());
            break;

        default:
            log.warn("Unsupported Node Type for node [" + nv + "]");
        }
    }
}

From source file:DomUtils.java

/**
 * Construct the XPath of the supplied DOM Node.
 * <p/>//  w  w w.j a  v a 2s . c  o m
 * Supports element, comment and cdata sections DOM Node types.
 * @param node DOM node for XPath generation.
 * @return XPath string representation of the supplied DOM Node.
 */
public static String getXPath(Node node) {

    StringBuffer xpath = new StringBuffer();
    Node parent = node.getParentNode();

    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        xpath.append(getXPathToken((Element) node));
        break;
    case Node.COMMENT_NODE:
        int commentNum = DomUtils.countNodesBefore(node, Node.COMMENT_NODE);
        xpath.append("/{COMMENT}[" + commentNum + 1 + "]");
        break;
    case Node.CDATA_SECTION_NODE:
        int cdataNum = DomUtils.countNodesBefore(node, Node.CDATA_SECTION_NODE);
        xpath.append("/{CDATA}[" + cdataNum + 1 + "]");
        break;
    default:
        throw new UnsupportedOperationException(
                "XPath generation for supplied DOM Node type not supported.  Only supports element, comment and cdata section DOM nodes.");
    }

    while (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
        xpath.insert(0, getXPathToken((Element) parent));
        parent = parent.getParentNode();
    }

    return xpath.toString();
}

From source file:DOMProcessor.java

/** Converts the given DOM node into XML. Recursively converts
  * any child nodes. This version allows the XML version, encoding and stand-alone
  * status to be set./*  w w w . ja v  a2  s  .  co  m*/
  * @param node DOM Node to display.
  * @param version XML version, or null if default ('1.0') is to be used.
  * @param encoding XML encoding, or null if encoding is not to be specified.
  * @param standalone XML stand-alone status or null if not to be specified.
  */
private void outputNodeAsXML(Node node, String version, String encoding, Boolean standalone) {
    // Store node name, type and value.
    String name = node.getNodeName(), value = makeFriendly(node.getNodeValue());
    int type = node.getNodeType();

    // Ignore empty nodes (e.g. blank lines etc.)
    if ((value != null) && (value.trim().equals(""))) {
        return;
    }

    switch (type) {
    case Node.DOCUMENT_NODE: // Start of document.
    {
        if (version == null) {
            out.print("<?xml version=\"1.0\" ");
        } else {
            out.print("<?xml version=\"" + version + "\" ");
        }

        if (encoding != null) {
            out.print("encoding=\"" + encoding + "\" ");
        }

        if (standalone != null) {
            if (standalone.booleanValue()) {
                out.print("standalone=\"yes\" ");
            } else {
                out.print("standalone=\"no\" ");
            }
        }

        out.println("?>");

        // Output the document's child nodes.
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            outputNodeAsXML(children.item(i));
        }
        break;
    }

    case Node.ELEMENT_NODE: // Document element with attributes.
    {
        // Output opening element tag.
        indent++;
        indent();
        out.print("<" + name);

        // Output any attributes the element might have.
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            out.print(" " + attribute.getNodeName() + "=\"" + attribute.getNodeValue() + "\"");
        }
        out.print(">");

        // Output any child nodes that exist.                    
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            outputNodeAsXML(children.item(i));
        }

        break;
    }

    case Node.CDATA_SECTION_NODE: // Display text.
    case Node.TEXT_NODE: {
        out.print(value);
        break;
    }

    case Node.COMMENT_NODE: // Comment node.
    {
        indent++;
        indent();
        out.print("<!--" + value + "-->");
        indent--;
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: // Entity reference nodes.
    {
        indent++;
        indent();
        out.print("&" + name + ";");
        indent--;
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: // Processing instruction.
    {
        indent++;
        indent();
        out.print("<?" + name);
        if ((value != null) && (value.length() > 0)) {
            out.print(" " + value);
        }
        out.println("?>");
        indent--;
        break;
    }
    }

    // Finally output closing tags for each element.
    if (type == Node.ELEMENT_NODE) {
        out.print("</" + node.getNodeName() + ">");
        indent--;
        if (node.getNextSibling() == null) {
            indent(); // Only throw new line if this is the last sibling.
        }
    }
}

From source file:com.connexta.arbitro.ctx.xacml3.XACML3EvaluationCtx.java

private Set<String> getChildXPaths(Node root, String xPath) {

    Set<String> xPaths = new HashSet<String>();
    NamespaceContext namespaceContext = null;

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    if (namespaceContext == null) {

        //see if the request root is in a namespace
        String namespace = null;// w ww.  j  a v a 2 s.  com
        if (root != null) {
            namespace = root.getNamespaceURI();
        }
        // name spaces are used, so we need to lookup the correct
        // prefix to use in the search string
        NamedNodeMap namedNodeMap = root.getAttributes();

        Map<String, String> nsMap = new HashMap<String, String>();
        if (namedNodeMap != null) {
            for (int i = 0; i < namedNodeMap.getLength(); i++) {
                Node n = namedNodeMap.item(i);
                // we found the matching namespace, so get the prefix
                // and then break out
                String prefix = DOMHelper.getLocalName(n);
                String nodeValue = n.getNodeValue();
                nsMap.put(prefix, nodeValue);
            }
        }

        // if there is not any namespace is defined for content element, default XACML request
        //  name space would be there.
        if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(namespace)
                || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(namespace)
                || XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(namespace)) {
            nsMap.put("xacml", namespace);
        }

        namespaceContext = new DefaultNamespaceContext(nsMap);
    }

    xpath.setNamespaceContext(namespaceContext);

    try {
        XPathExpression expression = xpath.compile(xPath);
        NodeList matches = (NodeList) expression.evaluate(root, XPathConstants.NODESET);
        if (matches != null && matches.getLength() > 0) {

            for (int i = 0; i < matches.getLength(); i++) {
                String text = null;
                Node node = matches.item(i);
                short nodeType = node.getNodeType();

                // see if this is straight text, or a node with data under
                // it and then get the values accordingly
                if ((nodeType == Node.CDATA_SECTION_NODE) || (nodeType == Node.COMMENT_NODE)
                        || (nodeType == Node.TEXT_NODE) || (nodeType == Node.ATTRIBUTE_NODE)) {
                    // there is no child to this node
                    text = node.getNodeValue();
                } else {

                    // the data is in a child node
                    text = "/" + DOMHelper.getLocalName(node);
                }
                String newXPath = '(' + xPath + ")[" + (i + 1) + ']';
                xPaths.add(newXPath);
            }
        }
    } catch (Exception e) {
        // TODO
    }

    return xPaths;
}

From source file:jef.tools.XMLUtils.java

/**
 * Element(??)//from w  ww.  jav a 2 s  .  c  o  m
 * 
 * @param node
 *            
 * @param tagName
 *            ????nullElement
 * @return ??
 */
public static List<Element> childElements(Node node, String... tagName) {
    if (node == null)
        throw new NullPointerException("the input node can not be null!");
    List<Element> list = new ArrayList<Element>();
    NodeList nds = node.getChildNodes();
    if (tagName.length == 0 || tagName[0] == null) {// ?API
        tagName = null;
    }
    for (int i = 0; i < nds.getLength(); i++) {
        Node child = nds.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) child;
            if (tagName == null || ArrayUtils.contains(tagName, e.getNodeName())) {
                list.add(e);
            }
        } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
        } else if (child.getNodeType() == Node.COMMENT_NODE) {
        } else if (child.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {

        } else if (child.getNodeType() == Node.DOCUMENT_NODE) {

        } else if (child.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        } else if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
        } else if (child.getNodeType() == Node.TEXT_NODE) {
        }
    }
    return list;
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

/**
 * Remove comments and whitespace-only text nodes
 *///  w  ww. j  av  a 2  s. c om
private static List<Node> canonizeNodeList(NodeList nodelist) {
    List<Node> list = new ArrayList<Node>(nodelist.getLength());
    for (int i = 0; i < nodelist.getLength(); i++) {
        Node aItem = nodelist.item(i);
        if (aItem.getNodeType() == Node.COMMENT_NODE) {
            continue;
        } else if (aItem.getNodeType() == Node.TEXT_NODE) {
            if (aItem.getTextContent().matches("\\s*")) {
                continue;
            }
        }
        list.add(aItem);
    }
    return list;
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static boolean isJunk(Node node) {
    if (node.getNodeType() == Node.COMMENT_NODE) {
        return true;
    }//from  www  .j  a v a2s  .  com
    if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return true;
    }
    if (node.getNodeType() == Node.TEXT_NODE) {
        Text text = (Text) node;
        if (text.getTextContent().matches("^\\s*$")) {
            return true;
        }
        return false;
    }
    return false;
}

From source file:com.twinsoft.convertigo.engine.util.WsReference.java

private static void extractSoapVariables(XmlSchema xmlSchema, List<RequestableHttpVariable> variables,
        Node node, String longName, boolean isMulti, QName variableType) throws EngineException {
    if (node == null)
        return;//from w ww .  j  a  va 2s.  c o  m
    int type = node.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (element != null) {
            String elementName = element.getLocalName();
            if (longName != null)
                elementName = longName + "_" + elementName;

            if (!element.getAttribute("soapenc:arrayType").equals("") && !element.hasChildNodes()) {
                String avalue = element.getAttribute("soapenc:arrayType");
                element.setAttribute("soapenc:arrayType", avalue.replaceAll("\\[\\]", "[1]"));

                Element child = element.getOwnerDocument().createElement("item");
                String atype = avalue.replaceAll("\\[\\]", "");
                child.setAttribute("xsi:type", atype);
                if (atype.startsWith("xsd:")) {
                    String variableName = elementName + "_item";
                    child.appendChild(
                            element.getOwnerDocument().createTextNode("$(" + variableName.toUpperCase() + ")"));
                    RequestableHttpVariable httpVariable = createHttpVariable(true, variableName,
                            new QName(Constants.URI_2001_SCHEMA_XSD, atype.split(":")[1]));
                    variables.add(httpVariable);
                }
                element.appendChild(child);
            }

            // extract from attributes
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node child = map.item(i);
                if (child.getNodeName().equals("soapenc:arrayType"))
                    continue;
                if (child.getNodeName().equals("xsi:type"))
                    continue;
                if (child.getNodeName().equals("soapenv:encodingStyle"))
                    continue;

                String variableName = getVariableName(variables, elementName + "_" + child.getLocalName());

                child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                RequestableHttpVariable httpVariable = createHttpVariable(false, variableName,
                        Constants.XSD_STRING);
                variables.add(httpVariable);
            }

            // extract from children nodes
            boolean multi = false;
            QName qname = Constants.XSD_STRING;
            NodeList children = element.getChildNodes();
            if (children.getLength() > 0) {
                Node child = element.getFirstChild();
                while (child != null) {
                    if (child.getNodeType() == Node.COMMENT_NODE) {
                        String value = child.getNodeValue();
                        if (value.startsWith("type:")) {
                            String schemaType = child.getNodeValue().substring("type:".length()).trim();
                            qname = getVariableSchemaType(xmlSchema, schemaType);
                        }
                        if (value.indexOf("repetitions:") != -1) {
                            multi = true;
                        }
                    } else if (child.getNodeType() == Node.TEXT_NODE) {
                        String value = child.getNodeValue().trim();
                        if (value.equals("?") || !value.equals("")) {
                            String variableName = getVariableName(variables, elementName);

                            child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                            RequestableHttpVariable httpVariable = createHttpVariable(isMulti, variableName,
                                    variableType);
                            variables.add(httpVariable);
                        }
                    } else if (child.getNodeType() == Node.ELEMENT_NODE) {
                        extractSoapVariables(xmlSchema, variables, child, elementName, multi, qname);
                        multi = false;
                        qname = Constants.XSD_STRING;
                    }

                    child = child.getNextSibling();
                }
            }

        }
    }
}

From source file:net.sourceforge.pmd.lang.xml.ast.DOMLineNumbers.java

private int determineLocation(Node n, int index) {
    int nextIndex = index;
    if (n.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        nextIndex = xmlString.indexOf("<!DOCTYPE", nextIndex);
    } else if (n.getNodeType() == Node.COMMENT_NODE) {
        nextIndex = xmlString.indexOf("<!--", nextIndex);
    } else if (n.getNodeType() == Node.ELEMENT_NODE) {
        nextIndex = xmlString.indexOf("<" + n.getNodeName(), nextIndex);
    } else if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
        nextIndex = xmlString.indexOf("<![CDATA[", nextIndex);
    } else if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        ProcessingInstruction pi = (ProcessingInstruction) n;
        nextIndex = xmlString.indexOf("<?" + pi.getTarget(), nextIndex);
    } else if (n.getNodeType() == Node.TEXT_NODE) {
        String te = unexpandEntities(n, n.getNodeValue());
        int newIndex = xmlString.indexOf(te, nextIndex);
        if (newIndex > 0) {
            nextIndex = newIndex;//from  w  w w.  j  av a 2  s  .c  om
        }
    } else if (n.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
        nextIndex = xmlString.indexOf("&" + n.getNodeName() + ";", nextIndex);
    }
    setBeginLocation(n, nextIndex);
    if (n.hasChildNodes()) {
        NodeList childs = n.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            nextIndex = determineLocation(childs.item(i), nextIndex);
        }
    }
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        nextIndex += 2 + n.getNodeName().length() + 1; // </nodename>
    } else if (n.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        Node nextSibling = n.getNextSibling();
        if (nextSibling.getNodeType() == Node.ELEMENT_NODE) {
            nextIndex = xmlString.indexOf("<" + nextSibling.getNodeName(), nextIndex) - 1;
        } else if (nextSibling.getNodeType() == Node.COMMENT_NODE) {
            nextIndex = xmlString.indexOf("<!--", nextIndex);
        } else {
            nextIndex = xmlString.indexOf(">", nextIndex);
        }
    } else if (n.getNodeType() == Node.COMMENT_NODE) {
        nextIndex += 4 + 3; // <!-- and -->
        nextIndex += n.getNodeValue().length();
    } else if (n.getNodeType() == Node.TEXT_NODE) {
        String te = unexpandEntities(n, n.getNodeValue());
        nextIndex += te.length();
    } else if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
        nextIndex += "<![CDATA[".length() + n.getNodeValue().length() + "]]>".length();
    } else if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        ProcessingInstruction pi = (ProcessingInstruction) n;
        nextIndex += "<?".length() + pi.getTarget().length() + "?>".length() + pi.getData().length();
    }
    setEndLocation(n, nextIndex - 1);
    return nextIndex;
}