Search in sources :

Example 41 with XPathContext

use of org.apache.xpath.XPathContext in project j2objc by google.

the class PredicatedNodeTest method acceptNode.

//=============== NodeFilter Implementation ===============
/**
   *  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 = m_lpi.getXPathContext();
    try {
        xctxt.pushCurrentNode(n);
        XObject score = execute(xctxt, n);
        // System.out.println("\n::acceptNode - score: "+score.num()+"::");
        if (score != NodeTest.SCORE_NONE) {
            if (getPredicateCount() > 0) {
                countProximityPosition(0);
                if (!executePredicates(n, xctxt))
                    return DTMIterator.FILTER_SKIP;
            }
            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 42 with XPathContext

use of org.apache.xpath.XPathContext in project j2objc by google.

the class TransformerImpl method applyTemplateToNode.

/**
   * Given an element and mode, find the corresponding
   * template and process the contents.
   *
   * @param xslInstruction The calling element.
   * @param template The template to use if xsl:for-each, current template for apply-imports, or null.
   * @param child The source context node.
   * @throws TransformerException
   * @return true if applied a template, false if not.
   * @xsl.usage advanced
   */
public // xsl:apply-templates or xsl:for-each
boolean applyTemplateToNode(// xsl:apply-templates or xsl:for-each
ElemTemplateElement xslInstruction, ElemTemplate template, int child) throws TransformerException {
    DTM dtm = m_xcontext.getDTM(child);
    short nodeType = dtm.getNodeType(child);
    boolean isDefaultTextRule = false;
    boolean isApplyImports = false;
    isApplyImports = ((xslInstruction == null) ? false : xslInstruction.getXSLToken() == Constants.ELEMNAME_APPLY_IMPORTS);
    if (null == template || isApplyImports) {
        int maxImportLevel, endImportLevel = 0;
        if (isApplyImports) {
            maxImportLevel = template.getStylesheetComposed().getImportCountComposed() - 1;
            endImportLevel = template.getStylesheetComposed().getEndImportCountComposed();
        } else {
            maxImportLevel = -1;
        }
        // We want to match -no- templates. See bugzilla bug 1170.
        if (isApplyImports && (maxImportLevel == -1)) {
            template = null;
        } else {
            // Find the XSL template that is the best match for the 
            // element.        
            XPathContext xctxt = m_xcontext;
            try {
                xctxt.pushNamespaceContext(xslInstruction);
                QName mode = this.getMode();
                if (isApplyImports)
                    template = m_stylesheetRoot.getTemplateComposed(xctxt, child, mode, maxImportLevel, endImportLevel, m_quietConflictWarnings, dtm);
                else
                    template = m_stylesheetRoot.getTemplateComposed(xctxt, child, mode, m_quietConflictWarnings, dtm);
            } finally {
                xctxt.popNamespaceContext();
            }
        }
        // See http://www.w3.org/TR/xslt#built-in-rule.
        if (null == template) {
            switch(nodeType) {
                case DTM.DOCUMENT_FRAGMENT_NODE:
                case DTM.ELEMENT_NODE:
                    template = m_stylesheetRoot.getDefaultRule();
                    break;
                case DTM.CDATA_SECTION_NODE:
                case DTM.TEXT_NODE:
                case DTM.ATTRIBUTE_NODE:
                    template = m_stylesheetRoot.getDefaultTextRule();
                    isDefaultTextRule = true;
                    break;
                case DTM.DOCUMENT_NODE:
                    template = m_stylesheetRoot.getDefaultRootRule();
                    break;
                default:
                    // No default rules for processing instructions and the like.
                    return false;
            }
        }
    }
    // the value directly to the result tree.
    try {
        pushElemTemplateElement(template);
        m_xcontext.pushCurrentNode(child);
        pushPairCurrentMatched(template, child);
        // Fix copy copy29 test.
        if (!isApplyImports) {
            DTMIterator cnl = new org.apache.xpath.NodeSetDTM(child, m_xcontext.getDTMManager());
            m_xcontext.pushContextNodeList(cnl);
        }
        if (isDefaultTextRule) {
            switch(nodeType) {
                case DTM.CDATA_SECTION_NODE:
                case DTM.TEXT_NODE:
                    ClonerToResultTree.cloneToResultTree(child, nodeType, dtm, getResultTreeHandler(), false);
                    break;
                case DTM.ATTRIBUTE_NODE:
                    dtm.dispatchCharactersEvents(child, getResultTreeHandler(), false);
                    break;
            }
        } else {
            // And execute the child templates.
            // 9/11/00: If template has been compiled, hand off to it
            // since much (most? all?) of the processing has been inlined.
            // (It would be nice if there was a single entry point that
            // worked for both... but the interpretive system works by
            // having the Tranformer execute the children, while the
            // compiled obviously has to run its own code. It's
            // also unclear that "execute" is really the right name for
            // that entry point.)
            m_xcontext.setSAXLocator(template);
            // m_xcontext.getVarStack().link();
            m_xcontext.getVarStack().link(template.m_frameSize);
            executeChildTemplates(template, true);
        }
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    } finally {
        if (!isDefaultTextRule)
            m_xcontext.getVarStack().unlink();
        m_xcontext.popCurrentNode();
        if (!isApplyImports) {
            m_xcontext.popContextNodeList();
        }
        popCurrentMatched();
        popElemTemplateElement();
    }
    return true;
}
Also used : QName(org.apache.xml.utils.QName) DTMIterator(org.apache.xml.dtm.DTMIterator) SAXException(org.xml.sax.SAXException) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) TransformerException(javax.xml.transform.TransformerException)

Example 43 with XPathContext

use of org.apache.xpath.XPathContext in project j2objc by google.

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)

Example 44 with XPathContext

use of org.apache.xpath.XPathContext in project j2objc by google.

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 45 with XPathContext

use of org.apache.xpath.XPathContext in project j2objc by google.

the class ElemVariable method getValue.

/**
   * Get the XObject representation of the variable.
   *
   * @param transformer non-null reference to the the current transform-time state.
   * @param sourceNode non-null reference to the <a href="http://www.w3.org/TR/xslt#dt-current-node">current source node</a>.
   *
   * @return the XObject representation of the variable.
   *
   * @throws TransformerException
   */
public XObject getValue(TransformerImpl transformer, int sourceNode) throws TransformerException {
    XObject var;
    XPathContext xctxt = transformer.getXPathContext();
    xctxt.pushCurrentNode(sourceNode);
    try {
        if (null != m_selectPattern) {
            var = m_selectPattern.execute(xctxt, sourceNode, this);
            var.allowDetachToRelease(false);
        } else if (null == getFirstChildElem()) {
            var = XString.EMPTYSTRING;
        } else {
            // Use result tree fragment.
            // Global variables may be deferred (see XUnresolvedVariable) and hence
            // need to be assigned to a different set of DTMs than local variables
            // so they aren't popped off the stack on return from a template.
            int df;
            ////// problem in parameters. Needs more study.
            try {
                //////////xctxt.getVarStack().link(0);
                if (// Global variable
                m_parentNode instanceof Stylesheet)
                    df = transformer.transformToGlobalRTF(this);
                else
                    df = transformer.transformToRTF(this);
            } finally {
            //////////////xctxt.getVarStack().unlink(); 
            }
            var = new XRTreeFrag(df, xctxt, this);
        }
    } finally {
        xctxt.popCurrentNode();
    }
    return var;
}
Also used : XRTreeFrag(org.apache.xpath.objects.XRTreeFrag) XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject)

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