Search in sources :

Example 21 with XMLString

use of org.apache.xml.utils.XMLString in project j2objc by google.

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)

Example 22 with XMLString

use of org.apache.xml.utils.XMLString in project j2objc by google.

the class DTMDocumentImpl 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) {
    // ###zaj - researching
    nodes.readSlot(nodeHandle, gotslot);
    int nodetype = gotslot[0] & 0xFF;
    String value = null;
    switch(nodetype) {
        case TEXT_NODE:
        case COMMENT_NODE:
        case CDATA_SECTION_NODE:
            value = m_char.getString(gotslot[2], gotslot[3]);
            break;
        case PROCESSING_INSTRUCTION_NODE:
        case ATTRIBUTE_NODE:
        case ELEMENT_NODE:
        case ENTITY_REFERENCE_NODE:
        default:
            break;
    }
    return m_xsf.newstr(value);
}
Also used : XMLString(org.apache.xml.utils.XMLString)

Example 23 with XMLString

use of org.apache.xml.utils.XMLString in project j2objc by google.

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 24 with XMLString

use of org.apache.xml.utils.XMLString in project j2objc by google.

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 25 with XMLString

use of org.apache.xml.utils.XMLString in project j2objc by google.

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)

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