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

The following are Jave code examples for showing how to use getPreviousSibling() 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: incubator-netbeans   File: MavenProjectGrammar.java   Source Code and License Vote up 6 votes
private ArtifactInfoHolder findArtifactInfo(Node previous) {
    ArtifactInfoHolder holder = new ArtifactInfoHolder();
    while (previous != null) {
        if (previous instanceof org.w3c.dom.Element) {
            org.w3c.dom.Element el = (org.w3c.dom.Element)previous;
            NodeList lst = el.getChildNodes();
            if (lst.getLength() > 0) {
                if ("artifactId".equals(el.getNodeName())) { //NOI18N
                    holder.setArtifactId(getNodeValue(lst.item(0).getNodeValue(), owner.getLookup().lookup(NbMavenProject.class)));
                }
                if ("groupId".equals(el.getNodeName())) { //NOI18N
                    holder.setGroupId(getNodeValue(lst.item(0).getNodeValue(), owner.getLookup().lookup(NbMavenProject.class)));
                }
                if ("version".equals(el.getNodeName())) { //NOI18N
                    holder.setVersion(getNodeValue(lst.item(0).getNodeValue(), owner.getLookup().lookup(NbMavenProject.class)));
                }
            }
        }
        previous = previous.getPreviousSibling();
    }
    return holder;
}
 
Example 2
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 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 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 4
Project: javaide   File: ManifestMerger.java   Source Code and License Vote up 6 votes
/**
 * Given an element E, select which previous siblings we want to merge.
 * We want to include any whitespace up to the closing of the previous element.
 * We also want to include up preceding comment nodes and their preceding whitespace.
 * <p/>
 * This may returns either {@code end} or a previous sibling. Never returns null.
 */
@NonNull
private Node selectPreviousSiblings(Node end) {

    Node start = end;
    Node prev = start.getPreviousSibling();
    while (prev != null) {
        short t = prev.getNodeType();
        if (t == Node.TEXT_NODE) {
            String text = prev.getNodeValue();
            if (text == null || text.trim().length() != 0) {
                // Not whitespace, we don't want it.
                break;
            }
        } else if (t == Node.COMMENT_NODE) {
            // It's a comment. We'll take it.
        } else {
            // Not a comment node nor a whitespace text. We don't want it.
            break;
        }
        start = prev;
        prev = start.getPreviousSibling();
    }

    return start;
}
 
Example 5
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 6
Project: OpenJSharp   File: DOMUtil.java   Source Code and License Vote up 6 votes
/**
 * 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 7
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 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
Project: cascade   File: Elements.java   Source Code and License Vote up 5 votes
public static boolean hasSameTypeBefore(Element element) {
    Node sibling = element.getPreviousSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            if (isSameType(element, (Element)sibling)) {
                return true;
            }
        }
        sibling = sibling.getPreviousSibling();
    }
    return false;
}
 
Example 9
Project: launcher-backend   File: ChoosePipelineStep.java   Source Code and License Vote up 5 votes
/**
 * Returns the combined text of previous nodes of the given element
 */
private static String getPreviousText(Node node) {
    StringBuilder builder = new StringBuilder();
    while (node != null) {
        node = node.getPreviousSibling();
        if (node instanceof Text) {
            Text textNode = (Text) node;
            builder.append(textNode.getWholeText());
        } else {
            break;
        }
    }
    return builder.toString();
}
 
Example 10
Project: openjdk-jdk10   File: TextImpl.java   Source Code and License Vote up 5 votes
/**
 * Concatenates the text of all logically-adjacent text nodes to the left of
 * the node
 * @param node
 * @param buffer
 * @param parent
 * @return true - if execution was stopped because the type of node
 *         other than EntityRef, Text, CDATA is encountered, otherwise
 *         return false
 */
private boolean getWholeTextBackward(Node node, StringBuffer buffer, Node parent){

    // boolean to indicate whether node is a child of an entity reference
    boolean inEntRef = false;
    if (parent!=null) {
            inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE;
    }

    while (node != null) {
        short type = node.getNodeType();
        if (type == Node.ENTITY_REFERENCE_NODE) {
            if (getWholeTextBackward(node.getLastChild(), buffer, node)){
                return true;
            }
        }
        else if (type == Node.TEXT_NODE ||
                 type == Node.CDATA_SECTION_NODE) {
            ((TextImpl)node).insertTextContent(buffer);
        }
        else {
            return true;
        }

        node = node.getPreviousSibling();
    }

    // if the parent node is an entity reference node, must
    // check nodes to the left of the parent entity reference node for logically adjacent
    // text nodes
    if (inEntRef) {
            getWholeTextBackward(parent.getPreviousSibling(), buffer, parent.getParentNode());
        return true;
    }

    return false;
}
 
Example 11
Project: convertigo-engine   File: DOMNodePointer.java   Source Code and License Vote up 5 votes
/**
 * Get relative position of this among all siblings.
 * @return 1..n
 */
private int getRelativePositionOfElement() {
    int count = 1;
    Node n = node.getPreviousSibling();
    while (n != null) {
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            count++;
        }
        n = n.getPreviousSibling();
    }
    return count;
}
 
Example 12
Project: openjdk-jdk10   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 13
Project: convertigo-engine   File: DOMNodePointer.java   Source Code and License Vote up 5 votes
/**
 * Get the relative position of this among same-target processing instruction siblings.
 * @return 1..n
 */
private int getRelativePositionOfPI() {
    int count = 1;
    String target = ((ProcessingInstruction) node).getTarget();
    Node n = node.getPreviousSibling();
    while (n != null) {
        if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE
            && ((ProcessingInstruction) n).getTarget().equals(target)) {
            count++;
        }
        n = n.getPreviousSibling();
    }
    return count;
}
 
Example 14
Project: javaide   File: XmlElement.java   Source Code and License Vote up 5 votes
/**
 * Returns all leading comments in the source xml before the node to be adopted.
 * @param nodeToBeAdopted node that will be added as a child to this node.
 */
static List<Node> getLeadingComments(Node nodeToBeAdopted) {
    ImmutableList.Builder<Node> nodesToAdopt = new ImmutableList.Builder<Node>();
    Node previousSibling = nodeToBeAdopted.getPreviousSibling();
    while (previousSibling != null
            && (previousSibling.getNodeType() == Node.COMMENT_NODE
            || previousSibling.getNodeType() == Node.TEXT_NODE)) {
        // we really only care about comments.
        if (previousSibling.getNodeType() == Node.COMMENT_NODE) {
            nodesToAdopt.add(previousSibling);
        }
        previousSibling = previousSibling.getPreviousSibling();
    }
    return nodesToAdopt.build().reverse();
}
 
Example 15
Project: cascade   File: Elements.java   Source Code and License Vote up 5 votes
public static boolean hasSiblingBefore(Element element) {
    Node sibling = element.getPreviousSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
        sibling = sibling.getPreviousSibling();
    }
    return false;
}
 
Example 16
Project: Ins_fb_pictureSpider_WEB   File: BookMark.java   Source Code and License Vote up 5 votes
/**
 * Inserts some text into a Word document immediately in front of the
 * location of a bookmark.
 *
 * This case is slightly more straightforward than inserting after the
 * bookmark. For example, it is possible only to insert a new node in front
 * of an existing node. When inserting after the bookmark, then end node had
 * to be located whereas, in this case, the node is already known, it is the
 * CTBookmark itself. The only information that must be discovered is
 * whether there is a run immediately in front of the boookmarkStart tag and
 * whether that run is styled. If there is and if it is, then this style
 * must be cloned and applied the text which will be inserted into the
 * paragraph.
 *
 * @param run An instance of the XWPFRun class that encapsulates the text
 * that is to be inserted into the document following the bookmark.
 */
private void insertBeforeBookmark(XWPFRun run) {
    Node insertBeforeNode = null;
    Node childNode = null;
    Node styleNode = null;

    // Get the dom node from the bookmarkStart tag and look for another
    // node immediately preceding it.
    insertBeforeNode = this._ctBookmark.getDomNode();
    childNode = insertBeforeNode.getPreviousSibling();

    // If a node is found, try to get the styling from it.
    if (childNode != null) {
        styleNode = this.getStyleNode(childNode);

        // If that previous node was styled, then apply this style to the
        // text which will be inserted.
        if (styleNode != null) {
            run.getCTR().getDomNode().insertBefore(
                    styleNode.cloneNode(true), run.getCTR().getDomNode().getFirstChild());
        }
    }

    // Insert the text into the paragraph immediately in front of the
    // bookmarkStart tag.
    this._para.getCTP().getDomNode().insertBefore(
            run.getCTR().getDomNode(), insertBeforeNode);
}
 
Example 17
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 18
Project: OpenJSharp   File: TreeWalkerImpl.java   Source Code and License Vote up 5 votes
/** Internal function.
 *  Return the previousSibling Node, from the input node
 *  after applying filter, whatToshow.
     *  NEVER TRAVERSES ABOVE THE SPECIFIED ROOT NODE.
 *  The current node is not consulted or set.
 */
Node getPreviousSibling(Node node, Node root) {

    if (node == null || node == root) return null;

    Node newNode = node.getPreviousSibling();
    if (newNode == null) {

        newNode = node.getParentNode();
        if (newNode == null || newNode == root)  return null;

        int parentAccept = acceptNode(newNode);

        if (parentAccept==NodeFilter.FILTER_SKIP) {
            return getPreviousSibling(newNode, root);
        }

        return null;
    }

    int accept = acceptNode(newNode);

    if (accept == NodeFilter.FILTER_ACCEPT)
        return newNode;
    else
    if (accept == NodeFilter.FILTER_SKIP) {
        Node fChild =  getLastChild(newNode);
        if (fChild == null) {
            return getPreviousSibling(newNode, root);
        }
        return fChild;
    }
    else
    //if (accept == NodeFilter.REJECT_NODE)
    {
        return getPreviousSibling(newNode, root);
    }

}
 
Example 19
Project: javaide   File: XmlPrettyPrinter.java   Source Code and License Vote up 4 votes
private boolean newlineBeforeElementOpen(Element element, int depth) {
    if (hasBlankLineAbove()) {
        return false;
    }

    if (mPrefs.removeEmptyLines || depth <= 0) {
        return false;
    }

    if (isMarkupElement(element)) {
        return false;
    }

    // See if this element should be separated from the previous element.
    // This is the case if we are not compressing whitespace (checked above),
    // or if we are not immediately following a comment (in which case the
    // newline would have been added above it), or if we are not in a formatting
    // style where
    if (mStyle == XmlFormatStyle.LAYOUT) {
        // In layouts we always separate elements
        return true;
    }

    if (mStyle == XmlFormatStyle.MANIFEST || mStyle == XmlFormatStyle.RESOURCE
            || mStyle == XmlFormatStyle.FILE) {
        Node curr = element.getPreviousSibling();

        // <style> elements are traditionally separated unless it follows a comment
        if (TAG_STYLE.equals(element.getTagName())) {
            if (curr == null
                    || curr.getNodeType() == Node.ELEMENT_NODE
                    || (curr.getNodeType() == Node.TEXT_NODE
                    && curr.getNodeValue().trim().isEmpty()
                    && (curr.getPreviousSibling() == null
                    || curr.getPreviousSibling().getNodeType()
                    == Node.ELEMENT_NODE))) {
                return true;
            }
        }

        // In all other styles, we separate elements if they have a different tag than
        // the previous one (but we don't insert a newline inside tags)
        while (curr != null) {
            short nodeType = curr.getNodeType();
            if (nodeType == Node.ELEMENT_NODE) {
                Element sibling = (Element) curr;
                if (!element.getTagName().equals(sibling.getTagName())) {
                    return true;
                }
                break;
            } else if (nodeType == Node.TEXT_NODE) {
                String text = curr.getNodeValue();
                if (!text.trim().isEmpty()) {
                    break;
                }
                // If there is just whitespace, continue looking for a previous sibling
            } else {
                // Any other previous node type, such as a comment, means we don't
                // continue looking: this element should not be separated
                break;
            }
            curr = curr.getPreviousSibling();
        }
        if (curr == null && depth <= 1) {
            // Insert new line inside tag if it's the first element inside the root tag
            return true;
        }

        return false;
    }

    return false;
}
 
Example 20
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

}