Java Code Examples for org.w3c.dom.traversal.NodeIterator#nextNode()

The following examples show how to use org.w3c.dom.traversal.NodeIterator#nextNode() . 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: NodeSet.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Copy NodeList members into this nodelist, adding in
 * document order.  Null references are not added.
 *
 * @param iterator NodeIterator which yields the nodes to be added.
 * @throws RuntimeException thrown if this NodeSet is not of
 * a mutable type.
 */
public void addNodes(NodeIterator iterator)
{

  if (!m_mutable)
    throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!");

  if (null != iterator)  // defensive to fix a bug that Sanjiva reported.
  {
    Node obj;

    while (null != (obj = iterator.nextNode()))
    {
      addElement(obj);
    }
  }

  // checkDups();
}
 
Example 2
Source File: XPathImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
private Object getResultAsType( XObject resultObject, QName returnType )
    throws javax.xml.transform.TransformerException {
    // XPathConstants.STRING
    if ( returnType.equals( XPathConstants.STRING ) ) {
        return resultObject.str();
    }
    // XPathConstants.NUMBER
    if ( returnType.equals( XPathConstants.NUMBER ) ) {
        return new Double ( resultObject.num());
    }
    // XPathConstants.BOOLEAN
    if ( returnType.equals( XPathConstants.BOOLEAN ) ) {
        return new Boolean( resultObject.bool());
    }
    // XPathConstants.NODESET ---ORdered, UNOrdered???
    if ( returnType.equals( XPathConstants.NODESET ) ) {
        return resultObject.nodelist();
    }
    // XPathConstants.NODE
    if ( returnType.equals( XPathConstants.NODE ) ) {
        NodeIterator ni = resultObject.nodeset();
        //Return the first node, or null
        return ni.nextNode();
    }
    String fmsg = XSLMessages.createXPATHMessage(
            XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
            new Object[] { returnType.toString()});
    throw new IllegalArgumentException( fmsg );
}
 
Example 3
Source File: NodeSet.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copy NodeList members into this nodelist, adding in
 * document order.  If a node is null, don't add it.
 *
 * @param iterator NodeIterator which yields the nodes to be added.
 * @param support The XPath runtime context.
 * @throws RuntimeException thrown if this NodeSet is not of
 * a mutable type.
 */
public void addNodesInDocOrder(NodeIterator iterator, XPathContext support)
{

  if (!m_mutable)
    throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!");

  Node node;

  while (null != (node = iterator.nextNode()))
  {
    addNodeInDocOrder(node, support);
  }
}
 
Example 4
Source File: XPathResultImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
         * The value of this single node result, which may be <code>null</code>.
     * @exception XPathException
     *   TYPE_ERR: raised if <code>resultType</code> is not
     *   <code>ANY_UNORDERED_NODE_TYPE</code> or
     *   <code>FIRST_ORDERED_NODE_TYPE</code>.
     *
         * @see org.w3c.dom.xpath.XPathResult#getSingleNodeValue()
         */
        public Node getSingleNodeValue() throws XPathException {

                if ((m_resultType != ANY_UNORDERED_NODE_TYPE) &&
                    (m_resultType != FIRST_ORDERED_NODE_TYPE)) {
                                String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_SINGLENODE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});
                                throw new XPathException(XPathException.TYPE_ERR,fmsg);
//                              "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a single node.
//                               This method applies only to types ANY_UNORDERED_NODE_TYPE and FIRST_ORDERED_NODE_TYPE."
            }

                NodeIterator result = null;
                try {
                        result = m_resultObj.nodeset();
                } catch (TransformerException te) {
                        throw new XPathException(XPathException.TYPE_ERR,te.getMessage());
                }

        if (null == result) return null;

        Node node = result.nextNode();

        // Wrap "namespace node" in an XPathNamespace
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }
        }
 
Example 5
Source File: XPathImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private Object getResultAsType( XObject resultObject, QName returnType )
    throws javax.xml.transform.TransformerException {
    // XPathConstants.STRING
    if ( returnType.equals( XPathConstants.STRING ) ) {
        return resultObject.str();
    }
    // XPathConstants.NUMBER
    if ( returnType.equals( XPathConstants.NUMBER ) ) {
        return new Double ( resultObject.num());
    }
    // XPathConstants.BOOLEAN
    if ( returnType.equals( XPathConstants.BOOLEAN ) ) {
        return new Boolean( resultObject.bool());
    }
    // XPathConstants.NODESET ---ORdered, UNOrdered???
    if ( returnType.equals( XPathConstants.NODESET ) ) {
        return resultObject.nodelist();
    }
    // XPathConstants.NODE
    if ( returnType.equals( XPathConstants.NODE ) ) {
        NodeIterator ni = resultObject.nodeset();
        //Return the first node, or null
        return ni.nextNode();
    }
    String fmsg = XSLMessages.createXPATHMessage(
            XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
            new Object[] { returnType.toString()});
    throw new IllegalArgumentException( fmsg );
}
 
Example 6
Source File: XPathAPI.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Use an XPath string to select a single node.
 * XPath namespace prefixes are resolved from the namespaceNode.
 *
 * @param contextNode The node to start searching from.
 * @param str A valid XPath string.
 * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 * @return The first node found that matches the XPath, or null.
 *
 * @throws TransformerException
 */
public static Node selectSingleNode(
        Node contextNode, String str, Node namespaceNode)
          throws TransformerException
{

  // Have the XObject return its result as a NodeSetDTM.
  NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);

  // Return the first node, or null
  return nl.nextNode();
}
 
Example 7
Source File: XPathResultImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
         * The value of this single node result, which may be <code>null</code>.
     * @exception XPathException
     *   TYPE_ERR: raised if <code>resultType</code> is not
     *   <code>ANY_UNORDERED_NODE_TYPE</code> or
     *   <code>FIRST_ORDERED_NODE_TYPE</code>.
     *
         * @see org.w3c.dom.xpath.XPathResult#getSingleNodeValue()
         */
        public Node getSingleNodeValue() throws XPathException {

                if ((m_resultType != ANY_UNORDERED_NODE_TYPE) &&
                    (m_resultType != FIRST_ORDERED_NODE_TYPE)) {
                                String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_SINGLENODE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});
                                throw new XPathException(XPathException.TYPE_ERR,fmsg);
//                              "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a single node.
//                               This method applies only to types ANY_UNORDERED_NODE_TYPE and FIRST_ORDERED_NODE_TYPE."
            }

                NodeIterator result = null;
                try {
                        result = m_resultObj.nodeset();
                } catch (TransformerException te) {
                        throw new XPathException(XPathException.TYPE_ERR,te.getMessage());
                }

        if (null == result) return null;

        Node node = result.nextNode();

        // Wrap "namespace node" in an XPathNamespace
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }
        }
 
Example 8
Source File: XPathResultImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
         * The value of this single node result, which may be <code>null</code>.
     * @exception XPathException
     *   TYPE_ERR: raised if <code>resultType</code> is not
     *   <code>ANY_UNORDERED_NODE_TYPE</code> or
     *   <code>FIRST_ORDERED_NODE_TYPE</code>.
     *
         * @see org.w3c.dom.xpath.XPathResult#getSingleNodeValue()
         */
        public Node getSingleNodeValue() throws XPathException {

                if ((m_resultType != ANY_UNORDERED_NODE_TYPE) &&
                    (m_resultType != FIRST_ORDERED_NODE_TYPE)) {
                                String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_SINGLENODE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});
                                throw new XPathException(XPathException.TYPE_ERR,fmsg);
//                              "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a single node.
//                               This method applies only to types ANY_UNORDERED_NODE_TYPE and FIRST_ORDERED_NODE_TYPE."
            }

                NodeIterator result = null;
                try {
                        result = m_resultObj.nodeset();
                } catch (TransformerException te) {
                        throw new XPathException(XPathException.TYPE_ERR,te.getMessage());
                }

        if (null == result) return null;

        Node node = result.nextNode();

        // Wrap "namespace node" in an XPathNamespace
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }
        }
 
Example 9
Source File: CachedXPathAPI.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Use an XPath string to select a single node.
 * XPath namespace prefixes are resolved from the namespaceNode.
 *
 * @param contextNode The node to start searching from.
 * @param str A valid XPath string.
 * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 * @return The first node found that matches the XPath, or null.
 *
 * @throws TransformerException
 */
public  Node selectSingleNode(
        Node contextNode, String str, Node namespaceNode)
          throws TransformerException
{

  // Have the XObject return its result as a NodeSetDTM.
  NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);

  // Return the first node, or null
  return nl.nextNode();
}
 
Example 10
Source File: CachedXPathAPI.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Use an XPath string to select a single node.
 * XPath namespace prefixes are resolved from the namespaceNode.
 *
 * @param contextNode The node to start searching from.
 * @param str A valid XPath string.
 * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 * @return The first node found that matches the XPath, or null.
 *
 * @throws TransformerException
 */
public  Node selectSingleNode(
        Node contextNode, String str, Node namespaceNode)
          throws TransformerException
{

  // Have the XObject return its result as a NodeSetDTM.
  NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);

  // Return the first node, or null
  return nl.nextNode();
}
 
Example 11
Source File: NodeSet.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copy NodeList members into this nodelist, adding in
 * document order.  If a node is null, don't add it.
 *
 * @param iterator NodeIterator which yields the nodes to be added.
 * @param support The XPath runtime context.
 * @throws RuntimeException thrown if this NodeSet is not of
 * a mutable type.
 */
public void addNodesInDocOrder(NodeIterator iterator, XPathContext support)
{

  if (!m_mutable)
    throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!");

  Node node;

  while (null != (node = iterator.nextNode()))
  {
    addNodeInDocOrder(node, support);
  }
}
 
Example 12
Source File: XPathAPI.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Use an XPath string to select a single node.
 * XPath namespace prefixes are resolved from the namespaceNode.
 *
 * @param contextNode The node to start searching from.
 * @param str A valid XPath string.
 * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 * @return The first node found that matches the XPath, or null.
 *
 * @throws TransformerException
 */
public static Node selectSingleNode(
        Node contextNode, String str, Node namespaceNode)
          throws TransformerException
{

  // Have the XObject return its result as a NodeSetDTM.
  NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);

  // Return the first node, or null
  return nl.nextNode();
}
 
Example 13
Source File: NodeSetDTM.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Create a NodeSetDTM, and copy the members of the
 * given DTMIterator into it.
 *
 * @param iterator Iterator which yields Nodes to be made members of the new set.
 */
public NodeSetDTM(NodeIterator iterator, XPathContext xctxt)
{

  super();

  Node node;
  m_manager = xctxt.getDTMManager();

  while (null != (node = iterator.nextNode()))
  {
    int handle = xctxt.getDTMHandleFromNode(node);
    addNodeInDocOrder(handle, xctxt);
  }
}
 
Example 14
Source File: NodeSet.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copy NodeList members into this nodelist, adding in
 * document order.  If a node is null, don't add it.
 *
 * @param iterator NodeIterator which yields the nodes to be added.
 * @param support The XPath runtime context.
 * @throws RuntimeException thrown if this NodeSet is not of
 * a mutable type.
 */
public void addNodesInDocOrder(NodeIterator iterator, XPathContext support)
{

  if (!m_mutable)
    throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!");

  Node node;

  while (null != (node = iterator.nextNode()))
  {
    addNodeInDocOrder(node, support);
  }
}
 
Example 15
Source File: CachedXPathAPI.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Use an XPath string to select a single node.
 * XPath namespace prefixes are resolved from the namespaceNode.
 *
 * @param contextNode The node to start searching from.
 * @param str A valid XPath string.
 * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
 * @return The first node found that matches the XPath, or null.
 *
 * @throws TransformerException
 */
public  Node selectSingleNode(
        Node contextNode, String str, Node namespaceNode)
          throws TransformerException
{

  // Have the XObject return its result as a NodeSetDTM.
  NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);

  // Return the first node, or null
  return nl.nextNode();
}
 
Example 16
Source File: XPathResultImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
         * The value of this single node result, which may be <code>null</code>.
     * @exception XPathException
     *   TYPE_ERR: raised if <code>resultType</code> is not
     *   <code>ANY_UNORDERED_NODE_TYPE</code> or
     *   <code>FIRST_ORDERED_NODE_TYPE</code>.
     *
         * @see org.w3c.dom.xpath.XPathResult#getSingleNodeValue()
         */
        public Node getSingleNodeValue() throws XPathException {

                if ((m_resultType != ANY_UNORDERED_NODE_TYPE) &&
                    (m_resultType != FIRST_ORDERED_NODE_TYPE)) {
                                String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_SINGLENODE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});
                                throw new XPathException(XPathException.TYPE_ERR,fmsg);
//                              "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a single node.
//                               This method applies only to types ANY_UNORDERED_NODE_TYPE and FIRST_ORDERED_NODE_TYPE."
            }

                NodeIterator result = null;
                try {
                        result = m_resultObj.nodeset();
                } catch (TransformerException te) {
                        throw new XPathException(XPathException.TYPE_ERR,te.getMessage());
                }

        if (null == result) return null;

        Node node = result.nextNode();

        // Wrap "namespace node" in an XPathNamespace
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }
        }
 
Example 17
Source File: XPathExpressionImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
private Object getResultAsType( XObject resultObject, QName returnType )
    throws javax.xml.transform.TransformerException {
    // XPathConstants.STRING
    if ( returnType.equals( XPathConstants.STRING ) ) {
        return resultObject.str();
    }
    // XPathConstants.NUMBER
    if ( returnType.equals( XPathConstants.NUMBER ) ) {
        return new Double ( resultObject.num());
    }
    // XPathConstants.BOOLEAN
    if ( returnType.equals( XPathConstants.BOOLEAN ) ) {
        return new Boolean( resultObject.bool());
    }
    // XPathConstants.NODESET ---ORdered, UNOrdered???
    if ( returnType.equals( XPathConstants.NODESET ) ) {
        return resultObject.nodelist();
    }
    // XPathConstants.NODE
    if ( returnType.equals( XPathConstants.NODE ) ) {
        NodeIterator ni = resultObject.nodeset();
        //Return the first node, or null
        return ni.nextNode();
    }
    // If isSupported check is already done then the execution path
    // shouldn't come here. Being defensive
    String fmsg = XSLMessages.createXPATHMessage(
            XPATHErrorResources.ER_UNSUPPORTED_RETURN_TYPE,
            new Object[] { returnType.toString()});
    throw new IllegalArgumentException ( fmsg );
}
 
Example 18
Source File: XPathResultImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
         * The value of this single node result, which may be <code>null</code>.
     * @exception XPathException
     *   TYPE_ERR: raised if <code>resultType</code> is not
     *   <code>ANY_UNORDERED_NODE_TYPE</code> or
     *   <code>FIRST_ORDERED_NODE_TYPE</code>.
     *
         * @see org.w3c.dom.xpath.XPathResult#getSingleNodeValue()
         */
        public Node getSingleNodeValue() throws XPathException {

                if ((m_resultType != ANY_UNORDERED_NODE_TYPE) &&
                    (m_resultType != FIRST_ORDERED_NODE_TYPE)) {
                                String fmsg = XPATHMessages.createXPATHMessage(XPATHErrorResources.ER_CANT_CONVERT_TO_SINGLENODE, new Object[] {m_xpath.getPatternString(), getTypeString(m_resultType)});
                                throw new XPathException(XPathException.TYPE_ERR,fmsg);
//                              "The XPathResult of XPath expression {0} has an XPathResultType of {1} which cannot be converted to a single node.
//                               This method applies only to types ANY_UNORDERED_NODE_TYPE and FIRST_ORDERED_NODE_TYPE."
            }

                NodeIterator result = null;
                try {
                        result = m_resultObj.nodeset();
                } catch (TransformerException te) {
                        throw new XPathException(XPathException.TYPE_ERR,te.getMessage());
                }

        if (null == result) return null;

        Node node = result.nextNode();

        // Wrap "namespace node" in an XPathNamespace
        if (isNamespaceNode(node)) {
            return new XPathNamespaceImpl(node);
        } else {
            return node;
        }
        }
 
Example 19
Source File: NodeSet.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Copy NodeList members into this nodelist, adding in
 * document order.  If a node is null, don't add it.
 *
 * @param iterator NodeIterator which yields the nodes to be added.
 * @param support The XPath runtime context.
 * @throws RuntimeException thrown if this NodeSet is not of
 * a mutable type.
 */
public void addNodesInDocOrder(NodeIterator iterator, XPathContext support)
{

  if (!m_mutable)
    throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESET_NOT_MUTABLE, null)); //"This NodeSet is not mutable!");

  Node node;

  while (null != (node = iterator.nextNode()))
  {
    addNodeInDocOrder(node, support);
  }
}
 
Example 20
Source File: NodeSetDTM.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Create a NodeSetDTM, and copy the members of the
 * given DTMIterator into it.
 *
 * @param iterator Iterator which yields Nodes to be made members of the new set.
 */
public NodeSetDTM(NodeIterator iterator, XPathContext xctxt)
{

  super();

  Node node;
  m_manager = xctxt.getDTMManager();

  while (null != (node = iterator.nextNode()))
  {
    int handle = xctxt.getDTMHandleFromNode(node);
    addNodeInDocOrder(handle, xctxt);
  }
}