Package com.ibm.commons.xml.xpath

Examples of com.ibm.commons.xml.xpath.NodeListImpl


    }
    public XResult evaluateXPath(NodeList nodeList, Object xpath, NamespaceContext nsContext) throws XPathException {
        try {
            if(nodeList.getLength()>0) {
                XResult result = null;
                NodeListImpl nodes = null;
                PrefixResolver pr = nsContext!=null ? new NSResolver(nsContext) : null;
                for( int i=0; i<nodeList.getLength(); i++ ) {
                    XObject res = XPathAPI.eval(nodeList.item(i),(String)xpath,pr);
                    switch(res.getType()) {
                        case XObject.CLASS_BOOLEAN: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti-1$
                            }
                            result = new XResultUtils.BooleanValue(res.bool());
                        } break;
                        case XObject.CLASS_NUMBER: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.1-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.1-1$
                            }
                            result = new XResultUtils.NumberValue(res.num());
                        } break;
                        case XObject.CLASS_STRING: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.2-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.2-1$
                            }
                            result = new XResultUtils.StringValue(res.str());
                        } break;
                        case XObject.CLASS_NODESET: {
                            if(result!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.3-1$
                            }
                            NodeList nl = res.nodelist();
                            int len = nl.getLength();
                            if(len>0) {
                                if(nodes==null) {
                                    nodes = new NodeListImpl();
                                }
                                for( int j=0; j<nl.getLength(); j++ ) {
                                    nodes.add(nl.item(j));
                                }
                            }
                        }
                    }
                }
                if(result!=null) {
                    return result;
                }
                if(nodes!=null) {
                    int len = nodes.getLength();
                    if(len==0) {
                        return XResultUtils.emptyResult;
                    } else if(len==1) {
                        return new XResultUtils.XMLNode(nodes.item(0));
                    } else {
                        return new XResultUtils.XMLNodeList(nodes);
                    }
                }
            }
View Full Code Here


    }
    public XResult evaluateXPath(NodeList nodeList, Object xpath, NamespaceContext nsContext) throws XPathException {
        try {
            if(nodeList.getLength()>0) {
                XResult result = null;
                NodeListImpl nodes = null;
                PrefixResolver pr = nsContext!=null ? new NSResolver(nsContext) : null;
                for( int i=0; i<nodeList.getLength(); i++ ) {
                    XObject res = XPathAPI.eval(nodeList.item(i),(String)xpath,pr);
                    switch(res.getType()) {
                        case XObject.CLASS_BOOLEAN: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti-1$
                            }
                            result = new XResultUtils.BooleanValue(res.bool());
                        } break;
                        case XObject.CLASS_NUMBER: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.1-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.1-1$
                            }
                            result = new XResultUtils.NumberValue(res.num());
                        } break;
                        case XObject.CLASS_STRING: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.2-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.2-1$
                            }
                            result = new XResultUtils.StringValue(res.str());
                        } break;
                        case XObject.CLASS_NODESET: {
                            if(result!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.3-1$
                            }
                            NodeList nl = res.nodelist();
                            int len = nl.getLength();
                            if(len>0) {
                                if(nodes==null) {
                                    nodes = new NodeListImpl();
                                }
                                for( int j=0; j<nl.getLength(); j++ ) {
                                    nodes.add(nl.item(j));
                                }
                            }
                        }
                    }
                }
                if(result!=null) {
                    return result;
                }
                if(nodes!=null) {
                    int len = nodes.getLength();
                    if(len==0) {
                        return XResultUtils.emptyResult;
                    } else if(len==1) {
                        return new XResultUtils.XMLNode(nodes.item(0));
                    } else {
                        return new XResultUtils.XMLNodeList(nodes);
                    }
                }
            }
View Full Code Here

    }
    public XResult evaluateXPath(NodeList nodeList, Object xpath, NamespaceContext nsContext) throws XPathException {
        try {
            if(nodeList.getLength()>0) {
                XResult result = null;
                NodeListImpl nodes = null;
                PrefixResolver pr = nsContext!=null ? new NSResolver(nsContext) : null;
                for( int i=0; i<nodeList.getLength(); i++ ) {
                    XObject res = XPathAPI.eval(nodeList.item(i),(String)xpath,pr);
                    switch(res.getType()) {
                        case XObject.CLASS_BOOLEAN: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti-1$
                            }
                            result = new XResultUtils.BooleanValue(res.bool());
                        } break;
                        case XObject.CLASS_NUMBER: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.1-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.1-1$
                            }
                            result = new XResultUtils.NumberValue(res.num());
                        } break;
                        case XObject.CLASS_STRING: {
                            if(nodes!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.2-1$
                            }
                            if(result!=null) {
                                throw new XPathException(null,"XPath exception cannot return multiple values"); // $NLS-AbstractXercesDriver.AnXPathExceptioncannotreturnmulti.2-1$
                            }
                            result = new XResultUtils.StringValue(res.str());
                        } break;
                        case XObject.CLASS_NODESET: {
                            if(result!=null) {
                                throw new XPathException(null,"XPath result cannot contain both values and nodes"); // $NLS-AbstractXercesDriver.AnXPathresultcannotcontainbothval.3-1$
                            }
                            NodeList nl = res.nodelist();
                            int len = nl.getLength();
                            if(len>0) {
                                if(nodes==null) {
                                    nodes = new NodeListImpl();
                                }
                                for( int j=0; j<nl.getLength(); j++ ) {
                                    nodes.add(nl.item(j));
                                }
                            }
                        }
                    }
                }
                if(result!=null) {
                    return result;
                }
                if(nodes!=null) {
                    int len = nodes.getLength();
                    if(len==0) {
                        return XResultUtils.emptyResult;
                    } else if(len==1) {
                        return new XResultUtils.XMLNode(nodes.item(0));
                    } else {
                        return new XResultUtils.XMLNodeList(nodes);
                    }
                }
            }
View Full Code Here

     * @return a created node or <code>null</code> if an event of failure
     * @see AbstractSimpleExpression#createPart(Object, Part)
     */
    protected Object createPart(Object node, Part part, NamespaceContext namespaceContext) throws XPathException {
        if (node instanceof NodeListImpl) {
            NodeListImpl nodeList = (NodeListImpl)node;
            if (nodeList.getLength() == 1) {
                node = nodeList.get(0);
            }
        }
        if (node == null || !(node instanceof Node)) {
            throw new XPathException(null,"Error creating part:" + part.toString() // $NLS-XmlSimpleExpression.ErrorcreatePART-1$
                    + " Node must be of the type " + Node.class // $NLS-XmlSimpleExpression.Nodemustbeofthetype-1$
View Full Code Here

     *            not used
     * @see AbstractSimpleExpression#deletePart(Object, Part)
     */
    protected void deletePart(Object node, Part part) {
        if (node instanceof NodeListImpl) {
            NodeListImpl nodeList = (NodeListImpl)node;
            if (nodeList.getLength() == 1) {
                node = nodeList.get(0);
            }
        }
        if (node instanceof Node) {
            Node nodeObj = (Node) node;
            Node parentNode = nodeObj.getParentNode();
View Full Code Here

            return null;
        }
       
        // In case of a list, then applies the part on each element
        if( node instanceof NodeListImpl ) {
            NodeListImpl result = new NodeListImpl(16);
            for (Iterator iter = ((NodeListImpl) node).iterator(); iter.hasNext();) {
                Object res = evaluatePart(iter.next(), part, namespaceContext);
                if (res instanceof NodeListImpl) {
                    result.addAll((NodeListImpl) res);
                } else if (res != null) {
                    result.add(res);
                }
            }
            return (result.isEmpty()) ? null : result;
        }

        Node nodeObj = (Node) node;
       
        if (part.getName().equals(".")) {
            return nodeObj;
        }
       
        if (part.getName().equals("..")) {
            return nodeObj.getParentNode();
        }
       
        if (part instanceof AttributePart) {
            if (node instanceof Element) {
                if (StringUtil.isEmpty(part.getPrefix())) {
                    return ((Element)node).getAttributeNode(part.getName());
                } else {
                    String nsURI = getNamespaceURI(namespaceContext,part.getPrefix());
                    return ((Element)node).getAttributeNodeNS(nsURI, part.getName());
                }
            }
            return null;
        }

        // process child nodes
        if (node instanceof Document) {
            Document doc = (Document) node;
            Element elem = doc.getDocumentElement();
            if (elem != null && equalsName(namespaceContext, part, elem)) {
                return elem;
            }
            return null;
        }
       
        int index = 0;
        if (part instanceof IndexedElementPart) {
            index = ((IndexedElementPart) part).getIndex();
            if (index < 1) {
                String message = "Invalid predicate {0}, XPath predicates are 1-based."; // $NLS-XmlSimpleExpression.Invalidpredicate0XPathpredicatesa-1$
                message = StringUtil.format(message, new Object[] { Integer.valueOf(index) });
                throw new IllegalArgumentException(message);
            }
        }

        NodeList content = nodeObj.getChildNodes();
        Node resultObject = null;
        NodeListImpl resultList = null;
        int found = 0;
        int len = content.getLength();
        for (int i = 0; i < len; i++) {
            Node childNode = content.item(i);
            if (equalsName(namespaceContext, part, childNode)) {
                found++;
                if(index==0 || index==found) {
                    if(resultObject==null) {
                        resultObject = childNode;
                    } else {
                        if(resultList==null) {
                            resultList = new NodeListImpl();
                            resultList.add(resultObject);
                        }
                        resultList.add(childNode);
                    }
                }
            }
        }
        if (resultList!=null) {
View Full Code Here

        if (node == null) {
            throw new XPathException(null, "Cannot set a value on an null XPath result" ); // $NLS-XmlSimpleExpression.CannotsetavalueonannullXPathresul-1$
        }
       
        if (node instanceof NodeListImpl) {
            NodeListImpl nodeList = (NodeListImpl)node;
            if (nodeList.getLength() == 1) {
                node = nodeList.get(0);
            }
            else {
                throw new XPathException(null, "Cannot set a value on a list of elements" ); // $NLS-XmlSimpleExpression.Cannotsetavalueonalistofelements-1$
            }
        }
View Full Code Here

    /**
     * Find a element by name.
     * @return the element found, or null if it is not available
     */
    public static NodeList getElementsByName( Element parent, String name ){
      NodeListImpl result = new NodeListImpl();
      NodeList l = parent.getChildNodes();
      int count = l.getLength();
      for( int i=0; i<count; i++ ) {
        Node n = l.item(i);
        if(n.getNodeType()==Node.ELEMENT_NODE) {
          Element e = (Element)n;
              String localName = e.getLocalName();
              if (localName == null) {
                  if( name.equals(e.getNodeName()) ) {
                    result.add(e);
                  }
              }
        }
      }
      return result;
View Full Code Here

    /**
     * Find a element by name.
     * @return the element found, or null if it is not available
     */
    public static NodeList getElementsByNameNS( Element parent, String namespaceURI, String name ){
      NodeListImpl result = new NodeListImpl();
      NodeList l = parent.getChildNodes();
      int count = l.getLength();
      for( int i=0; i<count; i++ ) {
        Node n = l.item(i);
        if(n.getNodeType()==Node.ELEMENT_NODE) {
          Element e = (Element)n;
              String localName = e.getLocalName();
              if (localName == null) {
                  if( name.equals(e.getNodeName()) ) {
                    result.add(e);
                  }
              }
              if (namespaceURI!=null && name.equals(localName)) {
                  String elemNsURI = e.getNamespaceURI();
                  if (namespaceURI.equals(elemNsURI)) {
                    result.add(e);
                  }
              }
        }
      }
      return result;
View Full Code Here

     * @return NodeList - matching elements that are children of element.
     */
    public static NodeList getChildElementsByTagNameNS(Element element, String uri, String tag) {
        NodeList found = element.getElementsByTagNameNS(uri, tag);
       
        NodeListImpl children = new NodeListImpl();
       
        for (int i = 0; i < found.getLength(); i++) {
            Node node = found.item(i);
           
            Node parentNode = node.getParentNode();
           
            if (parentNode.equals(element)) {
                children.add(node);
            }
        }
       
        return children;
    }
View Full Code Here

TOP

Related Classes of com.ibm.commons.xml.xpath.NodeListImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.