Search in sources :

Example 6 with XMLString

use of org.apache.xml.utils.XMLString in project robovm by robovm.

the class FunctionDef1Arg method getArg0AsNumber.

/**
   * Execute the first argument expression that is expected to return a
   * number.  If the argument is null, then get the number value from the
   * current context node.
   *
   * @param xctxt Runtime XPath context.
   *
   * @return The number value of the first argument, or the number value of the
   *         current context node if the first argument is null.
   *
   * @throws javax.xml.transform.TransformerException if an error occurs while
   *                                   executing the argument expression.
   */
protected double getArg0AsNumber(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    if (null == m_arg0) {
        int currentNode = xctxt.getCurrentNode();
        if (DTM.NULL == currentNode)
            return 0;
        else {
            DTM dtm = xctxt.getDTM(currentNode);
            XMLString str = dtm.getStringValue(currentNode);
            return str.toDouble();
        }
    } else
        return m_arg0.execute(xctxt).num();
}
Also used : DTM(org.apache.xml.dtm.DTM) XMLString(org.apache.xml.utils.XMLString)

Example 7 with XMLString

use of org.apache.xml.utils.XMLString in project robovm by robovm.

the class FuncSubstringAfter method execute.

/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    XMLString s1 = m_arg0.execute(xctxt).xstr();
    XMLString s2 = m_arg1.execute(xctxt).xstr();
    int index = s1.indexOf(s2);
    return (-1 == index) ? XString.EMPTYSTRING : (XString) s1.substring(index + s2.length());
}
Also used : XMLString(org.apache.xml.utils.XMLString)

Example 8 with XMLString

use of org.apache.xml.utils.XMLString in project robovm by robovm.

the class FuncSum method execute.

/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    DTMIterator nodes = m_arg0.asIterator(xctxt, xctxt.getCurrentNode());
    double sum = 0.0;
    int pos;
    while (DTM.NULL != (pos = nodes.nextNode())) {
        DTM dtm = nodes.getDTM(pos);
        XMLString s = dtm.getStringValue(pos);
        if (null != s)
            sum += s.toDouble();
    }
    nodes.detach();
    return new XNumber(sum);
}
Also used : XNumber(org.apache.xpath.objects.XNumber) DTM(org.apache.xml.dtm.DTM) XMLString(org.apache.xml.utils.XMLString) DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 9 with XMLString

use of org.apache.xml.utils.XMLString 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 10 with XMLString

use of org.apache.xml.utils.XMLString in project nokogiri by sparklemotion.

the class DOM2DTMExt method getStringValue.

/**
     * Get the string-value of a node as a String object
     * (see http://www.w3.org/TR/xpath#data-model
     * for the definition of a node's string-value).
     *
     * @param nodeHandle The node ID.
     *
     * @return A string object that represents the string-value of the given node.
     */
public XMLString getStringValue(int nodeHandle) {
    int type = getNodeType(nodeHandle);
    Node node = getNode(nodeHandle);
    // directly.
    if (DTM.ELEMENT_NODE == type || DTM.DOCUMENT_NODE == type || DTM.DOCUMENT_FRAGMENT_NODE == type) {
        FastStringBuffer buf = StringBufferPool.get();
        String s;
        try {
            getNodeData(node, buf);
            s = (buf.length() > 0) ? buf.toString() : "";
        } finally {
            StringBufferPool.free(buf);
        }
        return m_xstrf.newstr(s);
    } else if (TEXT_NODE == type || CDATA_SECTION_NODE == type) {
        // If this is a DTM text node, it may be made of multiple DOM text
        // nodes -- including navigating into Entity References. DOM2DTM
        // records the first node in the sequence and requires that we
        // pick up the others when we retrieve the DTM node's value.
        //
        // %REVIEW% DOM Level 3 is expected to add a "whole text"
        // retrieval method which performs this function for us.
        FastStringBuffer buf = StringBufferPool.get();
        while (node != null) {
            buf.append(node.getNodeValue());
            node = logicalNextDOMTextNode(node);
        }
        String s = (buf.length() > 0) ? buf.toString() : "";
        StringBufferPool.free(buf);
        return m_xstrf.newstr(s);
    } else
        return m_xstrf.newstr(node.getNodeValue());
}
Also used : FastStringBuffer(org.apache.xml.utils.FastStringBuffer) Node(org.w3c.dom.Node) XMLString(org.apache.xml.utils.XMLString)

Aggregations

XMLString (org.apache.xml.utils.XMLString)32 DTM (org.apache.xml.dtm.DTM)12 DTMIterator (org.apache.xml.dtm.DTMIterator)10 XNodeSet (org.apache.xpath.objects.XNodeSet)6 XObject (org.apache.xpath.objects.XObject)6 Node (org.w3c.dom.Node)6 Hashtable (java.util.Hashtable)4 TransformerException (javax.xml.transform.TransformerException)4 FastStringBuffer (org.apache.xml.utils.FastStringBuffer)3 Vector (java.util.Vector)2 KeyDeclaration (org.apache.xalan.templates.KeyDeclaration)2 KeyManager (org.apache.xalan.transformer.KeyManager)2 TransformerImpl (org.apache.xalan.transformer.TransformerImpl)2 NodeConsumer (org.apache.xml.utils.NodeConsumer)2 QName (org.apache.xml.utils.QName)2 WrappedRuntimeException (org.apache.xml.utils.WrappedRuntimeException)2 XMLStringFactory (org.apache.xml.utils.XMLStringFactory)2 Expression (org.apache.xpath.Expression)2 NodeSetDTM (org.apache.xpath.NodeSetDTM)2 XPathContext (org.apache.xpath.XPathContext)2