Search in sources :

Example 76 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class KeyTable method getRefsTable.

/**
   * @return lazy initialized refs table associating evaluation of key function
   *         with a XNodeSet
   */
private Hashtable getRefsTable() {
    if (m_refsTable == null) {
        // initial capacity set to a prime number to improve hash performance
        m_refsTable = new Hashtable(89);
        KeyIterator ki = (KeyIterator) (m_keyNodes).getContainedIter();
        XPathContext xctxt = ki.getXPathContext();
        Vector keyDecls = getKeyDeclarations();
        int nKeyDecls = keyDecls.size();
        int currentNode;
        m_keyNodes.reset();
        while (DTM.NULL != (currentNode = m_keyNodes.nextNode())) {
            try {
                for (int keyDeclIdx = 0; keyDeclIdx < nKeyDecls; keyDeclIdx++) {
                    KeyDeclaration keyDeclaration = (KeyDeclaration) keyDecls.elementAt(keyDeclIdx);
                    XObject xuse = keyDeclaration.getUse().execute(xctxt, currentNode, ki.getPrefixResolver());
                    if (xuse.getType() != xuse.CLASS_NODESET) {
                        XMLString exprResult = xuse.xstr();
                        addValueInRefsTable(xctxt, exprResult, currentNode);
                    } else {
                        DTMIterator i = ((XNodeSet) xuse).iterRaw();
                        int currentNodeInUseClause;
                        while (DTM.NULL != (currentNodeInUseClause = i.nextNode())) {
                            DTM dtm = xctxt.getDTM(currentNodeInUseClause);
                            XMLString exprResult = dtm.getStringValue(currentNodeInUseClause);
                            addValueInRefsTable(xctxt, exprResult, currentNode);
                        }
                    }
                }
            } catch (TransformerException te) {
                throw new WrappedRuntimeException(te);
            }
        }
    }
    return m_refsTable;
}
Also used : Hashtable(java.util.Hashtable) DTMIterator(org.apache.xml.dtm.DTMIterator) XNodeSet(org.apache.xpath.objects.XNodeSet) KeyDeclaration(org.apache.xalan.templates.KeyDeclaration) XPathContext(org.apache.xpath.XPathContext) XMLString(org.apache.xml.utils.XMLString) DTM(org.apache.xml.dtm.DTM) Vector(java.util.Vector) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException) WrappedRuntimeException(org.apache.xml.utils.WrappedRuntimeException)

Example 77 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class NodeSorter method compare.

/**
   * Return the results of a compare of two nodes.
   * TODO: Optimize compare -- cache the getStringExpr results, key by m_selectPat + hash of node.
   *
   * @param n1 First node to use in compare
   * @param n2 Second node to use in compare
   * @param kIndex Index of NodeSortKey to use for sort
   * @param support XPath context to use
   *
   * @return The results of the compare of the two nodes.
   *
   * @throws TransformerException
   */
int compare(NodeCompareElem n1, NodeCompareElem n2, int kIndex, XPathContext support) throws TransformerException {
    int result = 0;
    NodeSortKey k = (NodeSortKey) m_keys.elementAt(kIndex);
    if (k.m_treatAsNumbers) {
        double n1Num, n2Num;
        if (kIndex == 0) {
            n1Num = ((Double) n1.m_key1Value).doubleValue();
            n2Num = ((Double) n2.m_key1Value).doubleValue();
        } else if (kIndex == 1) {
            n1Num = ((Double) n1.m_key2Value).doubleValue();
            n2Num = ((Double) n2.m_key2Value).doubleValue();
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
      double n1Num = (double)n1.m_keyValue[kIndex];
      double n2Num = (double)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1Num = r1.num();
            // Can't use NaN for compare. They are never equal. Use zero instead.
            // That way we can keep elements in document order. 
            //n1Num = Double.isNaN(d) ? 0.0 : d;
            n2Num = r2.num();
        //n2Num = Double.isNaN(d) ? 0.0 : d;
        }
        if ((n1Num == n2Num) && ((kIndex + 1) < m_keys.size())) {
            result = compare(n1, n2, kIndex + 1, support);
        } else {
            double diff;
            if (Double.isNaN(n1Num)) {
                if (Double.isNaN(n2Num))
                    diff = 0.0;
                else
                    diff = -1;
            } else if (Double.isNaN(n2Num))
                diff = 1;
            else
                diff = n1Num - n2Num;
            // process order parameter 
            result = (int) ((diff < 0.0) ? (k.m_descending ? 1 : -1) : (diff > 0.0) ? (k.m_descending ? -1 : 1) : 0);
        }
    } else // end treat as numbers 
    {
        CollationKey n1String, n2String;
        if (kIndex == 0) {
            n1String = (CollationKey) n1.m_key1Value;
            n2String = (CollationKey) n2.m_key1Value;
        } else if (kIndex == 1) {
            n1String = (CollationKey) n1.m_key2Value;
            n2String = (CollationKey) n2.m_key2Value;
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
        String n1String = (String)n1.m_keyValue[kIndex];
        String n2String = (String)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1String = k.m_col.getCollationKey(r1.str());
            n2String = k.m_col.getCollationKey(r2.str());
        }
        // Use collation keys for faster compare, but note that whitespaces 
        // etc... are treated differently from if we were comparing Strings.
        result = n1String.compareTo(n2String);
        //Process caseOrder parameter
        if (k.m_caseOrderUpper) {
            String tempN1 = n1String.getSourceString().toLowerCase();
            String tempN2 = n2String.getSourceString().toLowerCase();
            if (tempN1.equals(tempN2)) {
                //java defaults to upper case is greater.
                result = result == 0 ? 0 : -result;
            }
        }
        //Process order parameter
        if (k.m_descending) {
            result = -result;
        }
    }
    if (0 == result) {
        if ((kIndex + 1) < m_keys.size()) {
            result = compare(n1, n2, kIndex + 1, support);
        }
    }
    if (0 == result) {
        // I shouldn't have to do this except that there seems to 
        // be a glitch in the mergesort
        // if(r1.getType() == r1.CLASS_NODESET)
        // {
        // %OPT%
        DTM dtm = support.getDTM(n1.m_node);
        result = dtm.isNodeAfter(n1.m_node, n2.m_node) ? -1 : 1;
    // }
    }
    return result;
}
Also used : CollationKey(java.text.CollationKey) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 78 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class ElemNumber method getCountString.

/**
   * Given an XML source node, get the count according to the
   * parameters set up by the xsl:number attributes.
   * @param transformer non-null reference to the the current transform-time state.
   * @param sourceNode The source node being counted.
   *
   * @return The count of nodes
   *
   * @throws TransformerException
   */
String getCountString(TransformerImpl transformer, int sourceNode) throws TransformerException {
    long[] list = null;
    XPathContext xctxt = transformer.getXPathContext();
    CountersTable ctable = transformer.getCountersTable();
    if (null != m_valueExpr) {
        XObject countObj = m_valueExpr.execute(xctxt, sourceNode, this);
        //According to Errata E24
        double d_count = java.lang.Math.floor(countObj.num() + 0.5);
        if (Double.isNaN(d_count))
            return "NaN";
        else if (d_count < 0 && Double.isInfinite(d_count))
            return "-Infinity";
        else if (Double.isInfinite(d_count))
            return "Infinity";
        else if (d_count == 0)
            return "0";
        else {
            long count = (long) d_count;
            list = new long[1];
            list[0] = count;
        }
    } else {
        if (Constants.NUMBERLEVEL_ANY == m_level) {
            list = new long[1];
            list[0] = ctable.countNode(xctxt, this, sourceNode);
        } else {
            NodeVector ancestors = getMatchingAncestors(xctxt, sourceNode, Constants.NUMBERLEVEL_SINGLE == m_level);
            int lastIndex = ancestors.size() - 1;
            if (lastIndex >= 0) {
                list = new long[lastIndex + 1];
                for (int i = lastIndex; i >= 0; i--) {
                    int target = ancestors.elementAt(i);
                    list[lastIndex - i] = ctable.countNode(xctxt, this, target);
                }
            }
        }
    }
    return (null != list) ? formatNumberList(transformer, list, sourceNode) : "";
}
Also used : NodeVector(org.apache.xml.utils.NodeVector) CountersTable(org.apache.xalan.transformer.CountersTable) XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject)

Example 79 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class ElemVariable method execute.

/**
   * Execute a variable declaration and push it onto the variable stack.
   * @see <a href="http://www.w3.org/TR/xslt#variables">variables in XSLT Specification</a>
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    int sourceNode = transformer.getXPathContext().getCurrentNode();
    XObject var = getValue(transformer, sourceNode);
    // transformer.getXPathContext().getVarStack().pushVariable(m_qname, var);
    transformer.getXPathContext().getVarStack().setLocalVariable(m_index, var);
}
Also used : XObject(org.apache.xpath.objects.XObject)

Example 80 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class XUnresolvedVariable method execute.

/**
   * For support of literal objects in xpaths.
   *
   * @param xctxt The XPath execution context.
   *
   * @return This object.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    if (!m_doneEval) {
        this.m_transformer.getMsgMgr().error(xctxt.getSAXLocator(), XSLTErrorResources.ER_REFERENCING_ITSELF, new Object[] { ((ElemVariable) this.object()).getName().getLocalName() });
    }
    VariableStack vars = xctxt.getVarStack();
    // These three statements need to be combined into one operation.
    int currentFrame = vars.getStackFrame();
    //// vars.setStackFrame(m_varStackPos);
    ElemVariable velem = (ElemVariable) m_obj;
    try {
        m_doneEval = false;
        if (-1 != velem.m_frameSize)
            vars.link(velem.m_frameSize);
        XObject var = velem.getValue(m_transformer, m_context);
        m_doneEval = true;
        return var;
    } finally {
        if (-1 != velem.m_frameSize)
            vars.unlink(currentFrame);
    }
}
Also used : VariableStack(org.apache.xpath.VariableStack) XObject(org.apache.xpath.objects.XObject)

Aggregations

XObject (org.apache.xpath.objects.XObject)102 TransformerException (javax.xml.transform.TransformerException)24 DTM (org.apache.xml.dtm.DTM)24 XPathContext (org.apache.xpath.XPathContext)24 XNodeSet (org.apache.xpath.objects.XNodeSet)14 DTMIterator (org.apache.xml.dtm.DTMIterator)12 VariableStack (org.apache.xpath.VariableStack)10 Vector (java.util.Vector)8 QName (org.apache.xml.utils.QName)8 Expression (org.apache.xpath.Expression)8 Node (org.w3c.dom.Node)7 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 XMLString (org.apache.xml.utils.XMLString)6 org.apache.xpath (org.apache.xpath)6 XString (org.apache.xpath.objects.XString)6 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4 QName (javax.xml.namespace.QName)4 XPathFunction (javax.xml.xpath.XPathFunction)4 XPathFunctionException (javax.xml.xpath.XPathFunctionException)4