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

The following examples show how to use org.w3c.dom.Node#ATTRIBUTE_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: DomUtil.java    From container with Apache License 2.0 7 votes vote down vote up
public static String getNodeAttribute(final String tagName, final String attrName, final NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        final Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            final NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                final Node data = childNodes.item(y);
                if (data.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if (data.getNodeName().equalsIgnoreCase(attrName)) {
                        return data.getNodeValue();
                    }
                }
            }
        }
    }
    return "";
}
 
Example 2
Source File: RangeImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns true IFF the given node can be contained by
 * a range.
 */
private boolean isLegalContainedNode( Node node )
{
        if ( node==null )
                return false;
        switch( node.getNodeType() )
        {
        case Node.DOCUMENT_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.ATTRIBUTE_NODE:
        case Node.ENTITY_NODE:
        case Node.NOTATION_NODE:
                return false;
        }
        return true;
}
 
Example 3
Source File: Xml.java    From RADL with Apache License 2.0 5 votes vote down vote up
private static boolean hasDifferentNamespaceThanParent(Node node) {
  if (node.getNamespaceURI() == null || isNamespaceNode(node)) {
    return false;
  }
  Node parent;
  if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
    parent = ((Attr)node).getOwnerElement();
  } else {
    parent = node.getParentNode();
  }
  if (parent == null) {
    return true;
  }
  return !node.getNamespaceURI().equals(parent.getNamespaceURI());
}
 
Example 4
Source File: XmlGetNodeType.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
protected cfData getXmlNodeTypeName(Node nodeData) {
	short type = nodeData.getNodeType();
	switch (type) {
	case Node.ATTRIBUTE_NODE:
		return new cfStringData("ATTRIBUTE_NODE");
	case Node.CDATA_SECTION_NODE:
		return new cfStringData("CDATA_SECTION_NODE");
	case Node.COMMENT_NODE:
		return new cfStringData("COMMENT_NODE");
	case Node.ELEMENT_NODE:
		return new cfStringData("ELEMENT_NODE");
	case Node.ENTITY_REFERENCE_NODE:
		return new cfStringData("ENTITY_REFERENCE_NODE");
	case Node.PROCESSING_INSTRUCTION_NODE:
		return new cfStringData("PROCESSING_INSTRUCTION_NODE");
	case Node.TEXT_NODE:
		return new cfStringData("TEXT_NODE");
	case Node.ENTITY_NODE:
		return new cfStringData("ENTITY_NODE");
	case Node.NOTATION_NODE:
		return new cfStringData("NOTATION_NODE");
	case Node.DOCUMENT_NODE:
		return new cfStringData("DOCUMENT_NODE");
	case Node.DOCUMENT_FRAGMENT_NODE:
		return new cfStringData("DOCUMENT_FRAGMENT_NODE");
	case Node.DOCUMENT_TYPE_NODE:
		return new cfStringData("DOCUMENT_TYPE_NODE");
	default:
		return cfNullData.NULL;
	}
}
 
Example 5
Source File: DOM2SAX.java    From jdk8u60 with GNU General Public License v2.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 6
Source File: DTMNodeProxy.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 *
 *
 * @see org.w3c.dom.Node
 */
@Override
public final Node getParentNode()
{

  if (getNodeType() == Node.ATTRIBUTE_NODE)
    return null;

  int newnode = dtm.getParent(node);

  return (newnode == DTM.NULL) ? null : dtm.getNode(newnode);
}
 
Example 7
Source File: DOMPrinter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 8
Source File: DOMPrinter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 9
Source File: DOMHelper.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test whether the given node is a namespace decl node. In DOM Level 2
 * this can be done in a namespace-aware manner, but in Level 1 DOMs
 * it has to be done by testing the node name.
 *
 * @param n Node to be examined.
 *
 * @return boolean -- true iff the node is an Attr whose name is
 * "xmlns" or has the "xmlns:" prefix.
 */
public boolean isNamespaceNode(Node n)
{

  if (Node.ATTRIBUTE_NODE == n.getNodeType())
  {
    String attrName = n.getNodeName();

    return (attrName.startsWith("xmlns:") || attrName.equals("xmlns"));
  }

  return false;
}
 
Example 10
Source File: DOMPrinter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void print(Node node) throws XMLStreamException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        visitDocument((Document) node);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        visitDocumentFragment((DocumentFragment) node);
        break;
    case Node.ELEMENT_NODE:
        visitElement((Element) node);
        break;
    case Node.TEXT_NODE:
        visitText((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        visitCDATASection((CDATASection) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        visitProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.ENTITY_REFERENCE_NODE:
        visitReference((EntityReference) node);
        break;
    case Node.COMMENT_NODE:
        visitComment((Comment) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        break;
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    default:
        throw new XMLStreamException("Unexpected DOM Node Type "
            + node.getNodeType()
        );
    }
}
 
Example 11
Source File: DOM2DTM.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieve the text content of a DOM subtree, appending it into a
 * user-supplied FastStringBuffer object. Note that attributes are
 * not considered part of the content of an element.
 * <p>
 * There are open questions regarding whitespace stripping.
 * Currently we make no special effort in that regard, since the standard
 * DOM doesn't yet provide DTD-based information to distinguish
 * whitespace-in-element-context from genuine #PCDATA. Note that we
 * should probably also consider xml:space if/when we address this.
 * DOM Level 3 may solve the problem for us.
 * <p>
 * %REVIEW% Actually, since this method operates on the DOM side of the
 * fence rather than the DTM side, it SHOULDN'T do
 * any special handling. The DOM does what the DOM does; if you want
 * DTM-level abstractions, use DTM-level methods.
 *
 * @param node Node whose subtree is to be walked, gathering the
 * contents of all Text or CDATASection nodes.
 * @param buf FastStringBuffer into which the contents of the text
 * nodes are to be concatenated.
 */
protected static void getNodeData(Node node, FastStringBuffer buf)
{

  switch (node.getNodeType())
  {
  case Node.DOCUMENT_FRAGMENT_NODE :
  case Node.DOCUMENT_NODE :
  case Node.ELEMENT_NODE :
  {
    for (Node child = node.getFirstChild(); null != child;
            child = child.getNextSibling())
    {
      getNodeData(child, buf);
    }
  }
  break;
  case Node.TEXT_NODE :
  case Node.CDATA_SECTION_NODE :
  case Node.ATTRIBUTE_NODE :  // Never a child but might be our starting node
    buf.append(node.getNodeValue());
    break;
  case Node.PROCESSING_INSTRUCTION_NODE :
    // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
    break;
  default :
    // ignore
    break;
  }
}
 
Example 12
Source File: FuncHere.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The here function returns a node-set containing the attribute or
 * processing instruction node or the parent element of the text node
 * that directly bears the XPath expression.  This expression results
 * in an error if the containing XPath expression does not appear in the
 * same XML document against which the XPath expression is being evaluated.
 *
 * @param xctxt
 * @return the xobject
 * @throws javax.xml.transform.TransformerException
 */
@Override
public XObject execute(XPathContext xctxt)
    throws javax.xml.transform.TransformerException {

    Node xpathOwnerNode = (Node) xctxt.getOwnerObject();

    if (xpathOwnerNode == null) {
        return null;
    }

    int xpathOwnerNodeDTM = xctxt.getDTMHandleFromNode(xpathOwnerNode);

    int currentNode = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(currentNode);
    int docContext = dtm.getDocument();

    if (DTM.NULL == docContext) {
        error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null);
    }

    {
        // check whether currentNode and the node containing the XPath expression
        // are in the same document
        Document currentDoc =
            XMLUtils.getOwnerDocument(dtm.getNode(currentNode));
        Document xpathOwnerDoc = XMLUtils.getOwnerDocument(xpathOwnerNode);

        if (currentDoc != xpathOwnerDoc) {
            throw new TransformerException(I18n.translate("xpath.funcHere.documentsDiffer"));
        }
    }

    XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
    NodeSetDTM nodeSet = nodes.mutableNodeset();

    {
        int hereNode = DTM.NULL;

        switch (dtm.getNodeType(xpathOwnerNodeDTM)) {

        case Node.ATTRIBUTE_NODE :
        case Node.PROCESSING_INSTRUCTION_NODE : {
            // returns a node-set containing the attribute /  processing instruction node
            hereNode = xpathOwnerNodeDTM;

            nodeSet.addNode(hereNode);

            break;
        }
        case Node.TEXT_NODE : {
            // returns a node-set containing the parent element of the
            // text node that directly bears the XPath expression
            hereNode = dtm.getParent(xpathOwnerNodeDTM);

            nodeSet.addNode(hereNode);

            break;
        }
        default :
            break;
        }
    }

    /** $todo$ Do I have to do this detach() call? */
    nodeSet.detach();

    return nodes;
}
 
Example 13
Source File: DOM2DTMdefaultNamespaceDeclarationNode.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
     * DOM Level 3 - Experimental:
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 14
Source File: AttrImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/** Default Constructor */
public AttrImpl() {
    nodeType = Node.ATTRIBUTE_NODE;
}
 
Example 15
Source File: DTMNodeProxy.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
     * DOM Level 3
     * Look up the namespace URI associated to the given prefix, starting from this node.
     * Use lookupNamespaceURI(null) to lookup the default namespace
     *
     * @param namespaceURI
     * @return th URI for the namespace
     * @since DOM Level 3
     */
    @Override
    public String lookupNamespaceURI(String specifiedPrefix) {
        short type = this.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE : {

                String namespace = this.getNamespaceURI();
                String prefix = this.getPrefix();
                if (namespace !=null) {
                    // REVISIT: is it possible that prefix is empty string?
                    if (specifiedPrefix== null && prefix==specifiedPrefix) {
                        // looking for default namespace
                        return namespace;
                    } else if (prefix != null && prefix.equals(specifiedPrefix)) {
                        // non default namespace
                        return namespace;
                    }
                }
                if (this.hasAttributes()) {
                    NamedNodeMap map = this.getAttributes();
                    int length = map.getLength();
                    for (int i=0;i<length;i++) {
                        Node attr = map.item(i);
                        String attrPrefix = attr.getPrefix();
                        String value = attr.getNodeValue();
                        namespace = attr.getNamespaceURI();
                        if (namespace !=null && namespace.equals("http://www.w3.org/2000/xmlns/")) {
                            // at this point we are dealing with DOM Level 2 nodes only
                            if (specifiedPrefix == null &&
                                attr.getNodeName().equals("xmlns")) {
                                // default namespace
                                return value;
                            } else if (attrPrefix !=null &&
                                       attrPrefix.equals("xmlns") &&
                                       attr.getLocalName().equals(specifiedPrefix)) {
                 // non default namespace
                                return value;
                            }
                        }
                    }
                }
                /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
                */

                return null;


            }
/*
        case Node.DOCUMENT_NODE : {
                return((NodeImpl)((Document)this).getDocumentElement()).lookupNamespaceURI(specifiedPrefix) ;
            }
*/
        case Node.ENTITY_NODE :
        case Node.NOTATION_NODE:
        case Node.DOCUMENT_FRAGMENT_NODE:
        case Node.DOCUMENT_TYPE_NODE:
            // type is unknown
            return null;
        case Node.ATTRIBUTE_NODE:{
                if (this.getOwnerElement().getNodeType() == Node.ELEMENT_NODE) {
                    return getOwnerElement().lookupNamespaceURI(specifiedPrefix);

                }
                return null;
            }
        default:{
           /*
                NodeImpl ancestor = (NodeImpl)getElementAncestor(this);
                if (ancestor != null) {
                    return ancestor.lookupNamespaceURI(specifiedPrefix);
                }
             */
                return null;
            }

        }
    }
 
Example 16
Source File: XMLHelper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Recursively called function that ensures all the visibly used namespaces referenced by the given Element or its
 * descendants are declared if they don't appear in the list of already resolved namespaces.
 * 
 * @param domElement the Element
 * @param upperNamespaceSearchBound the "root" element of the fragment where namespaces may be rooted
 * 
 * @throws XMLParserException thrown if a namespace prefix is encountered that can't be resolved to a namespace URI
 */
private static void rootNamespaces(Element domElement, Element upperNamespaceSearchBound) throws XMLParserException {
    String namespaceURI = null;
    String namespacePrefix = domElement.getPrefix();

    // Check if the namespace for this element is already declared on this element
    boolean nsDeclaredOnElement = false;
    if (namespacePrefix == null) {
        nsDeclaredOnElement = domElement.hasAttributeNS(null, XMLConstants.XMLNS_PREFIX);
    } else {
        nsDeclaredOnElement = domElement.hasAttributeNS(XMLConstants.XMLNS_NS, namespacePrefix);
    }

    if (!nsDeclaredOnElement) {
        // Namspace for element was not declared on the element itself, see if the namespace is declared on
        // an ancestral element within the subtree where namespaces much be rooted
        namespaceURI = lookupNamespaceURI(domElement, upperNamespaceSearchBound, namespacePrefix);

        if (namespaceURI == null) {
            // Namespace for the element is not declared on any ancestral nodes within the subtree where namespaces
            // must be rooted. Resolve the namespace from ancestors outside that subtree.
            namespaceURI = lookupNamespaceURI(upperNamespaceSearchBound, null, namespacePrefix);
            if (namespaceURI != null) {
                // Namespace resolved outside the subtree where namespaces must be declared so declare the namespace
                // on this element (within the subtree).
                appendNamespaceDeclaration(domElement, namespaceURI, namespacePrefix);
            } else {
                // Namespace couldn't be resolved from any ancestor. If the namespace prefix is null then the
                // element is simply in the undeclared default document namespace, which is fine. If it isn't null
                // then a namespace prefix, that hasn't properly been declared, is being used.
                if (namespacePrefix != null) {
                    throw new XMLParserException("Unable to resolve namespace prefix " + namespacePrefix
                            + " found on element " + getNodeQName(domElement));
                }
            }
        }
    }

    // Make sure all the attribute URIs are rooted here or have been rooted in an ancestor
    NamedNodeMap attributes = domElement.getAttributes();
    Node attributeNode;
    for (int i = 0; i < attributes.getLength(); i++) {
        namespacePrefix = null;
        namespaceURI = null;
        attributeNode = attributes.item(i);

        // Shouldn't need this check, but just to be safe, we have it
        if (attributeNode.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }

        namespacePrefix = attributeNode.getPrefix();
        if (!DatatypeHelper.isEmpty(namespacePrefix)) {
            // If it's the "xmlns" prefix then it is the namespace declaration,
            // don't try to look it up and redeclare it
            if (namespacePrefix.equals(XMLConstants.XMLNS_PREFIX)
                    || namespacePrefix.equals(XMLConstants.XML_PREFIX)) {
                continue;
            }

            // check to see if the namespace for the prefix has already been defined within the XML fragment
            namespaceURI = lookupNamespaceURI(domElement, upperNamespaceSearchBound, namespacePrefix);
            if (namespaceURI == null) {
                namespaceURI = lookupNamespaceURI(upperNamespaceSearchBound, null, namespacePrefix);
                if (namespaceURI == null) {
                    throw new XMLParserException("Unable to resolve namespace prefix " + namespacePrefix
                            + " found on attribute " + getNodeQName(attributeNode) + " found on element "
                            + getNodeQName(domElement));
                }

                appendNamespaceDeclaration(domElement, namespaceURI, namespacePrefix);
            }
        }
    }

    // Now for the child elements, we pass a copy of the resolved namespace list in order to
    // maintain proper scoping of namespaces.
    Element childNode = getFirstChildElement(domElement);
    while (childNode != null) {
        rootNamespaces(childNode, upperNamespaceSearchBound);
        childNode = getNextSiblingElement(childNode);
    }
}
 
Example 17
Source File: DOM2DTM.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
   * Retrieve the text content of a DOM subtree, appending it into a
   * user-supplied FastStringBuffer object. Note that attributes are
   * not considered part of the content of an element.
   * <p>
   * There are open questions regarding whitespace stripping.
   * Currently we make no special effort in that regard, since the standard
   * DOM doesn't yet provide DTD-based information to distinguish
   * whitespace-in-element-context from genuine #PCDATA. Note that we
   * should probably also consider xml:space if/when we address this.
   * DOM Level 3 may solve the problem for us.
   * <p>
   * %REVIEW% Note that as a DOM-level operation, it can be argued that this
   * routine _shouldn't_ perform any processing beyond what the DOM already
   * does, and that whitespace stripping and so on belong at the DTM level.
   * If you want a stripped DOM view, wrap DTM2DOM around DOM2DTM.
   *
   * @param node Node whose subtree is to be walked, gathering the
   * contents of all Text or CDATASection nodes.
   */
  protected static void dispatchNodeData(Node node,
                                         org.xml.sax.ContentHandler ch,
                                         int depth)
            throws org.xml.sax.SAXException
  {

    switch (node.getNodeType())
    {
    case Node.DOCUMENT_FRAGMENT_NODE :
    case Node.DOCUMENT_NODE :
    case Node.ELEMENT_NODE :
    {
      for (Node child = node.getFirstChild(); null != child;
              child = child.getNextSibling())
      {
        dispatchNodeData(child, ch, depth+1);
      }
    }
    break;
    case Node.PROCESSING_INSTRUCTION_NODE : // %REVIEW%
    case Node.COMMENT_NODE :
      if(0 != depth)
        break;
        // NOTE: Because this operation works in the DOM space, it does _not_ attempt
        // to perform Text Coalition. That should only be done in DTM space.
    case Node.TEXT_NODE :
    case Node.CDATA_SECTION_NODE :
    case Node.ATTRIBUTE_NODE :
      String str = node.getNodeValue();
      if(ch instanceof CharacterNodeHandler)
      {
        ((CharacterNodeHandler)ch).characters(node);
      }
      else
      {
        ch.characters(str.toCharArray(), 0, str.length());
      }
      break;
//    /* case Node.PROCESSING_INSTRUCTION_NODE :
//      // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);
//      break; */
    default :
      // ignore
      break;
    }
  }
 
Example 18
Source File: DOMHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtain the XPath-model parent of a DOM node -- ownerElement for Attrs,
 * parent for other nodes.
 * <p>
 * Background: The DOM believes that you must be your Parent's
 * Child, and thus Attrs don't have parents. XPath said that Attrs
 * do have their owning Element as their parent. This function
 * bridges the difference, either by using the DOM Level 2 ownerElement
 * function or by using a "silly and expensive function" in Level 1
 * DOMs.
 * <p>
 * (There's some discussion of future DOMs generalizing ownerElement
 * into ownerNode and making it work on all types of nodes. This
 * still wouldn't help the users of Level 1 or Level 2 DOMs)
 * <p>
 *
 * @param node Node whose XPath parent we want to obtain
 *
 * @return the parent of the node, or the ownerElement if it's an
 * Attr node, or null if the node is an orphan.
 *
 * @throws RuntimeException if the Document has no root element.
 * This can't arise if the Document was created
 * via the DOM Level 2 factory methods, but is possible if other
 * mechanisms were used to obtain it
 */
public static Node getParentOfNode(Node node) throws RuntimeException
{
  Node parent;
  short nodeType = node.getNodeType();

  if (Node.ATTRIBUTE_NODE == nodeType)
  {
    Document doc = node.getOwnerDocument();
        /*
    TBD:
    if(null == doc)
    {
      throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_CHILD_HAS_NO_OWNER_DOCUMENT, null));//"Attribute child does not have an owner document!");
    }
    */

        // Given how expensive the tree walk may be, we should first ask
        // whether this DOM can answer the question for us. The additional
        // test does slow down Level 1 DOMs slightly. DOMHelper2, which
        // is currently specialized for Xerces, assumes it can use the
        // Level 2 solution. We might want to have an intermediate stage,
        // which would assume DOM Level 2 but not assume Xerces.
        //
        // (Shouldn't have to check whether impl is null in a compliant DOM,
        // but let's be paranoid for a moment...)
        DOMImplementation impl=doc.getImplementation();
        if(impl!=null && impl.hasFeature("Core","2.0"))
        {
                parent=((Attr)node).getOwnerElement();
                return parent;
        }

        // DOM Level 1 solution, as fallback. Hugely expensive.

    Element rootElem = doc.getDocumentElement();

    if (null == rootElem)
    {
      throw new RuntimeException(
        XMLMessages.createXMLMessage(
          XMLErrorResources.ER_CHILD_HAS_NO_OWNER_DOCUMENT_ELEMENT,
          null));  //"Attribute child does not have an owner document element!");
    }

    parent = locateAttrParent(rootElem, node);

      }
  else
  {
    parent = node.getParentNode();

    // if((Node.DOCUMENT_NODE != nodeType) && (null == parent))
    // {
    //   throw new RuntimeException("Child does not have parent!");
    // }
  }

  return parent;
}
 
Example 19
Source File: DOM2Helper.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Get the XPath-model parent of a node. This version takes advantage of the
 * DOM Level 2 Attr.ownerElement() method; the base version we would
 * otherwise inherit is prepared to fall back on exhaustively walking the
 * document to find an Attr's parent.
 *
 * @param node Node to be examined
 *
 * @return the DOM parent of the input node, if there is one, or the
 * ownerElement if the input node is an Attr, or null if the node is a
 * Document, a DocumentFragment, or an orphan.
 */
public static Node getParentOfNode(Node node) {
    Node parent = node.getParentNode();
    if (parent == null && (Node.ATTRIBUTE_NODE == node.getNodeType())) {
        parent = ((Attr) node).getOwnerElement();
    }
    return parent;
}
 
Example 20
Source File: AttrImpl.java    From hottub with GNU General Public License v2.0 2 votes vote down vote up
/**
 * A short integer indicating what type of node this is. The named
 * constants for this value are defined in the org.w3c.dom.Node interface.
 */
public short getNodeType() {
    return Node.ATTRIBUTE_NODE;
}