Search in sources :

Example 1 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project robovm by robovm.

the class WalkerFactory method newDTMIterator.

/**
   * Create a new LocPathIterator iterator.  The exact type of iterator
   * returned is based on an analysis of the XPath operations.
   *
   * @param compiler non-null reference to compiler object that has processed
   *                 the XPath operations into an opcode map.
   * @param opPos The position of the operation code for this itterator.
   *
   * @return non-null reference to a LocPathIterator or derivative.
   *
   * @throws javax.xml.transform.TransformerException
   */
public static DTMIterator newDTMIterator(Compiler compiler, int opPos, boolean isTopLevel) throws javax.xml.transform.TransformerException {
    int firstStepPos = OpMap.getFirstChildPos(opPos);
    int analysis = analyze(compiler, firstStepPos, 0);
    boolean isOneStep = isOneStep(analysis);
    DTMIterator iter;
    // Is the iteration a one-step attribute pattern (i.e. select="@foo")?
    if (isOneStep && walksSelfOnly(analysis) && isWild(analysis) && !hasPredicate(analysis)) {
        if (DEBUG_ITERATOR_CREATION)
            diagnoseIterator("SelfIteratorNoPredicate", analysis, compiler);
        // Then use a simple iteration of the attributes, with node test 
        // and predicate testing.
        iter = new SelfIteratorNoPredicate(compiler, opPos, analysis);
    } else // Is the iteration exactly one child step?
    if (walksChildrenOnly(analysis) && isOneStep) {
        // Does the pattern specify *any* child with no predicate? (i.e. select="child::node()".
        if (isWild(analysis) && !hasPredicate(analysis)) {
            if (DEBUG_ITERATOR_CREATION)
                diagnoseIterator("ChildIterator", analysis, compiler);
            // Use simple child iteration without any test.
            iter = new ChildIterator(compiler, opPos, analysis);
        } else {
            if (DEBUG_ITERATOR_CREATION)
                diagnoseIterator("ChildTestIterator", analysis, compiler);
            // Else use simple node test iteration with predicate test.
            iter = new ChildTestIterator(compiler, opPos, analysis);
        }
    } else // Is the iteration a one-step attribute pattern (i.e. select="@foo")?
    if (isOneStep && walksAttributes(analysis)) {
        if (DEBUG_ITERATOR_CREATION)
            diagnoseIterator("AttributeIterator", analysis, compiler);
        // Then use a simple iteration of the attributes, with node test 
        // and predicate testing.
        iter = new AttributeIterator(compiler, opPos, analysis);
    } else if (isOneStep && !walksFilteredList(analysis)) {
        if (!walksNamespaces(analysis) && (walksInDocOrder(analysis) || isSet(analysis, BIT_PARENT))) {
            if (false || DEBUG_ITERATOR_CREATION)
                diagnoseIterator("OneStepIteratorForward", analysis, compiler);
            // Then use a simple iteration of the attributes, with node test 
            // and predicate testing.
            iter = new OneStepIteratorForward(compiler, opPos, analysis);
        } else {
            if (false || DEBUG_ITERATOR_CREATION)
                diagnoseIterator("OneStepIterator", analysis, compiler);
            // Then use a simple iteration of the attributes, with node test 
            // and predicate testing.
            iter = new OneStepIterator(compiler, opPos, analysis);
        }
    } else // to work right.
    if (isOptimizableForDescendantIterator(compiler, firstStepPos, 0)) // && getStepCount(analysis) <= 3 
    // && walksDescendants(analysis) 
    // && walksSubtreeOnlyFromRootOrContext(analysis)
    {
        if (DEBUG_ITERATOR_CREATION)
            diagnoseIterator("DescendantIterator", analysis, compiler);
        iter = new DescendantIterator(compiler, opPos, analysis);
    } else {
        if (isNaturalDocOrder(compiler, firstStepPos, 0, analysis)) {
            if (false || DEBUG_ITERATOR_CREATION) {
                diagnoseIterator("WalkingIterator", analysis, compiler);
            }
            iter = new WalkingIterator(compiler, opPos, analysis, true);
        } else {
            //        return new MatchPatternIterator(compiler, opPos, analysis);
            if (DEBUG_ITERATOR_CREATION)
                diagnoseIterator("WalkingIteratorSorted", analysis, compiler);
            iter = new WalkingIteratorSorted(compiler, opPos, analysis, true);
        }
    }
    if (iter instanceof LocPathIterator)
        ((LocPathIterator) iter).setIsTopLevel(isTopLevel);
    return iter;
}
Also used : DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 2 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project robovm by robovm.

the class FunctionPattern method execute.

/**
   * Test a node to see if it matches the given node test.
   *
   * @param xctxt XPath runtime context.
   *
   * @return {@link org.apache.xpath.patterns.NodeTest#SCORE_NODETEST},
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_NONE},
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_NSWILD},
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_QNAME}, or
   *         {@link org.apache.xpath.patterns.NodeTest#SCORE_OTHER}.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    int context = xctxt.getCurrentNode();
    DTMIterator nl = m_functionExpr.asIterator(xctxt, context);
    XNumber score = SCORE_NONE;
    if (null != nl) {
        int n;
        while (DTM.NULL != (n = nl.nextNode())) {
            score = (n == context) ? SCORE_OTHER : SCORE_NONE;
            if (score == SCORE_OTHER) {
                context = n;
                break;
            }
        }
        nl.detach();
    }
    return score;
}
Also used : XNumber(org.apache.xpath.objects.XNumber) DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 3 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project robovm by robovm.

the class NotEqualComparator method compare.

/**
   * Tell if one object is less than the other.
   *
   * @param obj2 Object to compare this nodeset to
   * @param comparator Comparator to use
   *
   * @return See the comments below for each object type comparison 
   *
   * @throws javax.xml.transform.TransformerException
   */
public boolean compare(XObject obj2, Comparator comparator) throws javax.xml.transform.TransformerException {
    boolean result = false;
    int type = obj2.getType();
    if (XObject.CLASS_NODESET == type) {
        // %OPT% This should be XMLString based instead of string based...
        // From http://www.w3.org/TR/xpath: 
        // If both objects to be compared are node-sets, then the comparison 
        // will be true if and only if there is a node in the first node-set 
        // and a node in the second node-set such that the result of performing 
        // the comparison on the string-values of the two nodes is true.
        // Note this little gem from the draft:
        // NOTE: If $x is bound to a node-set, then $x="foo" 
        // does not mean the same as not($x!="foo"): the former 
        // is true if and only if some node in $x has the string-value 
        // foo; the latter is true if and only if all nodes in $x have 
        // the string-value foo.
        DTMIterator list1 = iterRaw();
        DTMIterator list2 = ((XNodeSet) obj2).iterRaw();
        int node1;
        java.util.Vector node2Strings = null;
        while (DTM.NULL != (node1 = list1.nextNode())) {
            XMLString s1 = getStringFromNode(node1);
            if (null == node2Strings) {
                int node2;
                while (DTM.NULL != (node2 = list2.nextNode())) {
                    XMLString s2 = getStringFromNode(node2);
                    if (comparator.compareStrings(s1, s2)) {
                        result = true;
                        break;
                    }
                    if (null == node2Strings)
                        node2Strings = new java.util.Vector();
                    node2Strings.addElement(s2);
                }
            } else {
                int n = node2Strings.size();
                for (int i = 0; i < n; i++) {
                    if (comparator.compareStrings(s1, (XMLString) node2Strings.elementAt(i))) {
                        result = true;
                        break;
                    }
                }
            }
        }
        list1.reset();
        list2.reset();
    } else if (XObject.CLASS_BOOLEAN == type) {
        // From http://www.w3.org/TR/xpath: 
        // If one object to be compared is a node-set and the other is a boolean, 
        // then the comparison will be true if and only if the result of 
        // performing the comparison on the boolean and on the result of 
        // converting the node-set to a boolean using the boolean function 
        // is true.
        double num1 = bool() ? 1.0 : 0.0;
        double num2 = obj2.num();
        result = comparator.compareNumbers(num1, num2);
    } else if (XObject.CLASS_NUMBER == type) {
        // From http://www.w3.org/TR/xpath: 
        // If one object to be compared is a node-set and the other is a number, 
        // then the comparison will be true if and only if there is a 
        // node in the node-set such that the result of performing the 
        // comparison on the number to be compared and on the result of 
        // converting the string-value of that node to a number using 
        // the number function is true. 
        DTMIterator list1 = iterRaw();
        double num2 = obj2.num();
        int node;
        while (DTM.NULL != (node = list1.nextNode())) {
            double num1 = getNumberFromNode(node);
            if (comparator.compareNumbers(num1, num2)) {
                result = true;
                break;
            }
        }
        list1.reset();
    } else if (XObject.CLASS_RTREEFRAG == type) {
        XMLString s2 = obj2.xstr();
        DTMIterator list1 = iterRaw();
        int node;
        while (DTM.NULL != (node = list1.nextNode())) {
            XMLString s1 = getStringFromNode(node);
            if (comparator.compareStrings(s1, s2)) {
                result = true;
                break;
            }
        }
        list1.reset();
    } else if (XObject.CLASS_STRING == type) {
        // From http://www.w3.org/TR/xpath: 
        // If one object to be compared is a node-set and the other is a 
        // string, then the comparison will be true if and only if there 
        // is a node in the node-set such that the result of performing 
        // the comparison on the string-value of the node and the other 
        // string is true. 
        XMLString s2 = obj2.xstr();
        DTMIterator list1 = iterRaw();
        int node;
        while (DTM.NULL != (node = list1.nextNode())) {
            XMLString s1 = getStringFromNode(node);
            if (comparator.compareStrings(s1, s2)) {
                result = true;
                break;
            }
        }
        list1.reset();
    } else {
        result = comparator.compareNumbers(this.num(), obj2.num());
    }
    return result;
}
Also used : XMLString(org.apache.xml.utils.XMLString) DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 4 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project j2objc by google.

the class XObjectFactory method create.

/**
   * Create the right XObject based on the type of the object passed.
   * This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and 
   * NodeIterators to the XSLT stylesheet as node-sets.
   *
   * @param val The java object which this object will wrap.
   * @param xctxt The XPath context.
   *
   * @return the right XObject based on the type of the object passed.
   */
public static XObject create(Object val, XPathContext xctxt) {
    XObject result;
    if (val instanceof XObject) {
        result = (XObject) val;
    } else if (val instanceof String) {
        result = new XString((String) val);
    } else if (val instanceof Boolean) {
        result = new XBoolean((Boolean) val);
    } else if (val instanceof Number) {
        result = new XNumber(((Number) val));
    } else if (val instanceof DTM) {
        DTM dtm = (DTM) val;
        try {
            int dtmRoot = dtm.getDocument();
            DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
            iter.setStartNode(dtmRoot);
            DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
            iterator.setRoot(dtmRoot, xctxt);
            result = new XNodeSet(iterator);
        } catch (Exception ex) {
            throw new org.apache.xml.utils.WrappedRuntimeException(ex);
        }
    } else if (val instanceof DTMAxisIterator) {
        DTMAxisIterator iter = (DTMAxisIterator) val;
        try {
            DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
            iterator.setRoot(iter.getStartNode(), xctxt);
            result = new XNodeSet(iterator);
        } catch (Exception ex) {
            throw new org.apache.xml.utils.WrappedRuntimeException(ex);
        }
    } else if (val instanceof DTMIterator) {
        result = new XNodeSet((DTMIterator) val);
    } else // might also implement a Node!
    if (val instanceof org.w3c.dom.Node) {
        result = new XNodeSetForDOM((org.w3c.dom.Node) val, xctxt);
    } else // also implement NodeList.
    if (val instanceof org.w3c.dom.NodeList) {
        result = new XNodeSetForDOM((org.w3c.dom.NodeList) val, xctxt);
    } else if (val instanceof org.w3c.dom.traversal.NodeIterator) {
        result = new XNodeSetForDOM((org.w3c.dom.traversal.NodeIterator) val, xctxt);
    } else {
        result = new XObject(val);
    }
    return result;
}
Also used : DTMIterator(org.apache.xml.dtm.DTMIterator) OneStepIterator(org.apache.xpath.axes.OneStepIterator) DTMAxisIterator(org.apache.xml.dtm.DTMAxisIterator) DTM(org.apache.xml.dtm.DTM)

Example 5 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project j2objc by google.

the class NotEqualComparator method compare.

/**
   * Tell if one object is less than the other.
   *
   * @param obj2 Object to compare this nodeset to
   * @param comparator Comparator to use
   *
   * @return See the comments below for each object type comparison 
   *
   * @throws javax.xml.transform.TransformerException
   */
public boolean compare(XObject obj2, Comparator comparator) throws javax.xml.transform.TransformerException {
    boolean result = false;
    int type = obj2.getType();
    if (XObject.CLASS_NODESET == type) {
        // %OPT% This should be XMLString based instead of string based...
        // From http://www.w3.org/TR/xpath: 
        // If both objects to be compared are node-sets, then the comparison 
        // will be true if and only if there is a node in the first node-set 
        // and a node in the second node-set such that the result of performing 
        // the comparison on the string-values of the two nodes is true.
        // Note this little gem from the draft:
        // NOTE: If $x is bound to a node-set, then $x="foo" 
        // does not mean the same as not($x!="foo"): the former 
        // is true if and only if some node in $x has the string-value 
        // foo; the latter is true if and only if all nodes in $x have 
        // the string-value foo.
        DTMIterator list1 = iterRaw();
        DTMIterator list2 = ((XNodeSet) obj2).iterRaw();
        int node1;
        java.util.Vector node2Strings = null;
        while (DTM.NULL != (node1 = list1.nextNode())) {
            XMLString s1 = getStringFromNode(node1);
            if (null == node2Strings) {
                int node2;
                while (DTM.NULL != (node2 = list2.nextNode())) {
                    XMLString s2 = getStringFromNode(node2);
                    if (comparator.compareStrings(s1, s2)) {
                        result = true;
                        break;
                    }
                    if (null == node2Strings)
                        node2Strings = new java.util.Vector();
                    node2Strings.addElement(s2);
                }
            } else {
                int n = node2Strings.size();
                for (int i = 0; i < n; i++) {
                    if (comparator.compareStrings(s1, (XMLString) node2Strings.elementAt(i))) {
                        result = true;
                        break;
                    }
                }
            }
        }
        list1.reset();
        list2.reset();
    } else if (XObject.CLASS_BOOLEAN == type) {
        // From http://www.w3.org/TR/xpath: 
        // If one object to be compared is a node-set and the other is a boolean, 
        // then the comparison will be true if and only if the result of 
        // performing the comparison on the boolean and on the result of 
        // converting the node-set to a boolean using the boolean function 
        // is true.
        double num1 = bool() ? 1.0 : 0.0;
        double num2 = obj2.num();
        result = comparator.compareNumbers(num1, num2);
    } else if (XObject.CLASS_NUMBER == type) {
        // From http://www.w3.org/TR/xpath: 
        // If one object to be compared is a node-set and the other is a number, 
        // then the comparison will be true if and only if there is a 
        // node in the node-set such that the result of performing the 
        // comparison on the number to be compared and on the result of 
        // converting the string-value of that node to a number using 
        // the number function is true. 
        DTMIterator list1 = iterRaw();
        double num2 = obj2.num();
        int node;
        while (DTM.NULL != (node = list1.nextNode())) {
            double num1 = getNumberFromNode(node);
            if (comparator.compareNumbers(num1, num2)) {
                result = true;
                break;
            }
        }
        list1.reset();
    } else if (XObject.CLASS_RTREEFRAG == type) {
        XMLString s2 = obj2.xstr();
        DTMIterator list1 = iterRaw();
        int node;
        while (DTM.NULL != (node = list1.nextNode())) {
            XMLString s1 = getStringFromNode(node);
            if (comparator.compareStrings(s1, s2)) {
                result = true;
                break;
            }
        }
        list1.reset();
    } else if (XObject.CLASS_STRING == type) {
        // From http://www.w3.org/TR/xpath: 
        // If one object to be compared is a node-set and the other is a 
        // string, then the comparison will be true if and only if there 
        // is a node in the node-set such that the result of performing 
        // the comparison on the string-value of the node and the other 
        // string is true. 
        XMLString s2 = obj2.xstr();
        DTMIterator list1 = iterRaw();
        int node;
        while (DTM.NULL != (node = list1.nextNode())) {
            XMLString s1 = getStringFromNode(node);
            if (comparator.compareStrings(s1, s2)) {
                result = true;
                break;
            }
        }
        list1.reset();
    } else {
        result = comparator.compareNumbers(this.num(), obj2.num());
    }
    return result;
}
Also used : XMLString(org.apache.xml.utils.XMLString) DTMIterator(org.apache.xml.dtm.DTMIterator)

Aggregations

DTMIterator (org.apache.xml.dtm.DTMIterator)46 DTM (org.apache.xml.dtm.DTM)23 XPathContext (org.apache.xpath.XPathContext)12 XObject (org.apache.xpath.objects.XObject)12 TransformerException (javax.xml.transform.TransformerException)11 XMLString (org.apache.xml.utils.XMLString)10 XNumber (org.apache.xpath.objects.XNumber)10 XNodeSet (org.apache.xpath.objects.XNodeSet)8 Vector (java.util.Vector)6 QName (org.apache.xml.utils.QName)6 SAXException (org.xml.sax.SAXException)6 SerializationHandler (org.apache.xml.serializer.SerializationHandler)5 Hashtable (java.util.Hashtable)4 IntStack (org.apache.xml.utils.IntStack)4 NodeSetDTM (org.apache.xpath.NodeSetDTM)4 SubContextList (org.apache.xpath.axes.SubContextList)4 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)2 KeyDeclaration (org.apache.xalan.templates.KeyDeclaration)2