Java Code Examples for org.w3c.dom.Node#getLastChild()

The following examples show how to use org.w3c.dom.Node#getLastChild() . 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: DOMUtil.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/** Finds and returns the last visible child element node. */
public static Element getLastVisibleChildElement(Node parent) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE &&
                !isHidden(child)) {
            return (Element)child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 2
Source File: DOMUtil.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last child element node.
 *  Overload previous method for non-Xerces node impl.
 */
public static Element getLastChildElement(Node parent) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element)child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 3
Source File: DOMUtil.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last visible child element node.
 *  Overload previous method for non-Xerces node impl
 */
public static Element getLastVisibleChildElement(Node parent, Hashtable hiddenNodes) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE &&
                !isHidden(child, hiddenNodes)) {
            return (Element)child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 4
Source File: DOMUtil.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last visible child element node. */
public static Element getLastVisibleChildElement(Node parent) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE &&
                !isHidden(child)) {
            return (Element)child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 5
Source File: DOMUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last visible child element node.
 *  Overload previous method for non-Xerces node impl
 */
public static Element getLastVisibleChildElement(Node parent, Map<Node, String> hiddenNodes) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE &&
                !isHidden(child, hiddenNodes)) {
            return (Element)child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 6
Source File: DOMUtil.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last visible child element node. */
public static Element getLastVisibleChildElement(Node parent) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE &&
                !isHidden(child)) {
            return (Element)child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 7
Source File: DOMUtil.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemName) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element)child;
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 8
Source File: DOMUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last child node with the given qualified name. */
public static Element getLastChildElementNS(Node parent,
        String uri, String localpart) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            String childURI = child.getNamespaceURI();
            if (childURI != null && childURI.equals(uri) &&
                    child.getLocalName().equals(localpart)) {
                return (Element)child;
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 9
Source File: DOMUtil.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Finds and returns the last child node with the given name and
 * attribute name, value pair.
 */
public static Element getLastChildElement(Node   parent,
        String elemName,
        String attrName,
        String attrValue) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element)child;
            if (element.getNodeName().equals(elemName) &&
                    element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 10
Source File: ElementImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private Element getLastElementChild(Node n) {
    final Node top = n;
    while (n != null) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) n;
        }
        Node next = n.getLastChild();
        while (next == null) {
            if (top == n) {
                break;
            }
            next = n.getPreviousSibling();
            if (next == null) {
                n = n.getParentNode();
                if (n == null || top == n) {
                    return null;
                }
            }
        }
        n = next;
    }
    return null;
}
 
Example 11
Source File: DOMUtil.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Finds and returns the last child node with the given qualified name. */
public static Element getLastChildElementNS(Node parent,
        String[][] elemNames) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                String uri = child.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0]) &&
                        child.getLocalName().equals(elemNames[i][1])) {
                    return (Element)child;
                }
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 12
Source File: NodeIteratorImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** The method previousNode(Node) returns the previous node
 *  from the actual DOM tree.
 */
Node previousNode(Node node) {

    Node result;

    // if we're at the root, return null.
    if (node == fRoot) return null;

    // get sibling
    result = node.getPreviousSibling();
    if (result == null) {
        //if 1st sibling, return parent
        result = node.getParentNode();
        return result;
    }

    // if sibling has children, keep getting last child of child.
    if (result.hasChildNodes()
        && !(!fEntityReferenceExpansion
            && result != null
            && result.getNodeType() == Node.ENTITY_REFERENCE_NODE))

    {
        while (result.hasChildNodes()) {
            result = result.getLastChild();
        }
    }

    return result;
}
 
Example 13
Source File: NodeIteratorImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/** The method previousNode(Node) returns the previous node
 *  from the actual DOM tree.
 */
Node previousNode(Node node) {

    Node result;

    // if we're at the root, return null.
    if (node == fRoot) return null;

    // get sibling
    result = node.getPreviousSibling();
    if (result == null) {
        //if 1st sibling, return parent
        result = node.getParentNode();
        return result;
    }

    // if sibling has children, keep getting last child of child.
    if (result.hasChildNodes()
        && !(!fEntityReferenceExpansion
            && result != null
            && result.getNodeType() == Node.ENTITY_REFERENCE_NODE))

    {
        while (result.hasChildNodes()) {
            result = result.getLastChild();
        }
    }

    return result;
}
 
Example 14
Source File: XMLStreamBuffer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes out the contents of this buffer as DOM node and append that to the given node.
 *
 * Faster implementation would be desirable.
 *
 * @return
 *      The newly added child node.
 */
public final Node writeTo(Node n) throws XMLStreamBufferException {
    try {
        Transformer t = trnsformerFactory.get().newTransformer();
        t.transform(new XMLStreamBufferSource(this), new DOMResult(n));
        return n.getLastChild();
    } catch (TransformerException e) {
        throw new XMLStreamBufferException(e);
    }
}
 
Example 15
Source File: XMLStreamBuffer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes out the contents of this buffer as DOM node and append that to the given node.
 *
 * Faster implementation would be desirable.
 *
 * @return
 *      The newly added child node.
 */
public final Node writeTo(Node n) throws XMLStreamBufferException {
    try {
        Transformer t = trnsformerFactory.get().newTransformer();
        t.transform(new XMLStreamBufferSource(this), new DOMResult(n));
        return n.getLastChild();
    } catch (TransformerException e) {
        throw new XMLStreamBufferException(e);
    }
}
 
Example 16
Source File: NodeIteratorImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** The method previousNode(Node) returns the previous node
 *  from the actual DOM tree.
 */
Node previousNode(Node node) {

    Node result;

    // if we're at the root, return null.
    if (node == fRoot) return null;

    // get sibling
    result = node.getPreviousSibling();
    if (result == null) {
        //if 1st sibling, return parent
        result = node.getParentNode();
        return result;
    }

    // if sibling has children, keep getting last child of child.
    if (result.hasChildNodes()
        && !(!fEntityReferenceExpansion
            && result != null
            && result.getNodeType() == Node.ENTITY_REFERENCE_NODE))

    {
        while (result.hasChildNodes()) {
            result = result.getLastChild();
        }
    }

    return result;
}
 
Example 17
Source File: DeferredDocumentImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/** Synchronizes the node's data. */
protected void synchronizeData() {

    // no need to sync in the future
    needsSyncData(false);

    // fluff up enough nodes to fill identifiers hash
    if (fIdElement != null) {

        // REVISIT: There has to be a more efficient way of
        //          doing this. But keep in mind that the
        //          tree can have been altered and re-ordered
        //          before all of the element nodes with ID
        //          attributes have been registered. For now
        //          this is reasonable and safe. -Ac

        IntVector path = new IntVector();
        for (int i = 0; i < fIdCount; i++) {

            // ignore if it's already been registered
            int elementNodeIndex = fIdElement[i];
            String idName      = fIdName[i];
            if (idName == null) {
                continue;
            }

            // find path from this element to the root
            path.removeAllElements();
            int index = elementNodeIndex;
            do {
                path.addElement(index);
                int pchunk = index >> CHUNK_SHIFT;
                int pindex = index & CHUNK_MASK;
                index = getChunkIndex(fNodeParent, pchunk, pindex);
            } while (index != -1);

            // Traverse path (backwards), fluffing the elements
            // along the way. When this loop finishes, "place"
            // will contain the reference to the element node
            // we're interested in. -Ac
            Node place = this;
            for (int j = path.size() - 2; j >= 0; j--) {
                index = path.elementAt(j);
                Node child = place.getLastChild();
                while (child != null) {
                    if (child instanceof DeferredNode) {
                        int nodeIndex =
                            ((DeferredNode)child).getNodeIndex();
                        if (nodeIndex == index) {
                            place = child;
                            break;
                        }
                    }
                    child = child.getPreviousSibling();
                }
            }

            // register the element
            Element element = (Element)place;
            putIdentifier0(idName, element);
            fIdName[i] = null;

            // see if there are more IDs on this element
            while (i + 1 < fIdCount &&
                fIdElement[i + 1] == elementNodeIndex) {
                idName = fIdName[++i];
                if (idName == null) {
                    continue;
                }
                putIdentifier0(idName, element);
            }
        }

    } // if identifiers

}
 
Example 18
Source File: TextImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * If any EntityReference to be removed has descendants that are not
 * EntityReference, Text, or CDATASection nodes, the replaceWholeText method
 * must fail before performing any modification of the document, raising a
 * DOMException with the code NO_MODIFICATION_ALLOWED_ERR. Traverse previous
 * siblings of the node to be replaced. If a previous sibling is an
 * EntityReference node, get it's last child. If the last child was a Text
 * or CDATASection node and its previous siblings are neither a replaceable
 * EntityReference or Text or CDATASection nodes, return false. IF the last
 * child was neither Text nor CDATASection nor a replaceable EntityReference
 * Node, then return true. If the last child was a Text or CDATASection node
 * any its previous sibling was not or was an EntityReference that did not
 * contain only Text or CDATASection nodes, return false. Check this
 * recursively for EntityReference nodes.
 *
 * @param node
 * @return true - can replace text false - can't replace exception must be
 *         raised
 */
private boolean canModifyPrev(Node node) {
    boolean textLastChild = false;

    Node prev = node.getPreviousSibling();

    while (prev != null) {

        short type = prev.getNodeType();

        if (type == Node.ENTITY_REFERENCE_NODE) {
            //If the previous sibling was entityreference
            //check if its content is replaceable
            Node lastChild = prev.getLastChild();

            //if the entity reference has no children
            //return false
            if (lastChild == null) {
                return false;
            }

            //The replacement text of the entity reference should
            //be either only text,cadatsections or replaceable entity
            //reference nodes or the last child should be neither of these
            while (lastChild != null) {
                short lType = lastChild.getNodeType();

                if (lType == Node.TEXT_NODE
                        || lType == Node.CDATA_SECTION_NODE) {
                    textLastChild = true;
                } else if (lType == Node.ENTITY_REFERENCE_NODE) {
                    if (!canModifyPrev(lastChild)) {
                        return false;
                    } else {
                        //If the EntityReference child contains
                        //only text, or non-text or ends with a
                        //non-text node.
                        textLastChild = true;
                    }
                } else {
                    //If the last child was replaceable and others are not
                    //Text or CDataSection or replaceable EntityRef nodes
                    //return false.
                    if (textLastChild) {
                        return false;
                    } else {
                        return true;
                    }
                }
                lastChild = lastChild.getPreviousSibling();
            }
        } else if (type == Node.TEXT_NODE
                || type == Node.CDATA_SECTION_NODE) {
            //If the previous sibling was text or cdatasection move to next
        } else {
            //If the previous sibling was anything but text or
            //cdatasection or an entity reference, stop search and
            //return true
            return true;
        }

        prev = prev.getPreviousSibling();
    }

    return true;
}
 
Example 19
Source File: DeferredDocumentImpl.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/** Synchronizes the node's data. */
protected void synchronizeData() {

    // no need to sync in the future
    needsSyncData(false);

    // fluff up enough nodes to fill identifiers hash
    if (fIdElement != null) {

        // REVISIT: There has to be a more efficient way of
        //          doing this. But keep in mind that the
        //          tree can have been altered and re-ordered
        //          before all of the element nodes with ID
        //          attributes have been registered. For now
        //          this is reasonable and safe. -Ac

        IntVector path = new IntVector();
        for (int i = 0; i < fIdCount; i++) {

            // ignore if it's already been registered
            int elementNodeIndex = fIdElement[i];
            String idName      = fIdName[i];
            if (idName == null) {
                continue;
            }

            // find path from this element to the root
            path.removeAllElements();
            int index = elementNodeIndex;
            do {
                path.addElement(index);
                int pchunk = index >> CHUNK_SHIFT;
                int pindex = index & CHUNK_MASK;
                index = getChunkIndex(fNodeParent, pchunk, pindex);
            } while (index != -1);

            // Traverse path (backwards), fluffing the elements
            // along the way. When this loop finishes, "place"
            // will contain the reference to the element node
            // we're interested in. -Ac
            Node place = this;
            for (int j = path.size() - 2; j >= 0; j--) {
                index = path.elementAt(j);
                Node child = place.getLastChild();
                while (child != null) {
                    if (child instanceof DeferredNode) {
                        int nodeIndex =
                            ((DeferredNode)child).getNodeIndex();
                        if (nodeIndex == index) {
                            place = child;
                            break;
                        }
                    }
                    child = child.getPreviousSibling();
                }
            }

            // register the element
            Element element = (Element)place;
            putIdentifier0(idName, element);
            fIdName[i] = null;

            // see if there are more IDs on this element
            while (i + 1 < fIdCount &&
                fIdElement[i + 1] == elementNodeIndex) {
                idName = fIdName[++i];
                if (idName == null) {
                    continue;
                }
                putIdentifier0(idName, element);
            }
        }

    } // if identifiers

}
 
Example 20
Source File: DOMUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the last child element of the specified node, or null if there
 * is no such element.
 *
 * @param node the node
 * @return the last child element of the specified node, or null if there
 *    is no such element
 * @throws NullPointerException if <code>node == null</code>
 */
public static Element getLastChildElement(Node node) {
    Node child = node.getLastChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getPreviousSibling();
    }
    return (Element)child;
}