Search in sources :

Example 26 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project j2objc by google.

the class LocPathIterator method asNode.

/**
   * Return the first node out of the nodeset, if this expression is 
   * a nodeset expression.  This is the default implementation for 
   * nodesets.  Derived classes should try and override this and return a 
   * value without having to do a clone operation.
   * @param xctxt The XPath runtime context.
   * @return the first node out of the nodeset, or DTM.NULL.
   */
public int asNode(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    DTMIterator iter = (DTMIterator) m_clones.getInstance();
    int current = xctxt.getCurrentNode();
    iter.setRoot(current, xctxt);
    int next = iter.nextNode();
    // m_clones.freeInstance(iter);
    iter.detach();
    return next;
}
Also used : DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 27 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project j2objc by google.

the class FuncPosition method getPositionInContextNodeList.

/**
   * Get the position in the current context node list.
   *
   * @param xctxt Runtime XPath context.
   *
   * @return The current position of the itteration in the context node list, 
   *         or -1 if there is no active context node list.
   */
public int getPositionInContextNodeList(XPathContext xctxt) {
    // System.out.println("FuncPosition- entry");
    // If we're in a predicate, then this will return non-null.
    SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
    if (null != iter) {
        int prox = iter.getProximityPosition(xctxt);
        // System.out.println("FuncPosition- prox: "+prox);
        return prox;
    }
    DTMIterator cnl = xctxt.getContextNodeList();
    if (null != cnl) {
        int n = cnl.getCurrentNode();
        if (n == DTM.NULL) {
            if (cnl.getCurrentPos() == 0)
                return 0;
            // a problem for current().
            try {
                cnl = cnl.cloneWithReset();
            } catch (CloneNotSupportedException cnse) {
                throw new org.apache.xml.utils.WrappedRuntimeException(cnse);
            }
            int currentNode = xctxt.getContextNode();
            // System.out.println("currentNode: "+currentNode);
            while (DTM.NULL != (n = cnl.nextNode())) {
                if (n == currentNode)
                    break;
            }
        }
        // System.out.println("FuncPosition- cnl.getCurrentPos(): "+cnl.getCurrentPos());
        return cnl.getCurrentPos();
    }
    // System.out.println("FuncPosition - out of guesses: -1");
    return -1;
}
Also used : SubContextList(org.apache.xpath.axes.SubContextList) DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 28 with DTMIterator

use of org.apache.xml.dtm.DTMIterator 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 29 with DTMIterator

use of org.apache.xml.dtm.DTMIterator 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 30 with DTMIterator

use of org.apache.xml.dtm.DTMIterator in project j2objc by google.

the class FuncKey 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 {
    // TransformerImpl transformer = (TransformerImpl)xctxt;
    TransformerImpl transformer = (TransformerImpl) xctxt.getOwnerObject();
    XNodeSet nodes = null;
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int docContext = dtm.getDocumentRoot(context);
    if (DTM.NULL == docContext) {
    // path.error(context, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC); //"context does not have an owner document!");
    }
    String xkeyname = getArg0().execute(xctxt).str();
    QName keyname = new QName(xkeyname, xctxt.getNamespaceContext());
    XObject arg = getArg1().execute(xctxt);
    boolean argIsNodeSetDTM = (XObject.CLASS_NODESET == arg.getType());
    KeyManager kmgr = transformer.getKeyManager();
    // Don't bother with nodeset logic if the thing is only one node.
    if (argIsNodeSetDTM) {
        XNodeSet ns = (XNodeSet) arg;
        ns.setShouldCacheNodes(true);
        int len = ns.getLength();
        if (len <= 1)
            argIsNodeSetDTM = false;
    }
    if (argIsNodeSetDTM) {
        Hashtable usedrefs = null;
        DTMIterator ni = arg.iter();
        int pos;
        UnionPathIterator upi = new UnionPathIterator();
        upi.exprSetParent(this);
        while (DTM.NULL != (pos = ni.nextNode())) {
            dtm = xctxt.getDTM(pos);
            XMLString ref = dtm.getStringValue(pos);
            if (null == ref)
                continue;
            if (null == usedrefs)
                usedrefs = new Hashtable();
            if (usedrefs.get(ref) != null) {
                // We already have 'em.
                continue;
            } else {
                // ISTRUE being used as a dummy value.
                usedrefs.put(ref, ISTRUE);
            }
            XNodeSet nl = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());
            nl.setRoot(xctxt.getCurrentNode(), xctxt);
            //        try
            //        {
            upi.addIterator(nl);
        //        }
        //        catch(CloneNotSupportedException cnse)
        //        {
        //          // will never happen.
        //        }
        //mnodeset.addNodesInDocOrder(nl, xctxt); needed??
        }
        int current = xctxt.getCurrentNode();
        upi.setRoot(current, xctxt);
        nodes = new XNodeSet(upi);
    } else {
        XMLString ref = arg.xstr();
        nodes = kmgr.getNodeSetDTMByKey(xctxt, docContext, keyname, ref, xctxt.getNamespaceContext());
        nodes.setRoot(xctxt.getCurrentNode(), xctxt);
    }
    return nodes;
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) UnionPathIterator(org.apache.xpath.axes.UnionPathIterator) QName(org.apache.xml.utils.QName) Hashtable(java.util.Hashtable) XMLString(org.apache.xml.utils.XMLString) XNodeSet(org.apache.xpath.objects.XNodeSet) DTMIterator(org.apache.xml.dtm.DTMIterator) DTM(org.apache.xml.dtm.DTM) XMLString(org.apache.xml.utils.XMLString) KeyManager(org.apache.xalan.transformer.KeyManager) XObject(org.apache.xpath.objects.XObject)

Aggregations

DTMIterator (org.apache.xml.dtm.DTMIterator)46 DTM (org.apache.xml.dtm.DTM)23 XPathContext (org.apache.xpath.XPathContext)12 XObject (org.apache.xpath.objects.XObject)12 TransformerException (javax.xml.transform.TransformerException)11 XMLString (org.apache.xml.utils.XMLString)10 XNumber (org.apache.xpath.objects.XNumber)10 XNodeSet (org.apache.xpath.objects.XNodeSet)8 Vector (java.util.Vector)6 QName (org.apache.xml.utils.QName)6 SAXException (org.xml.sax.SAXException)6 SerializationHandler (org.apache.xml.serializer.SerializationHandler)5 Hashtable (java.util.Hashtable)4 IntStack (org.apache.xml.utils.IntStack)4 NodeSetDTM (org.apache.xpath.NodeSetDTM)4 SubContextList (org.apache.xpath.axes.SubContextList)4 IOException (java.io.IOException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)2 KeyDeclaration (org.apache.xalan.templates.KeyDeclaration)2