Search in sources :

Example 31 with XMLString

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

the class FuncSubstring 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();
    double start = m_arg1.execute(xctxt).num();
    int lenOfS1 = s1.length();
    XMLString substr;
    if (lenOfS1 <= 0)
        return XString.EMPTYSTRING;
    else {
        int startIndex;
        if (Double.isNaN(start)) {
            // Double.MIN_VALUE doesn't work with math below 
            // so just use a big number and hope I never get caught.
            start = -1000000;
            startIndex = 0;
        } else {
            start = Math.round(start);
            startIndex = (start > 0) ? (int) start - 1 : 0;
        }
        if (null != m_arg2) {
            double len = m_arg2.num(xctxt);
            int end = (int) (Math.round(len) + start) - 1;
            // Normalize end index.
            if (end < 0)
                end = 0;
            else if (end > lenOfS1)
                end = lenOfS1;
            if (startIndex > lenOfS1)
                startIndex = lenOfS1;
            substr = s1.substring(startIndex, end);
        } else {
            if (startIndex > lenOfS1)
                startIndex = lenOfS1;
            substr = s1.substring(startIndex);
        }
    }
    // cast semi-safe
    return (XString) substr;
}
Also used : XString(org.apache.xpath.objects.XString) XMLString(org.apache.xml.utils.XMLString)

Example 32 with XMLString

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

the class DOM2DTM 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