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

The following are Jave code examples for showing how to use getLastChild() of the org.w3c.dom.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: openjdk-jdk10   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 2
Project: javaide   File: ManifestMerger.java   Source Code and License Vote up 6 votes
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 3
Project: openjdk-jdk10   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 4
Project: openjdk-jdk10   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 5
Project: openjdk-jdk10   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 6
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 7
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 8
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 9
Project: openjdk-jdk10   File: ElementImpl.java   Source Code and License Vote up 6 votes
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 10
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 11
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(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++) {
                if (child.getNodeName().equals(elemNames[i])) {
                    return (Element)child;
                }
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}
 
Example 12
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 13
Project: openjdk-jdk10   File: DOMUtil.java   Source Code and License Vote up 6 votes
/** 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 14
Project: openjdk-jdk10   File: TreeWalkerImpl.java   Source Code and License Vote up 5 votes
/** Internal function.
 *  Return the last child Node, from the input node
 *  after applying filter, whatToshow.
 *  The current node is not consulted or set.
 */
Node getLastChild(Node node) {

    if (node == null) return null;

    if ( !fEntityReferenceExpansion
         && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
        return null;

    Node newNode = node.getLastChild();
    if (newNode == null)  return null;

    int accept = acceptNode(newNode);

    if (accept == NodeFilter.FILTER_ACCEPT)
        return newNode;
    else
    if (accept == NodeFilter.FILTER_SKIP
        && newNode.hasChildNodes())
    {
        Node lChild = getLastChild(newNode);
        if (lChild == null) {
            return getPreviousSibling(newNode, node);
        }
        return lChild;
    }
    else
    //if (accept == NodeFilter.REJECT_NODE)
    {
        return getPreviousSibling(newNode, node);
    }


}
 
Example 15
Project: OpenJSharp   File: NodeIteratorImpl.java   Source Code and License Vote up 5 votes
/** 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 16
Project: OpenJSharp   File: TreeWalkerImpl.java   Source Code and License Vote up 5 votes
/** Internal function.
 *  Return the last child Node, from the input node
 *  after applying filter, whatToshow.
 *  The current node is not consulted or set.
 */
Node getLastChild(Node node) {

    if (node == null) return null;

    if ( !fEntityReferenceExpansion
         && node.getNodeType() == Node.ENTITY_REFERENCE_NODE)
        return null;

    Node newNode = node.getLastChild();
    if (newNode == null)  return null;

    int accept = acceptNode(newNode);

    if (accept == NodeFilter.FILTER_ACCEPT)
        return newNode;
    else
    if (accept == NodeFilter.FILTER_SKIP
        && newNode.hasChildNodes())
    {
        Node lChild = getLastChild(newNode);
        if (lChild == null) {
            return getPreviousSibling(newNode, node);
        }
        return lChild;
    }
    else
    //if (accept == NodeFilter.REJECT_NODE)
    {
        return getPreviousSibling(newNode, node);
    }


}
 
Example 17
Project: ccpa   File: TreeOptimizer.java   Source Code and License Vote up 4 votes
private static void convertToFinalForm(Node program) throws TransformerException
{
	Element functions = doc.createElement("functions");
	Element dataStructures = doc.createElement("structures");
	Element init = doc.createElement("init");

	removeFunctions(program, functions);
	removeDataStructures(program, dataStructures);
	Node initCode = program.getFirstChild();
	Node globalVariable = initCode.getLastChild();

	while (globalVariable.getNodeName().equals("uses"))
	{
		dataStructures.appendChild(globalVariable.cloneNode(true));
		globalVariable = globalVariable.getPreviousSibling();
		initCode.removeChild(globalVariable.getNextSibling());
	}

	init.appendChild(initCode.cloneNode(true));

	while (program.hasChildNodes())
		program.removeChild(program.getFirstChild());

	NodeList myFunctions = functions.getChildNodes();

	for (int i = 0; i < myFunctions.getLength();i++)
	{
		for (Node temp = myFunctions.item(i).getLastChild();temp != null;temp = temp.getPreviousSibling())
		{
			if (temp.getLastChild().getTextContent().equals(myFunctions.item(i).getFirstChild().getTextContent()))
			{
				myFunctions.item(i).removeChild(temp);
				break;
			}
		}
	}

	Element callMain = doc.createElement("op");
	callMain.setAttribute("name", "CALL");
	callMain.setAttribute("type", "void");

	Element mainName = doc.createElement("value");
	mainName.setTextContent("main");

	callMain.appendChild(mainName);

	Element mainParams = doc.createElement("op");
	mainParams.setAttribute("name", "NO_ARG");
	mainParams.setAttribute("type", "");

	callMain.appendChild(mainParams);

	init.appendChild(callMain);

	Element haltProgram = doc.createElement("op");
	haltProgram.setAttribute("name", "HALT");
	init.appendChild(haltProgram);

	program.appendChild(init);
	program.appendChild(functions);
	program.appendChild(dataStructures);
}
 
Example 18
Project: ccpa   File: TreeTransformer.java   Source Code and License Vote up 4 votes
public static void setStructIndices(Node program)
{
	Node structures = program.getLastChild();
	for (Node n = structures.getFirstChild(); n != null; n = n.getNextSibling())
	{
		if (n.getNodeName().equals("data"))
		{
			String kind = n.getAttributes().getNamedItem("kind").getTextContent();
			if (kind.equals("STRUCT"))
			{
				int totalSize = 0;
				for (Node child = n.getLastChild(); !child.getNodeName().equals("value"); child = child.getPreviousSibling())
				{
					TreeToAE2.setIndexOf("struct " + n.getFirstChild().getTextContent(), child.getLastChild().getTextContent(), totalSize);
					try
					{
					    totalSize += TreeToAE2.getSizeOf(child.getFirstChild().getTextContent());
					}
					catch(Exception e)
					{
					}
				}
			}
			else if (kind.equals("UNION"))
			{
				for (Node child = n.getLastChild(); !child.getNodeName().equals("value"); child = child.getPreviousSibling())
				{
					TreeToAE2.setIndexOf("union " + n.getFirstChild().getTextContent(), child.getLastChild().getTextContent(), 0);
				}
			}
			else if (kind.equals("ENUM"))
			{
				int i = 0;
				for (Node child = n.getLastChild(); !child.getNodeName().equals("value"); child = child.getPreviousSibling())
				{
					TreeToAE2.setIndexOf("union " + n.getFirstChild().getTextContent(), child.getLastChild().getTextContent(), i);
					i++;
				}
			}
		}
	}
}
 
Example 19
Project: openjdk-jdk10   File: DeferredDocumentImpl.java   Source Code and License Vote up 4 votes
/** 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
Project: jdk8u-jdk   File: DOMUtils.java   Source Code and License Vote up 3 votes
/**
 * 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;
}