Search in sources :

Example 21 with DTMIterator

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

the class ElemApplyTemplates method transformSelectedNodes.

/**
   * Perform a query if needed, and call transformNode for each child.
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException Thrown in a variety of circumstances.
   * @xsl.usage advanced
   */
public void transformSelectedNodes(TransformerImpl transformer) throws TransformerException {
    final XPathContext xctxt = transformer.getXPathContext();
    final int sourceNode = xctxt.getCurrentNode();
    DTMIterator sourceNodes = m_selectExpression.asIterator(xctxt, sourceNode);
    VariableStack vars = xctxt.getVarStack();
    int nParams = getParamElemCount();
    int thisframe = vars.getStackFrame();
    boolean pushContextNodeListFlag = false;
    try {
        xctxt.pushCurrentNode(DTM.NULL);
        xctxt.pushCurrentExpressionNode(DTM.NULL);
        xctxt.pushSAXLocatorNull();
        transformer.pushElemTemplateElement(null);
        final Vector keys = (m_sortElems == null) ? null : transformer.processSortKeys(this, sourceNode);
        // Sort if we need to.
        if (null != keys)
            sourceNodes = sortNodes(xctxt, keys, sourceNodes);
        final SerializationHandler rth = transformer.getSerializationHandler();
        //      ContentHandler chandler = rth.getContentHandler();
        final StylesheetRoot sroot = transformer.getStylesheet();
        final TemplateList tl = sroot.getTemplateListComposed();
        final boolean quiet = transformer.getQuietConflictWarnings();
        // Should be able to get this from the iterator but there must be a bug.
        DTM dtm = xctxt.getDTM(sourceNode);
        int argsFrame = -1;
        if (nParams > 0) {
            // This code will create a section on the stack that is all the 
            // evaluated arguments.  These will be copied into the real params 
            // section of each called template.
            argsFrame = vars.link(nParams);
            vars.setStackFrame(thisframe);
            for (int i = 0; i < nParams; i++) {
                ElemWithParam ewp = m_paramElems[i];
                XObject obj = ewp.getValue(transformer, sourceNode);
                vars.setLocalVariable(i, obj, argsFrame);
            }
            vars.setStackFrame(argsFrame);
        }
        xctxt.pushContextNodeList(sourceNodes);
        pushContextNodeListFlag = true;
        IntStack currentNodes = xctxt.getCurrentNodeStack();
        IntStack currentExpressionNodes = xctxt.getCurrentExpressionNodeStack();
        // pushParams(transformer, xctxt);
        int child;
        while (DTM.NULL != (child = sourceNodes.nextNode())) {
            currentNodes.setTop(child);
            currentExpressionNodes.setTop(child);
            if (xctxt.getDTM(child) != dtm) {
                dtm = xctxt.getDTM(child);
            }
            final int exNodeType = dtm.getExpandedTypeID(child);
            final int nodeType = dtm.getNodeType(child);
            final QName mode = transformer.getMode();
            ElemTemplate template = tl.getTemplateFast(xctxt, child, exNodeType, mode, -1, quiet, dtm);
            // 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 = sroot.getDefaultRule();
                        // %OPT% direct faster?
                        break;
                    case DTM.ATTRIBUTE_NODE:
                    case DTM.CDATA_SECTION_NODE:
                    case DTM.TEXT_NODE:
                        // if(rth.m_elemIsPending || rth.m_docPending)
                        //  rth.flushPending(true);
                        transformer.pushPairCurrentMatched(sroot.getDefaultTextRule(), child);
                        transformer.setCurrentElement(sroot.getDefaultTextRule());
                        // dtm.dispatchCharactersEvents(child, chandler, false);
                        dtm.dispatchCharactersEvents(child, rth, false);
                        transformer.popCurrentMatched();
                        continue;
                    case DTM.DOCUMENT_NODE:
                        template = sroot.getDefaultRootRule();
                        break;
                    default:
                        // No default rules for processing instructions and the like.
                        continue;
                }
            } else {
                transformer.setCurrentElement(template);
            }
            transformer.pushPairCurrentMatched(template, child);
            // See comment with unlink, below
            int currentFrameBottom;
            if (template.m_frameSize > 0) {
                xctxt.pushRTFContext();
                // See comment with unlink, below
                currentFrameBottom = vars.getStackFrame();
                vars.link(template.m_frameSize);
                // xsl:params might not be nulled.
                if (/* nParams > 0 && */
                template.m_inArgsSize > 0) {
                    int paramIndex = 0;
                    for (ElemTemplateElement elem = template.getFirstChildElem(); null != elem; elem = elem.getNextSiblingElem()) {
                        if (Constants.ELEMNAME_PARAMVARIABLE == elem.getXSLToken()) {
                            ElemParam ep = (ElemParam) elem;
                            int i;
                            for (i = 0; i < nParams; i++) {
                                ElemWithParam ewp = m_paramElems[i];
                                if (ewp.m_qnameID == ep.m_qnameID) {
                                    XObject obj = vars.getLocalVariable(i, argsFrame);
                                    vars.setLocalVariable(paramIndex, obj);
                                    break;
                                }
                            }
                            if (i == nParams)
                                vars.setLocalVariable(paramIndex, null);
                        } else
                            break;
                        paramIndex++;
                    }
                }
            } else
                currentFrameBottom = 0;
            // each of them.
            for (ElemTemplateElement t = template.m_firstChild; t != null; t = t.m_nextSibling) {
                xctxt.setSAXLocator(t);
                try {
                    transformer.pushElemTemplateElement(t);
                    t.execute(transformer);
                } finally {
                    transformer.popElemTemplateElement();
                }
            }
            if (template.m_frameSize > 0) {
                // See Frank Weiss bug around 03/19/2002 (no Bugzilla report yet).
                // While unlink will restore to the proper place, the real position 
                // may have been changed for xsl:with-param, so that variables 
                // can be accessed.  
                // of right now.
                // More:
                // When we entered this function, the current 
                // frame buffer (cfb) index in the variable stack may 
                // have been manually set.  If we just call 
                // unlink(), however, it will restore the cfb to the 
                // previous link index from the link stack, rather than 
                // the manually set cfb.  So, 
                // the only safe solution is to restore it back 
                // to the same position it was on entry, since we're 
                // really not working in a stack context here. (Bug4218)
                vars.unlink(currentFrameBottom);
                xctxt.popRTFContext();
            }
            transformer.popCurrentMatched();
        }
    // end while (DTM.NULL != (child = sourceNodes.nextNode()))
    } catch (SAXException se) {
        transformer.getErrorListener().fatalError(new TransformerException(se));
    } finally {
        // Unlink to the original stack frame
        if (nParams > 0)
            vars.unlink(thisframe);
        xctxt.popSAXLocator();
        if (pushContextNodeListFlag)
            xctxt.popContextNodeList();
        transformer.popElemTemplateElement();
        xctxt.popCurrentExpressionNode();
        xctxt.popCurrentNode();
        sourceNodes.detach();
    }
}
Also used : VariableStack(org.apache.xpath.VariableStack) IntStack(org.apache.xml.utils.IntStack) QName(org.apache.xml.utils.QName) SerializationHandler(org.apache.xml.serializer.SerializationHandler) DTMIterator(org.apache.xml.dtm.DTMIterator) SAXException(org.xml.sax.SAXException) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) Vector(java.util.Vector) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 22 with DTMIterator

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

the class ElemCopyOf method execute.

/**
   * The xsl:copy-of element can be used to insert a result tree
   * fragment into the result tree, without first converting it to
   * a string as xsl:value-of does (see [7.6.1 Generating Text with
   * xsl:value-of]).
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    try {
        XPathContext xctxt = transformer.getXPathContext();
        int sourceNode = xctxt.getCurrentNode();
        XObject value = m_selectExpression.execute(xctxt, sourceNode, this);
        SerializationHandler handler = transformer.getSerializationHandler();
        if (null != value) {
            int type = value.getType();
            String s;
            switch(type) {
                case XObject.CLASS_BOOLEAN:
                case XObject.CLASS_NUMBER:
                case XObject.CLASS_STRING:
                    s = value.str();
                    handler.characters(s.toCharArray(), 0, s.length());
                    break;
                case XObject.CLASS_NODESET:
                    // System.out.println(value);
                    DTMIterator nl = value.iter();
                    // Copy the tree.
                    DTMTreeWalker tw = new TreeWalker2Result(transformer, handler);
                    int pos;
                    while (DTM.NULL != (pos = nl.nextNode())) {
                        DTM dtm = xctxt.getDTMManager().getDTM(pos);
                        short t = dtm.getNodeType(pos);
                        // generated, so we need to only walk the child nodes.
                        if (t == DTM.DOCUMENT_NODE) {
                            for (int child = dtm.getFirstChild(pos); child != DTM.NULL; child = dtm.getNextSibling(child)) {
                                tw.traverse(child);
                            }
                        } else if (t == DTM.ATTRIBUTE_NODE) {
                            SerializerUtils.addAttribute(handler, pos);
                        } else {
                            tw.traverse(pos);
                        }
                    }
                    // nl.detach();
                    break;
                case XObject.CLASS_RTREEFRAG:
                    SerializerUtils.outputResultTreeFragment(handler, value, transformer.getXPathContext());
                    break;
                default:
                    s = value.str();
                    handler.characters(s.toCharArray(), 0, s.length());
                    break;
            }
        }
    // I don't think we want this.  -sb
    //  if (transformer.getDebug())
    //  transformer.getTraceManager().fireSelectedEvent(sourceNode, this,
    //  "endSelect", m_selectExpression, value);
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) TreeWalker2Result(org.apache.xalan.transformer.TreeWalker2Result) DTMTreeWalker(org.apache.xml.dtm.ref.DTMTreeWalker) DTMIterator(org.apache.xml.dtm.DTMIterator) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 23 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 24 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 25 with DTMIterator

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

the class UnionPathIterator method setRoot.

/**
   * Initialize the context values for this expression 
   * after it is cloned.
   *
   * @param context The XPath runtime context for this 
   * transformation.
   */
public void setRoot(int context, Object environment) {
    super.setRoot(context, environment);
    try {
        if (null != m_exprs) {
            int n = m_exprs.length;
            DTMIterator[] newIters = new DTMIterator[n];
            for (int i = 0; i < n; i++) {
                DTMIterator iter = m_exprs[i].asIterator(m_execContext, context);
                newIters[i] = iter;
                iter.nextNode();
            }
            m_iterators = newIters;
        }
    } catch (Exception e) {
        throw new org.apache.xml.utils.WrappedRuntimeException(e);
    }
}
Also used : DTMIterator(org.apache.xml.dtm.DTMIterator)

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