Search in sources :

Example 66 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class UnionChildIterator method acceptNode.

/**
   * Test whether a specified node is visible in the logical view of a
   * TreeWalker or NodeIterator. This function will be called by the
   * implementation of TreeWalker and NodeIterator; it is not intended to
   * be called directly from user code.
   * @param n  The node to check to see if it passes the filter or not.
   * @return  a constant to determine whether the node is accepted,
   *   rejected, or skipped, as defined  above .
   */
public short acceptNode(int n) {
    XPathContext xctxt = getXPathContext();
    try {
        xctxt.pushCurrentNode(n);
        for (int i = 0; i < m_nodeTests.length; i++) {
            PredicatedNodeTest pnt = m_nodeTests[i];
            XObject score = pnt.execute(xctxt, n);
            if (score != NodeTest.SCORE_NONE) {
                // Note that we are assuming there are no positional predicates!
                if (pnt.getPredicateCount() > 0) {
                    if (pnt.executePredicates(n, xctxt))
                        return DTMIterator.FILTER_ACCEPT;
                } else
                    return DTMIterator.FILTER_ACCEPT;
            }
        }
    } catch (javax.xml.transform.TransformerException se) {
        // TODO: Fix this.
        throw new RuntimeException(se.getMessage());
    } finally {
        xctxt.popCurrentNode();
    }
    return DTMIterator.FILTER_SKIP;
}
Also used : XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject)

Example 67 with XPathContext

use of org.apache.xpath.XPathContext in project nokogiri by sparklemotion.

the class XmlXpathContext method getXPathContext.

private XPathContext getXPathContext(final NokogiriXPathFunctionResolver fnResolver) {
    Node doc = context.getNode().getOwnerDocument();
    if (doc == null)
        doc = context.getNode();
    XPathContext xpathContext = (XPathContext) doc.getUserData(XPATH_CONTEXT);
    if (xpathContext == null) {
        xpathContext = newXPathContext(fnResolver);
        if (variableResolver == null) {
            // NOTE: only caching without variables - could be improved by more sophisticated caching
            doc.setUserData(XPATH_CONTEXT, xpathContext, null);
        }
    } else {
        Object owner = xpathContext.getOwnerObject();
        if ((owner == null && fnResolver == null) || (owner instanceof JAXPExtensionsProvider && ((JAXPExtensionsProvider) owner).hasSameResolver(fnResolver))) {
            // can be re-used assuming it has the same variable-stack (for now only cached if no variables)
            if (variableResolver == null)
                return xpathContext;
        }
        // otherwise we can not use the cached xpath-context
        xpathContext = newXPathContext(fnResolver);
    }
    if (variableResolver != null) {
        xpathContext.setVarStack(new JAXPVariableStack(variableResolver));
    }
    return xpathContext;
}
Also used : JAXPVariableStack(org.apache.xpath.jaxp.JAXPVariableStack) Node(org.w3c.dom.Node) JAXPExtensionsProvider(org.apache.xpath.jaxp.JAXPExtensionsProvider) XPathContext(org.apache.xpath.XPathContext) RubyObject(org.jruby.RubyObject) IRubyObject(org.jruby.runtime.builtin.IRubyObject) XObject(org.apache.xpath.objects.XObject)

Example 68 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class TransformerImpl method transformNode.

/**
   * Process the source node to the output result, if the
   * processor supports the "http://xml.org/trax/features/dom/input"
   * feature.
   * %REVIEW% Do we need a Node version of this?
   * @param node  The input source node, which can be any valid DTM node.
   *
   * @throws TransformerException
   */
public void transformNode(int node) throws TransformerException {
    //dml
    setExtensionsTable(getStylesheet());
    // Make sure we're not writing to the same output content handler.
    synchronized (m_serializationHandler) {
        m_hasBeenReset = false;
        XPathContext xctxt = getXPathContext();
        DTM dtm = xctxt.getDTM(node);
        try {
            pushGlobalVars(node);
            // ==========
            // Give the top-level templates a chance to pass information into 
            // the context (this is mainly for setting up tables for extensions).
            StylesheetRoot stylesheet = this.getStylesheet();
            int n = stylesheet.getGlobalImportCount();
            for (int i = 0; i < n; i++) {
                StylesheetComposed imported = stylesheet.getGlobalImport(i);
                int includedCount = imported.getIncludeCountComposed();
                for (int j = -1; j < includedCount; j++) {
                    Stylesheet included = imported.getIncludeComposed(j);
                    included.runtimeInit(this);
                    for (ElemTemplateElement child = included.getFirstChildElem(); child != null; child = child.getNextSiblingElem()) {
                        child.runtimeInit(this);
                    }
                }
            }
            // ===========        
            // System.out.println("Calling applyTemplateToNode - "+Thread.currentThread().getName());
            DTMIterator dtmIter = new org.apache.xpath.axes.SelfIteratorNoPredicate();
            dtmIter.setRoot(node, xctxt);
            xctxt.pushContextNodeList(dtmIter);
            try {
                this.applyTemplateToNode(null, null, node);
            } finally {
                xctxt.popContextNodeList();
            }
            // System.out.println("Done with applyTemplateToNode - "+Thread.currentThread().getName());
            if (null != m_serializationHandler) {
                m_serializationHandler.endDocument();
            }
        } catch (Exception se) {
            // SAXSourceLocator
            while (se instanceof org.apache.xml.utils.WrappedRuntimeException) {
                Exception e = ((org.apache.xml.utils.WrappedRuntimeException) se).getException();
                if (null != e)
                    se = e;
            }
            if (null != m_serializationHandler) {
                try {
                    if (se instanceof org.xml.sax.SAXParseException)
                        m_serializationHandler.fatalError((org.xml.sax.SAXParseException) se);
                    else if (se instanceof TransformerException) {
                        TransformerException te = ((TransformerException) se);
                        SAXSourceLocator sl = new SAXSourceLocator(te.getLocator());
                        m_serializationHandler.fatalError(new org.xml.sax.SAXParseException(te.getMessage(), sl, te));
                    } else {
                        m_serializationHandler.fatalError(new org.xml.sax.SAXParseException(se.getMessage(), new SAXSourceLocator(), se));
                    }
                } catch (Exception e) {
                }
            }
            if (se instanceof TransformerException) {
                m_errorHandler.fatalError((TransformerException) se);
            } else if (se instanceof org.xml.sax.SAXParseException) {
                m_errorHandler.fatalError(new TransformerException(se.getMessage(), new SAXSourceLocator((org.xml.sax.SAXParseException) se), se));
            } else {
                m_errorHandler.fatalError(new TransformerException(se));
            }
        } finally {
            this.reset();
        }
    }
}
Also used : StylesheetComposed(org.apache.xalan.templates.StylesheetComposed) Stylesheet(org.apache.xalan.templates.Stylesheet) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DTMIterator(org.apache.xml.dtm.DTMIterator) StylesheetRoot(org.apache.xalan.templates.StylesheetRoot) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 69 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class TransformerImpl method processSortKeys.

/**
   * Get the keys for the xsl:sort elements.
   * Note: Should this go into ElemForEach?
   *
   * @param foreach Valid ElemForEach element, not null.
   * @param sourceNodeContext The current node context in the source tree,
   * needed to evaluate the Attribute Value Templates.
   *
   * @return A Vector of NodeSortKeys, or null.
   *
   * @throws TransformerException
   * @xsl.usage advanced
   */
public Vector processSortKeys(ElemForEach foreach, int sourceNodeContext) throws TransformerException {
    Vector keys = null;
    XPathContext xctxt = m_xcontext;
    int nElems = foreach.getSortElemCount();
    if (nElems > 0)
        keys = new Vector();
    // March backwards, collecting the sort keys.
    for (int i = 0; i < nElems; i++) {
        ElemSort sort = foreach.getSortElem(i);
        String langString = (null != sort.getLang()) ? sort.getLang().evaluate(xctxt, sourceNodeContext, foreach) : null;
        String dataTypeString = sort.getDataType().evaluate(xctxt, sourceNodeContext, foreach);
        if (dataTypeString.indexOf(":") >= 0)
            System.out.println("TODO: Need to write the hooks for QNAME sort data type");
        else if (!(dataTypeString.equalsIgnoreCase(Constants.ATTRVAL_DATATYPE_TEXT)) && !(dataTypeString.equalsIgnoreCase(Constants.ATTRVAL_DATATYPE_NUMBER)))
            foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_DATATYPE, dataTypeString });
        boolean treatAsNumbers = ((null != dataTypeString) && dataTypeString.equals(Constants.ATTRVAL_DATATYPE_NUMBER)) ? true : false;
        String orderString = sort.getOrder().evaluate(xctxt, sourceNodeContext, foreach);
        if (!(orderString.equalsIgnoreCase(Constants.ATTRVAL_ORDER_ASCENDING)) && !(orderString.equalsIgnoreCase(Constants.ATTRVAL_ORDER_DESCENDING)))
            foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_ORDER, orderString });
        boolean descending = ((null != orderString) && orderString.equals(Constants.ATTRVAL_ORDER_DESCENDING)) ? true : false;
        AVT caseOrder = sort.getCaseOrder();
        boolean caseOrderUpper;
        if (null != caseOrder) {
            String caseOrderString = caseOrder.evaluate(xctxt, sourceNodeContext, foreach);
            if (!(caseOrderString.equalsIgnoreCase(Constants.ATTRVAL_CASEORDER_UPPER)) && !(caseOrderString.equalsIgnoreCase(Constants.ATTRVAL_CASEORDER_LOWER)))
                foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_CASEORDER, caseOrderString });
            caseOrderUpper = ((null != caseOrderString) && caseOrderString.equals(Constants.ATTRVAL_CASEORDER_UPPER)) ? true : false;
        } else {
            caseOrderUpper = false;
        }
        keys.addElement(new NodeSortKey(this, sort.getSelect(), treatAsNumbers, descending, langString, caseOrderUpper, foreach));
    }
    return keys;
}
Also used : XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject) Vector(java.util.Vector) NodeVector(org.apache.xml.utils.NodeVector) ElemSort(org.apache.xalan.templates.ElemSort) AVT(org.apache.xalan.templates.AVT)

Example 70 with XPathContext

use of org.apache.xpath.XPathContext in project robovm by robovm.

the class TransformerImpl method executeChildTemplates.

/**
   * Execute each of the children of a template element.
   *
   * @param elem The ElemTemplateElement that contains the children
   * that should execute.
   * @param shouldAddAttrs true if xsl:attributes should be executed.
   *
   * @throws TransformerException
   * @xsl.usage advanced
   */
public void executeChildTemplates(ElemTemplateElement elem, boolean shouldAddAttrs) throws TransformerException {
    // Does this element have any children?
    ElemTemplateElement t = elem.getFirstChildElem();
    if (null == t)
        return;
    if (elem.hasTextLitOnly() && m_optimizer) {
        char[] chars = ((ElemTextLiteral) t).getChars();
        try {
            // Have to push stuff on for tooling...
            this.pushElemTemplateElement(t);
            m_serializationHandler.characters(chars, 0, chars.length);
        } catch (SAXException se) {
            throw new TransformerException(se);
        } finally {
            this.popElemTemplateElement();
        }
        return;
    }
    //    // Check for infinite loops if we have to.
    //    boolean check = (m_stackGuard.m_recursionLimit > -1);
    //
    //    if (check)
    //      getStackGuard().push(elem, xctxt.getCurrentNode());
    XPathContext xctxt = m_xcontext;
    xctxt.pushSAXLocatorNull();
    int currentTemplateElementsTop = m_currentTemplateElements.size();
    m_currentTemplateElements.push(null);
    try {
        // each of them.
        for (; t != null; t = t.getNextSiblingElem()) {
            if (!shouldAddAttrs && t.getXSLToken() == Constants.ELEMNAME_ATTRIBUTE)
                continue;
            xctxt.setSAXLocator(t);
            m_currentTemplateElements.setElementAt(t, currentTemplateElementsTop);
            t.execute(this);
        }
    } catch (RuntimeException re) {
        TransformerException te = new TransformerException(re);
        te.setLocator(t);
        throw te;
    } finally {
        m_currentTemplateElements.pop();
        xctxt.popSAXLocator();
    }
// Check for infinite loops if we have to
//    if (check)
//      getStackGuard().pop();
}
Also used : ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) XPathContext(org.apache.xpath.XPathContext) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Aggregations

XPathContext (org.apache.xpath.XPathContext)73 TransformerException (javax.xml.transform.TransformerException)30 XObject (org.apache.xpath.objects.XObject)29 DTM (org.apache.xml.dtm.DTM)16 SAXException (org.xml.sax.SAXException)16 SerializationHandler (org.apache.xml.serializer.SerializationHandler)14 DTMIterator (org.apache.xml.dtm.DTMIterator)12 Vector (java.util.Vector)10 VariableStack (org.apache.xpath.VariableStack)8 NodeVector (org.apache.xml.utils.NodeVector)6 QName (org.apache.xml.utils.QName)6 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)4 StylesheetRoot (org.apache.xalan.templates.StylesheetRoot)4 IntStack (org.apache.xml.utils.IntStack)4 Expression (org.apache.xpath.Expression)4 XNodeSet (org.apache.xpath.objects.XNodeSet)4 XRTreeFrag (org.apache.xpath.objects.XRTreeFrag)4 Node (org.w3c.dom.Node)3 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2