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

The following are Jave code examples for showing how to use getParentNode() 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.
+ Save this method
Example 1
Project: Equella   File: PropBagEx.java   View Source Code Vote up 6 votes
/**
 * Creates an iterator which return's PropBagEx's for each child for the
 * path given<br>
 * E.g.<br>
 * /path/node/item<br>
 * Will iterate over all the "item" nodes.
 * 
 * @param path
 * @return
 */
public PropBagIterator iterator(final String path)
{
	checkNotAttribute(path);
	ensureRoot();

	String name = null;
	Node parent = null;

	final Node node = getNodeHelper(path, false, false);
	if( node != null )
	{
		parent = node.getParentNode();

		// see Jira Defect TLE-1293 :
		// http://apps.dytech.com.au/jira/browse/TLE-1293
		name = DOMHelper.stripNamespace(node.getNodeName());
		if( path.endsWith(WILD) )
		{
			name = WILD;
		}

	}
	return new PropBagIterator(parent, node, name, m_elRoot);
}
 
Example 2
Project: fc-java-sdk   File: XmlUtils.java   View Source Code Vote up 6 votes
public static List<Element> getChildElements(Element parent, String tagName) {
    if (null == parent) {
        return null;
    }
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getParentNode() == parent) {
            elements.add((Element) node);
        }
    }

    return elements;
}
 
Example 3
Project: incubator-netbeans   File: XMLUtil.java   View Source Code Vote up 6 votes
private static void collectCDATASections(Node node, Set<String> cdataQNames) {
    if (node instanceof CDATASection) {
        Node parent = node.getParentNode();
        if (parent != null) {
            String uri = parent.getNamespaceURI();
            if (uri != null) {
                cdataQNames.add("{" + uri + "}" + parent.getNodeName()); //NOI18N
            } else {
                cdataQNames.add(parent.getNodeName());
            }
        }
    }
    
    NodeList children = node.getChildNodes();
    for(int i = 0; i < children.getLength(); i++) {
        collectCDATASections(children.item(i), cdataQNames);
    }
}
 
Example 4
Project: openjdk-jdk10   File: ElementImpl.java   View Source Code Vote up 6 votes
private Node getNextLogicalSibling(Node n) {
    Node next = n.getNextSibling();
    // If "n" has no following sibling and its parent is an entity reference node we
    // need to continue the search through the following siblings of the entity
    // reference as these are logically siblings of the given node.
    if (next == null) {
        Node parent = n.getParentNode();
        while (parent != null && parent.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            next = parent.getNextSibling();
            if (next != null) {
                break;
            }
            parent = parent.getParentNode();
        }
    }
    return next;
}
 
Example 5
Project: OpenJSharp   File: CoreDocumentImpl.java   View Source Code Vote up 6 votes
/**
 * Returns a previously registered element with the specified
 * identifier name, or null if no element is registered.
 *
 * @see #putIdentifier
 * @see #removeIdentifier
 */
public Element getIdentifier(String idName) {

    if (needsSyncData()) {
        synchronizeData();
    }

    if (identifiers == null) {
        return null;
    }
    Element elem = (Element) identifiers.get(idName);
    if (elem != null) {
        // check that the element is in the tree
        Node parent = elem.getParentNode();
        while (parent != null) {
            if (parent == this) {
                return elem;
            }
            parent = parent.getParentNode();
        }
    }
    return null;
}
 
Example 6
Project: OpenJSharp   File: RangeImpl.java   View Source Code Vote up 5 votes
Node nextNode(Node node, boolean visitChildren) {

        if (node == null) return null;

        Node result;
        if (visitChildren) {
            result = node.getFirstChild();
            if (result != null) {
                return result;
            }
        }

        // if hasSibling, return sibling
        result = node.getNextSibling();
        if (result != null) {
            return result;
        }


        // return parent's 1st sibling.
        Node parent = node.getParentNode();
        while (parent != null
               && parent != fDocument
                ) {
            result = parent.getNextSibling();
            if (result != null) {
                return result;
            } else {
                parent = parent.getParentNode();
            }

        } // while (parent != null && parent != fRoot) {

        // end of list, return null
        return null;
    }
 
Example 7
Project: openjdk-jdk10   File: XMLSignatureInputDebugger.java   View Source Code Vote up 5 votes
/**
 * Checks whether a Comment or ProcessingInstruction is before or after the
 * document element. This is needed for prepending or appending "\n"s.
 *
 * @param currentNode
 *            comment or pi to check
 * @return NODE_BEFORE_DOCUMENT_ELEMENT,
 *         NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT or
 *         NODE_AFTER_DOCUMENT_ELEMENT
 * @see #NODE_BEFORE_DOCUMENT_ELEMENT
 * @see #NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT
 * @see #NODE_AFTER_DOCUMENT_ELEMENT
 */
private int getPositionRelativeToDocumentElement(Node currentNode) {
    if (currentNode == null) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    Document doc = currentNode.getOwnerDocument();

    if (currentNode.getParentNode() != doc) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    Element documentElement = doc.getDocumentElement();

    if (documentElement == null) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    if (documentElement == currentNode) {
        return NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
    }

    for (Node x = currentNode; x != null; x = x.getNextSibling()) {
        if (x == documentElement) {
            return NODE_BEFORE_DOCUMENT_ELEMENT;
        }
    }

    return NODE_AFTER_DOCUMENT_ELEMENT;
}
 
Example 8
Project: OpenJSharp   File: DOMValidatorHelper.java   View Source Code Vote up 5 votes
/** Traverse the DOM and fire events to the schema validator. */
private void validate(Node node) {
    final Node top = node;
    // Performs a non-recursive traversal of the DOM. This
    // will avoid a stack overflow for DOMs with high depth.
    while (node != null) {
        beginNode(node);
        Node next = node.getFirstChild();
        while (next == null) {
            finishNode(node);
            if (top == node) {
                break;
            }
            next = node.getNextSibling();
            if (next == null) {
                node = node.getParentNode();
                if (node == null || top == node) {
                    if (node != null) {
                        finishNode(node);
                    }
                    next = null;
                    break;
                }
            }
        }
        node = next;
    }
}
 
Example 9
Project: openjdk-jdk10   File: NodeImpl.java   View Source Code Vote up 5 votes
Node getElementAncestor (Node currentNode){
    Node parent = currentNode.getParentNode();
    if (parent != null) {
        short type = parent.getNodeType();
        if (type == Node.ELEMENT_NODE) {
            return parent;
        }
        return getElementAncestor(parent);
    }
    return null;
}
 
Example 10
Project: OpenJSharp   File: TreeWalkerImpl.java   View Source Code 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 11
Project: openjdk-jdk10   File: DOMValidatorHelper.java   View Source Code Vote up 5 votes
private void fillNamespaceContext() {
    if (fRoot != null) {
        Node currentNode = fRoot.getParentNode();
        while (currentNode != null) {
            if (Node.ELEMENT_NODE == currentNode.getNodeType()) {
                NamedNodeMap attributes = currentNode.getAttributes();
                final int attrCount = attributes.getLength();
                for (int i = 0; i < attrCount; ++i) {
                    Attr attr = (Attr) attributes.item(i);
                    String value = attr.getValue();
                    if (value == null) {
                        value = XMLSymbols.EMPTY_STRING;
                    }
                    fillQName(fAttributeQName, attr);
                    // REVISIT: Should we be looking at non-namespace attributes
                    // for additional mappings? Should we detect illegal namespace
                    // declarations and exclude them from the context? -- mrglavas
                    if (fAttributeQName.uri == NamespaceContext.XMLNS_URI) {
                        // process namespace attribute
                        if (fAttributeQName.prefix == XMLSymbols.PREFIX_XMLNS) {
                            declarePrefix0(fAttributeQName.localpart, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);
                        }
                        else {
                            declarePrefix0(XMLSymbols.EMPTY_STRING, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);
                        }
                    }
                }

            }
            currentNode = currentNode.getParentNode();
        }
    }
}
 
Example 12
Project: dpdirect   File: SchemaLoader.java   View Source Code Vote up 5 votes
/**
 * Gets a named attribute value of a node if one exists.
 * 
 * @param attrName the attribute local name. The attribute object has not been constructed at this point.
 * @param parentNode the parent Node object, allows a string representation of a relative path snippet of the
 *           attribute to be constructed.
 * @return the value of the attribute if it exists.
 */
public String getAttributeValue(String attrName,
                                Node parentNode) {
   String value = null;
   int maxDepth = maxValuePathDepth;

   for (int i = 0; i < maxDepth; i++) {
      if (attributeValueMap.containsKey(attrName)) {
         List<String> currentValue = attributeValueMap.get(attrName);
         if (currentValue != null && currentValue.size() > 1) {
            // multiple values - remove to cycle through
            if (currentValue.get(0) != null) {
               value = (String) currentValue.remove(0);
               currentValue.add(null);
            }
         }
         else if (currentValue != null && currentValue.size() > 0) {
            value = (String) currentValue.get(0);
         }
      }
      // construct a string representation of a path snippet of the
      // attribute to attempt a more granular match. A more granular
      // match will overwrite a less granular match.
      if (parentNode != null) {
         String parentNodeName = getNodeName(parentNode);
         attrName = parentNodeName + "." + attrName;
         parentNode = parentNode.getParentNode();
      }
      else {
         break;
      }
   }
   return value;
}
 
Example 13
Project: OpenJSharp   File: TreeWalker.java   View Source Code Vote up 5 votes
/**
 * Perform a pre-order traversal non-recursive style.
 *
 * In contrast to the traverse() method this method will not issue
 * startDocument() and endDocument() events to the SAX listener.
 *
 * @param pos Node in the tree where to start traversal
 *
 * @throws TransformerException
 */
public void traverseFragment(Node pos) throws org.xml.sax.SAXException
{
  Node top = pos;

  while (null != pos)
  {
    startNode(pos);

    Node nextNode = pos.getFirstChild();

    while (null == nextNode)
    {
      endNode(pos);

      if (top.equals(pos))
        break;

      nextNode = pos.getNextSibling();

      if (null == nextNode)
      {
        pos = pos.getParentNode();

        if ((null == pos) || (top.equals(pos)))
        {
          if (null != pos)
            endNode(pos);

          nextNode = null;

          break;
        }
      }
    }

    pos = nextNode;
  }
}
 
Example 14
Project: pay   File: XmlUtils.java   View Source Code Vote up 5 votes
/**
 * Gets the immediately child elements list from the parent element.
 *
 * @param parent the parent element in the element tree
 * @param tagName the specified tag name
 * @return the NOT NULL immediately child elements list
 */
public static List<Element> getChildElements(Element parent, String tagName) {
    NodeList nodes = parent.getElementsByTagName(tagName);
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element && node.getParentNode() == parent) {
            elements.add((Element) node);
        }
    }

    return elements;
}
 
Example 15
Project: pScrobbler   File: DomElement.java   View Source Code Vote up 5 votes
/**
 * Returns the child element with the given name or <code>null</code> if it doesn't exist.
 *
 * @param name The child's name
 * @return the child element or <code>null</code>
 */
public DomElement getChild(String name) {
	NodeList list = e.getElementsByTagName(name);
	if (list.getLength() == 0)
		return null;
	for (int i = 0, j = list.getLength(); i < j; i++) {
		Node item = list.item(i);
		if (item.getParentNode() == e)
			return new DomElement((Element) item);
	}
	return null;
}
 
Example 16
Project: OpenJSharp   File: XMLUtils.java   View Source Code Vote up 4 votes
/**
 * This method is a tree-search to help prevent against wrapping attacks. It checks that no
 * two Elements have ID Attributes that match the "value" argument, if this is the case then
 * "false" is returned. Note that a return value of "true" does not necessarily mean that
 * a matching Element has been found, just that no wrapping attack has been detected.
 */
public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
    Node startParent = startNode.getParentNode();
    Node processedNode = null;
    Element foundElement = null;

    String id = value.trim();
    if (id.charAt(0) == '#') {
        id = id.substring(1);
    }

    while (startNode != null) {
        if (startNode.getNodeType() == Node.ELEMENT_NODE) {
            Element se = (Element) startNode;

            NamedNodeMap attributes = se.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr attr = (Attr)attributes.item(i);
                    if (attr.isId() && id.equals(attr.getValue())) {
                        if (foundElement == null) {
                            // Continue searching to find duplicates
                            foundElement = attr.getOwnerElement();
                        } else {
                            log.log(java.util.logging.Level.FINE, "Multiple elements with the same 'Id' attribute value!");
                            return false;
                        }
                    }
                }
            }
        }

        processedNode = startNode;
        startNode = startNode.getFirstChild();

        // no child, this node is done.
        if (startNode == null) {
            // close node processing, get sibling
            startNode = processedNode.getNextSibling();
        }

        // no more siblings, get parent, all children
        // of parent are processed.
        while (startNode == null) {
            processedNode = processedNode.getParentNode();
            if (processedNode == startParent) {
                return true;
            }
            // close parent node processing (processed node now)
            startNode = processedNode.getNextSibling();
        }
    }
    return true;
}
 
Example 17
Project: lams   File: HTMLOptionElementImpl.java   View Source Code Vote up 4 votes
private HTMLSelectElementImpl getSelect() {
    Node parent = getParentNode();
    while (parent != null && !("select".equalsIgnoreCase( parent.getNodeName() ))) parent = parent.getParentNode();
    return (HTMLSelectElementImpl) parent;
}
 
Example 18
Project: OpenJSharp   File: XMLUtils.java   View Source Code Vote up 4 votes
/**
 * This is the work horse for {@link #circumventBug2650}.
 *
 * @param node
 * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
 * Namespace axis resolution is not XPath compliant </A>
 */
@SuppressWarnings("fallthrough")
private static void circumventBug2650internal(Node node) {
    Node parent = null;
    Node sibling = null;
    final String namespaceNs = Constants.NamespaceSpecNS;
    do {
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE :
            Element element = (Element) node;
            if (!element.hasChildNodes()) {
                break;
            }
            if (element.hasAttributes()) {
                NamedNodeMap attributes = element.getAttributes();
                int attributesLength = attributes.getLength();

                for (Node child = element.getFirstChild(); child!=null;
                    child = child.getNextSibling()) {

                    if (child.getNodeType() != Node.ELEMENT_NODE) {
                        continue;
                    }
                    Element childElement = (Element) child;

                    for (int i = 0; i < attributesLength; i++) {
                        Attr currentAttr = (Attr) attributes.item(i);
                        if (!namespaceNs.equals(currentAttr.getNamespaceURI())) {
                            continue;
                        }
                        if (childElement.hasAttributeNS(namespaceNs,
                                                        currentAttr.getLocalName())) {
                            continue;
                        }
                        childElement.setAttributeNS(namespaceNs,
                                                    currentAttr.getName(),
                                                    currentAttr.getNodeValue());
                    }
                }
            }
        case Node.ENTITY_REFERENCE_NODE :
        case Node.DOCUMENT_NODE :
            parent = node;
            sibling = node.getFirstChild();
            break;
        }
        while ((sibling == null) && (parent != null)) {
            sibling = parent.getNextSibling();
            parent = parent.getParentNode();
        }
        if (sibling == null) {
            return;
        }

        node = sibling;
        sibling = node.getNextSibling();
    } while (true);
}
 
Example 19
Project: OpenJSharp   File: XMLUtils.java   View Source Code Vote up 4 votes
/**
 * This method is a tree-search to help prevent against wrapping attacks. It checks that no other
 * Element than the given "knownElement" argument has an ID attribute that matches the "value"
 * argument, which is the ID value of "knownElement". If this is the case then "false" is returned.
 */
public static boolean protectAgainstWrappingAttack(
    Node startNode, Element knownElement, String value
) {
    Node startParent = startNode.getParentNode();
    Node processedNode = null;

    String id = value.trim();
    if (id.charAt(0) == '#') {
        id = id.substring(1);
    }

    while (startNode != null) {
        if (startNode.getNodeType() == Node.ELEMENT_NODE) {
            Element se = (Element) startNode;

            NamedNodeMap attributes = se.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr attr = (Attr)attributes.item(i);
                    if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) {
                        log.log(java.util.logging.Level.FINE, "Multiple elements with the same 'Id' attribute value!");
                        return false;
                    }
                }
            }
        }

        processedNode = startNode;
        startNode = startNode.getFirstChild();

        // no child, this node is done.
        if (startNode == null) {
            // close node processing, get sibling
            startNode = processedNode.getNextSibling();
        }

        // no more siblings, get parent, all children
        // of parent are processed.
        while (startNode == null) {
            processedNode = processedNode.getParentNode();
            if (processedNode == startParent) {
                return true;
            }
            // close parent node processing (processed node now)
            startNode = processedNode.getNextSibling();
        }
    }
    return true;
}
 
Example 20
Project: openjdk-jdk10   File: DOM3TreeWalker.java   View Source Code Vote up 4 votes
/**
 * Perform a pre-order traversal non-recursive style.
 *
 * Note that TreeWalker assumes that the subtree is intended to represent
 * a complete (though not necessarily well-formed) document and, during a
 * traversal, startDocument and endDocument will always be issued to the
 * SAX listener.
 *
 * @param pos Node in the tree where to start traversal
 *
 * @throws TransformerException
 */
public void traverse(Node pos) throws org.xml.sax.SAXException {
    this.fSerializer.startDocument();

    // Determine if the Node is a DOM Level 3 Core Node.
    if (pos.getNodeType() != Node.DOCUMENT_NODE) {
        Document ownerDoc = pos.getOwnerDocument();
        if (ownerDoc != null
            && ownerDoc.getImplementation().hasFeature("Core", "3.0")) {
            fIsLevel3DOM = true;
        }
    } else {
        if (((Document) pos)
            .getImplementation()
            .hasFeature("Core", "3.0")) {
            fIsLevel3DOM = true;
        }
    }

    if (fSerializer instanceof LexicalHandler) {
        fLexicalHandler = ((LexicalHandler) this.fSerializer);
    }

    if (fFilter != null)
        fWhatToShowFilter = fFilter.getWhatToShow();

    Node top = pos;

    while (null != pos) {
        startNode(pos);

        Node nextNode = null;

        nextNode = pos.getFirstChild();

        while (null == nextNode) {
            endNode(pos);

            if (top.equals(pos))
                break;

            nextNode = pos.getNextSibling();

            if (null == nextNode) {
                pos = pos.getParentNode();

                if ((null == pos) || (top.equals(pos))) {
                    if (null != pos)
                        endNode(pos);

                    nextNode = null;

                    break;
                }
            }
        }

        pos = nextNode;
    }
    this.fSerializer.endDocument();
}