Java Code Examples for org.w3c.dom.Node#COMMENT_NODE

The following examples show how to use org.w3c.dom.Node#COMMENT_NODE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DOMDocumentSerializer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Serialize a {@link Document}.
 *
 * @param d the document to serialize.
 */
public final void serialize(Document d) throws IOException {
    reset();
    encodeHeader(false);
    encodeInitialVocabulary();

    final NodeList nl = d.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        final Node n = nl.item(i);
        switch (n.getNodeType()) {
            case Node.ELEMENT_NODE:
                serializeElement(n);
                break;
            case Node.COMMENT_NODE:
                serializeComment(n);
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                serializeProcessingInstruction(n);
                break;
        }
    }
    encodeDocumentTermination();
}
 
Example 2
Source File: XpathNodeTracker.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Call when visiting a node whose xpath location needs tracking
 *
 * <p>Delegates to {@link #visitedAttribute visitedAttribute} for
 * attribute nodes, {@link #visitedNode visitedNode} for elements,
 * texts, CDATA sections, comments or processing instructions and
 * ignores any other type of node.</p>
 *
 * @param node the Node being visited - must not be null.
 */
public void visited(Node node) {
    switch(node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        visitedAttribute(getNodeName(node));
        break;
    case Node.ELEMENT_NODE:
        visitedNode(node, getNodeName(node));
        break;
    case Node.COMMENT_NODE:
        visitedNode(node, XPATH_COMMENT_IDENTIFIER);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitedNode(node, XPATH_PROCESSING_INSTRUCTION_IDENTIFIER);
        break;
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        visitedNode(node, XPATH_CHARACTER_NODE_IDENTIFIER);
        break;
    default:
        // ignore unhandled node types
        break;
    }
}
 
Example 3
Source File: ConfigParseUtil.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static String getText(Node elem, StringBuilder buffer) {
  if (elem.getNodeType() != Node.CDATA_SECTION_NODE) {
    NodeList childs = elem.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
      Node child = childs.item(i);
      short childType = child.getNodeType();
      if (childType != Node.COMMENT_NODE
              && childType != Node.PROCESSING_INSTRUCTION_NODE) {
        getText(child, buffer);
      }
    }
  } else {
    buffer.append(elem.getNodeValue());
  }

  return buffer.toString();
}
 
Example 4
Source File: DefaultParser.java    From tutorials with MIT License 6 votes vote down vote up
private void clean(Node node) {

        NodeList childs = node.getChildNodes();

        for (int n = childs.getLength() - 1; n >= 0; n--) {
            Node child = childs.item(n);
            short nodeType = child.getNodeType();

            if (nodeType == Node.ELEMENT_NODE)
                clean(child);
            else if (nodeType == Node.TEXT_NODE) {
                String trimmedNodeVal = child.getNodeValue().trim();
                if (trimmedNodeVal.length() == 0)
                    node.removeChild(child);
                else
                    child.setNodeValue(trimmedNodeVal);
            } else if (nodeType == Node.COMMENT_NODE)
                node.removeChild(child);
        }
    }
 
Example 5
Source File: ManifestMerger.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private static File insertSourceMarkers(@NonNull Node node, @Nullable File currentFile) {
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node child = node.getChildNodes().item(i);
        short nodeType = child.getNodeType();
        if (nodeType == Node.ELEMENT_NODE
                || nodeType == Node.COMMENT_NODE
                || nodeType == Node.DOCUMENT_NODE
                || nodeType == Node.CDATA_SECTION_NODE) {
            File file = MergerXmlUtils.getFileFor(child);
            if (file != null && !file.equals(currentFile)) {
                i += insertSourceMarker(node, child, file, false);
                currentFile = file;
            }

            currentFile = insertSourceMarkers(child, currentFile);
        }
    }

    Node lastElement = node.getLastChild();
    while (lastElement != null && lastElement.getNodeType() == Node.TEXT_NODE) {
        lastElement = lastElement.getPreviousSibling();
    }
    if (lastElement != null && lastElement.getNodeType() == Node.ELEMENT_NODE) {
        File parentFile = MergerXmlUtils.getFileFor(node);
        File lastFile = MergerXmlUtils.getFileFor(lastElement);
        if (lastFile != null && parentFile != null && !parentFile.equals(lastFile)) {
            insertSourceMarker(node, lastElement, parentFile, true);
            currentFile = parentFile;
        }
    }

    return currentFile;
}
 
Example 6
Source File: DOM2TO.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private String getNodeTypeFromCode(short code) {
    String retval = null;
    switch (code) {
    case Node.ATTRIBUTE_NODE :
        retval = "ATTRIBUTE_NODE"; break;
    case Node.CDATA_SECTION_NODE :
        retval = "CDATA_SECTION_NODE"; break;
    case Node.COMMENT_NODE :
        retval = "COMMENT_NODE"; break;
    case Node.DOCUMENT_FRAGMENT_NODE :
        retval = "DOCUMENT_FRAGMENT_NODE"; break;
    case Node.DOCUMENT_NODE :
        retval = "DOCUMENT_NODE"; break;
    case Node.DOCUMENT_TYPE_NODE :
        retval = "DOCUMENT_TYPE_NODE"; break;
    case Node.ELEMENT_NODE :
        retval = "ELEMENT_NODE"; break;
    case Node.ENTITY_NODE :
        retval = "ENTITY_NODE"; break;
    case Node.ENTITY_REFERENCE_NODE :
        retval = "ENTITY_REFERENCE_NODE"; break;
    case Node.NOTATION_NODE :
        retval = "NOTATION_NODE"; break;
    case Node.PROCESSING_INSTRUCTION_NODE :
        retval = "PROCESSING_INSTRUCTION_NODE"; break;
    case Node.TEXT_NODE:
        retval = "TEXT_NODE"; break;
    }
    return retval;
}
 
Example 7
Source File: FessXpathTransformer.java    From fess with Apache License 2.0 5 votes vote down vote up
protected Node processGoogleOffOn(final Node node, final ValueHolder<Boolean> flag) {
    final NodeList nodeList = node.getChildNodes();
    List<Node> removedNodeList = null;
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.COMMENT_NODE) {
            final String comment = childNode.getNodeValue().trim();
            if (comment.startsWith("googleoff:")) {
                flag.setValue(false);
            } else if (comment.startsWith("googleon:")) {
                flag.setValue(true);
            }
        }

        if (!flag.getValue() && childNode.getNodeType() == Node.TEXT_NODE) {
            if (removedNodeList == null) {
                removedNodeList = new ArrayList<>();
            }
            removedNodeList.add(childNode);
        } else {
            processGoogleOffOn(childNode, flag);
        }
    }

    if (removedNodeList != null) {
        removedNodeList.stream().forEach(n -> node.removeChild(n));
    }

    return node;
}
 
Example 8
Source File: ManifestMerger.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private static File insertSourceMarkers(@NonNull Node node, @Nullable File currentFile) {
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node child = node.getChildNodes().item(i);
        short nodeType = child.getNodeType();
        if (nodeType == Node.ELEMENT_NODE
                || nodeType == Node.COMMENT_NODE
                || nodeType == Node.DOCUMENT_NODE
                || nodeType == Node.CDATA_SECTION_NODE) {
            File file = MergerXmlUtils.getFileFor(child);
            if (file != null && !file.equals(currentFile)) {
                i += insertSourceMarker(node, child, file, false);
                currentFile = file;
            }

            currentFile = insertSourceMarkers(child, currentFile);
        }
    }

    Node lastElement = node.getLastChild();
    while (lastElement != null && lastElement.getNodeType() == Node.TEXT_NODE) {
        lastElement = lastElement.getPreviousSibling();
    }
    if (lastElement != null && lastElement.getNodeType() == Node.ELEMENT_NODE) {
        File parentFile = MergerXmlUtils.getFileFor(node);
        File lastFile = MergerXmlUtils.getFileFor(lastElement);
        if (lastFile != null && parentFile != null && !parentFile.equals(lastFile)) {
            insertSourceMarker(node, lastElement, parentFile, true);
            currentFile = parentFile;
        }
    }

    return currentFile;
}
 
Example 9
Source File: AndroidStringsXmlReader.java    From mojito with Apache License 2.0 4 votes vote down vote up
private static List<AndroidStringsTextUnit> fromDocument(Document document, String pluralNameSeparator) {

        List<AndroidStringsTextUnit> resultList = new ArrayList<>();

        Node lastCommentNode = null;
        for (int i = 0; i < document.getDocumentElement().getChildNodes().getLength(); i++) {
            Node currentNode = document.getDocumentElement().getChildNodes().item(i);
            if (Node.COMMENT_NODE == currentNode.getNodeType()) {
                lastCommentNode = currentNode;
            } else if (Node.ELEMENT_NODE == currentNode.getNodeType()) {
                Element currentElement = (Element) currentNode;

                switch (currentElement.getTagName()) {
                    case SINGULAR_ELEMENT_NAME:
                        resultList.add(createSingular(
                                getAttribute(currentElement, NAME_ATTRIBUTE_NAME),
                                removeEscape(currentElement.getTextContent()),
                                getContent(lastCommentNode),
                                getAttribute(currentElement, ID_ATTRIBUTE_NAME)));
                        break;

                    case PLURAL_ELEMENT_NAME:
                        String comment = getContent(lastCommentNode);
                        String name = getAttribute(currentElement, NAME_ATTRIBUTE_NAME);

                        NodeList nodeList = currentElement.getElementsByTagName(PLURAL_ITEM_ELEMENT_NAME);
                        for (int j = 0; j < nodeList.getLength(); j++) {
                            if (Node.ELEMENT_NODE == nodeList.item(j).getNodeType()) {
                                Element itemElement = (Element) nodeList.item(j);
                                resultList.add(createPlural(name, PluralItem.valueOf(getAttribute(itemElement, QUANTITY_ATTRIBUTE_NAME)),
                                        removeEscape(itemElement.getTextContent()), comment,
                                        getAttribute(itemElement, ID_ATTRIBUTE_NAME), pluralNameSeparator));
                            }
                        }
                        break;
                }
            }
        }

        return resultList;
    }
 
Example 10
Source File: XmlNode.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
final boolean isCommentType() {
    return dom.getNodeType() == Node.COMMENT_NODE;
}
 
Example 11
Source File: XMLUtils.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("fallthrough")
private static void getSetRec(final Node rootNode, final Set<Node> result,
                            final Node exclude, final boolean com) {
    if (rootNode == exclude) {
        return;
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element)rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = el.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);
    }
}
 
Example 12
Source File: DOMValidatorHelper.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/** Do processing for the start of a node. */
private void beginNode(Node node) {
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            fCurrentElement = node;
            // push namespace context
            fNamespaceContext.pushContext();
            // start element
            fillQName(fElementQName, node);
            processAttributes(node.getAttributes());
            fSchemaValidator.startElement(fElementQName, fAttributes, null);
            break;
        case Node.TEXT_NODE:
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.setIgnoringCharacters(true);
                sendCharactersToValidator(node.getNodeValue());
                fDOMValidatorHandler.setIgnoringCharacters(false);
                fDOMValidatorHandler.characters((Text) node);
            }
            else {
                sendCharactersToValidator(node.getNodeValue());
            }
            break;
        case Node.CDATA_SECTION_NODE:
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.setIgnoringCharacters(true);
                fSchemaValidator.startCDATA(null);
                sendCharactersToValidator(node.getNodeValue());
                fSchemaValidator.endCDATA(null);
                fDOMValidatorHandler.setIgnoringCharacters(false);
                fDOMValidatorHandler.cdata((CDATASection) node);
            }
            else {
                fSchemaValidator.startCDATA(null);
                sendCharactersToValidator(node.getNodeValue());
                fSchemaValidator.endCDATA(null);
            }
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            /**
             * The validator does nothing with processing instructions so bypass it.
             * Send the ProcessingInstruction node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.processingInstruction((ProcessingInstruction) node);
            }
            break;
        case Node.COMMENT_NODE:
            /**
             * The validator does nothing with comments so bypass it.
             * Send the Comment node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.comment((Comment) node);
            }
            break;
        case Node.DOCUMENT_TYPE_NODE:
            /**
             * Send the DocumentType node directly to the result builder.
             */
            if (fDOMValidatorHandler != null) {
                fDOMValidatorHandler.doctypeDecl((DocumentType) node);
            }
            break;
        default: // Ignore other node types.
            break;
    }
}
 
Example 13
Source File: SignatureValidator.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
boolean validate(String fn, KeySelector ks, URIDereferencer ud,
    boolean cache) throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);
    Document doc = dbf.newDocumentBuilder().parse(new File(dir, fn));
    NodeList nl =
        doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
    if (nl.getLength() == 0) {
        throw new Exception("Couldn't find signature Element");
    }
    Element sigElement = (Element) nl.item(0);
    DOMValidateContext vc = new DOMValidateContext(ks, sigElement);
    vc.setBaseURI(dir.toURI().toString());
    if (cache) {
        vc.setProperty("javax.xml.crypto.dsig.cacheReference", Boolean.TRUE);
    }
    XMLSignatureFactory factory = XMLSignatureFactory.getInstance();
    XMLSignature signature = factory.unmarshalXMLSignature(vc);
    if (ud != null) {
        vc.setURIDereferencer(ud);
    }
    boolean coreValidity = signature.validate(vc);

    // Check reference cache
    if (cache) {
        Iterator i = signature.getSignedInfo().getReferences().iterator();
        for (int j=0; i.hasNext(); j++) {
            Reference ref = (Reference) i.next();
            if (!digestInputEqual(ref)) {
                throw new Exception
                    ("cached data for Reference[" + j + "] is not correct");
            }
            // check that dereferenced data does not contain comment nodes
            if (ref.getURI() == "") {
                System.out.println("checking deref data");
                NodeSetData data = (NodeSetData) ref.getDereferencedData();
                Iterator ni = data.iterator();
                while (ni.hasNext()) {
                    Node n = (Node) ni.next();
                    if (n.getNodeType() == Node.COMMENT_NODE) {
                        throw new Exception("dereferenced data for " +
                            " Reference[" + j + " contains comment node");
                    }
                }
            }
        }
    }
    return coreValidity;
}
 
Example 14
Source File: DOMSubTreeData.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Recursively traverses the subtree, and returns an XPath-equivalent
 * node-set of all nodes traversed, excluding any comment nodes,
 * if specified.
 *
 * @param node the node to traverse
 * @param nodeSet the set of nodes traversed so far
 * @param the previous sibling node
 */
@SuppressWarnings("fallthrough")
private void nodeSetMinusCommentNodes(Node node, List<Node> nodeSet,
                                      Node prevSibling)
{
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            NamedNodeMap attrs = node.getAttributes();
            if (attrs != null) {
                for (int i = 0, len = attrs.getLength(); i < len; i++) {
                    nodeSet.add(attrs.item(i));
                }
            }
            nodeSet.add(node);
            Node pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.DOCUMENT_NODE :
            pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE:
            // emulate XPath which only returns the first node in
            // contiguous text/cdata nodes
            if (prevSibling != null &&
                (prevSibling.getNodeType() == Node.TEXT_NODE ||
                 prevSibling.getNodeType() == Node.CDATA_SECTION_NODE)) {
                return;
            }
            nodeSet.add(node);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE :
            nodeSet.add(node);
            break;
        case Node.COMMENT_NODE:
            if (withComments) {
                nodeSet.add(node);
            }
    }
}
 
Example 15
Source File: AndroidResValuesProvider.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
private void decodeElements(NodeList childNodes, List<BasicValuesCompletionItem> tmp) {
    String comment = "";
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node node = childNodes.item(j);
        switch (node.getNodeType()) {
            case Node.COMMENT_NODE:
                if (!"".equals(comment)) {
                    comment += "\n";
                }
                comment += node.getTextContent();
                break;
            case Node.ELEMENT_NODE:
                String nodeName = node.getNodeName();
                switch (nodeName) {
                    case "eat-comment":
                        comment = "";
                        break;
                    case "string":
                        makeBasic(AndroidValueType.STRING, node, tmp, comment);
                        comment = "";
                        break;
                    case "integer":
                        makeBasic(AndroidValueType.INTEGER, node, tmp, comment);
                        comment = "";
                        break;
                    case "bool":
                        makeBasic(AndroidValueType.BOOL, node, tmp, comment);
                        comment = "";
                        break;
                    case "color":
                        makeBasic(AndroidValueType.COLOR, node, tmp, comment);
                        comment = "";
                        break;
                    case "dimen":
                        makeBasic(AndroidValueType.DIMEN, node, tmp, comment);
                        comment = "";
                        break;
                    case "java-symbol":
                        makeBasicWithType(AndroidValueType.SYMBOL, node, tmp, comment);
                        comment = "";
                        break;
                    case "item":
                        makeBasicWithType(AndroidValueType.ITEM, node, tmp, comment);
                        comment = "";
                        break;
                }
        }
    }
}
 
Example 16
Source File: DOMUtil.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 17
Source File: ReferenceSubTreeData.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Recursively traverses the subtree, and returns an XPath-equivalent
 * node-set of all nodes traversed, excluding any comment nodes,
 * if specified.
 *
 * @param node the node to traverse
 * @param nodeSet the set of nodes traversed so far
 * @param the previous sibling node
 */
@SuppressWarnings("fallthrough")
private void nodeSetMinusCommentNodes(Node node, List<Node> nodeSet,
                                      Node prevSibling)
{
    switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            nodeSet.add(node);
            NamedNodeMap attrs = node.getAttributes();
            if (attrs != null) {
                for (int i = 0, len = attrs.getLength(); i < len; i++) {
                    nodeSet.add(attrs.item(i));
                }
            }
            Node pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.DOCUMENT_NODE :
            pSibling = null;
            for (Node child = node.getFirstChild(); child != null;
                child = child.getNextSibling()) {
                nodeSetMinusCommentNodes(child, nodeSet, pSibling);
                pSibling = child;
            }
            break;
        case Node.TEXT_NODE :
        case Node.CDATA_SECTION_NODE:
            // emulate XPath which only returns the first node in
            // contiguous text/cdata nodes
            if (prevSibling != null &&
                (prevSibling.getNodeType() == Node.TEXT_NODE ||
                 prevSibling.getNodeType() == Node.CDATA_SECTION_NODE)) {
                return;
            }
            nodeSet.add(node);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE :
            nodeSet.add(node);
            break;
        case Node.COMMENT_NODE:
            if (withComments) {
                nodeSet.add(node);
            }
    }
}
 
Example 18
Source File: TestingUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private static Node skipBlankText(Node node) {
  while (node != null && (((node.getNodeType() == Node.TEXT_NODE) && Utilities.isWhitespace(node.getTextContent())) || (node.getNodeType() == Node.COMMENT_NODE))) 
    node = node.getNextSibling();
  return node;
}
 
Example 19
Source File: DOMUtil.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
Example 20
Source File: TestingUtilities.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
private static Node skipBlankText(Node node) {
  while (node != null && (((node.getNodeType() == Node.TEXT_NODE) && Utilities.isWhitespace(node.getTextContent())) || (node.getNodeType() == Node.COMMENT_NODE)))
    node = node.getNextSibling();
  return node;
}