Search in sources :

Example 1 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

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 2 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

the class ElemCopy method execute.

/**
   * The xsl:copy element provides an easy way of copying the current node.
   * Executing this function creates a copy of the current node into the
   * result tree.
   * <p>The namespace nodes of the current node are automatically
   * copied as well, but the attributes and children of the node are not
   * automatically copied. The content of the xsl:copy element is a
   * template for the attributes and children of the created node;
   * the content is instantiated only for nodes of types that can have
   * attributes or children (i.e. root nodes and element nodes).</p>
   * <p>The root node is treated specially because the root node of the
   * result tree is created implicitly. When the current node is the
   * root node, xsl:copy will not create a root node, but will just use
   * the content template.</p>
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    XPathContext xctxt = transformer.getXPathContext();
    try {
        int sourceNode = xctxt.getCurrentNode();
        xctxt.pushCurrentNode(sourceNode);
        DTM dtm = xctxt.getDTM(sourceNode);
        short nodeType = dtm.getNodeType(sourceNode);
        if ((DTM.DOCUMENT_NODE != nodeType) && (DTM.DOCUMENT_FRAGMENT_NODE != nodeType)) {
            SerializationHandler rthandler = transformer.getSerializationHandler();
            // TODO: Process the use-attribute-sets stuff
            ClonerToResultTree.cloneToResultTree(sourceNode, nodeType, dtm, rthandler, false);
            if (DTM.ELEMENT_NODE == nodeType) {
                super.execute(transformer);
                SerializerUtils.processNSDecls(rthandler, sourceNode, nodeType, dtm);
                transformer.executeChildTemplates(this, true);
                String ns = dtm.getNamespaceURI(sourceNode);
                String localName = dtm.getLocalName(sourceNode);
                transformer.getResultTreeHandler().endElement(ns, localName, dtm.getNodeName(sourceNode));
            }
        } else {
            super.execute(transformer);
            transformer.executeChildTemplates(this, true);
        }
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    } finally {
        xctxt.popCurrentNode();
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) TransformerException(javax.xml.transform.TransformerException)

Example 3 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

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 4 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

the class SerializerUtils method outputResultTreeFragment.

/**
     * Given a result tree fragment, walk the tree and
     * output it to the SerializationHandler.
     *
     * @param obj Result tree fragment object
     * @param support XPath context for the result tree fragment
     *
     * @throws org.xml.sax.SAXException
     */
public static void outputResultTreeFragment(SerializationHandler handler, XObject obj, XPathContext support) throws org.xml.sax.SAXException {
    int doc = obj.rtf();
    DTM dtm = support.getDTM(doc);
    if (null != dtm) {
        for (int n = dtm.getFirstChild(doc); DTM.NULL != n; n = dtm.getNextSibling(n)) {
            handler.flushPending();
            // to flush prefixes, will that cause problems ???
            if (dtm.getNodeType(n) == DTM.ELEMENT_NODE && dtm.getNamespaceURI(n) == null)
                handler.startPrefixMapping("", "");
            dtm.dispatchToEvents(n, handler);
        }
    }
}
Also used : DTM(org.apache.xml.dtm.DTM)

Example 5 with DTM

use of org.apache.xml.dtm.DTM in project robovm by robovm.

the class FuncDocument 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 {
    int context = xctxt.getCurrentNode();
    DTM dtm = xctxt.getDTM(context);
    int docContext = dtm.getDocumentRoot(context);
    XObject arg = (XObject) this.getArg0().execute(xctxt);
    String base = "";
    Expression arg1Expr = this.getArg1();
    if (null != arg1Expr) {
        // The URI reference may be relative. The base URI (see [3.2 Base URI]) 
        // of the node in the second argument node-set that is first in document 
        // order is used as the base URI for resolving the 
        // relative URI into an absolute URI. 
        XObject arg2 = arg1Expr.execute(xctxt);
        if (XObject.CLASS_NODESET == arg2.getType()) {
            int baseNode = arg2.iter().nextNode();
            if (baseNode == DTM.NULL) {
                // See http://www.w3.org/1999/11/REC-xslt-19991116-errata#E14.
                // If the second argument is an empty nodeset, this is an error.
                // The processor can recover by returning an empty nodeset.
                warn(xctxt, XSLTErrorResources.WG_EMPTY_SECOND_ARG, null);
                XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
                return nodes;
            } else {
                DTM baseDTM = xctxt.getDTM(baseNode);
                base = baseDTM.getDocumentBaseURI();
            }
        // %REVIEW% This doesn't seem to be a problem with the conformance
        // suite, but maybe it's just not doing a good test?
        //        int baseDoc = baseDTM.getDocument();
        //
        //        if (baseDoc == DTM.NULL /* || baseDoc instanceof Stylesheet  -->What to do?? */)
        //        {
        //
        //          // base = ((Stylesheet)baseDoc).getBaseIdentifier();
        //          base = xctxt.getNamespaceContext().getBaseIdentifier();
        //        }
        //        else
        //          base = xctxt.getSourceTreeManager().findURIFromDoc(baseDoc);
        } else {
            //Can not convert other type to a node-set!;
            arg2.iter();
        }
    } else {
        // If the second argument is omitted, then it defaults to 
        // the node in the stylesheet that contains the expression that 
        // includes the call to the document function. Note that a 
        // zero-length URI reference is a reference to the document 
        // relative to which the URI reference is being resolved; thus 
        // document("") refers to the root node of the stylesheet; 
        // the tree representation of the stylesheet is exactly 
        // the same as if the XML document containing the stylesheet 
        // was the initial source document.
        assertion(null != xctxt.getNamespaceContext(), "Namespace context can not be null!");
        base = xctxt.getNamespaceContext().getBaseIdentifier();
    }
    XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
    NodeSetDTM mnl = nodes.mutableNodeset();
    DTMIterator iterator = (XObject.CLASS_NODESET == arg.getType()) ? arg.iter() : null;
    int pos = DTM.NULL;
    while ((null == iterator) || (DTM.NULL != (pos = iterator.nextNode()))) {
        XMLString ref = (null != iterator) ? xctxt.getDTM(pos).getStringValue(pos) : arg.xstr();
        // member.
        if (null == arg1Expr && DTM.NULL != pos) {
            DTM baseDTM = xctxt.getDTM(pos);
            base = baseDTM.getDocumentBaseURI();
        }
        if (null == ref)
            continue;
        if (DTM.NULL == docContext) {
            //"context does not have an owner document!");
            error(xctxt, XSLTErrorResources.ER_NO_CONTEXT_OWNERDOC, null);
        }
        // From http://www.ics.uci.edu/pub/ietf/uri/rfc1630.txt
        // A partial form can be distinguished from an absolute form in that the
        // latter must have a colon and that colon must occur before any slash
        // characters. Systems not requiring partial forms should not use any
        // unencoded slashes in their naming schemes.  If they do, absolute URIs
        // will still work, but confusion may result.
        int indexOfColon = ref.indexOf(':');
        int indexOfSlash = ref.indexOf('/');
        if ((indexOfColon != -1) && (indexOfSlash != -1) && (indexOfColon < indexOfSlash)) {
            // The url (or filename, for that matter) is absolute.
            base = null;
        }
        int newDoc = getDoc(xctxt, context, ref.toString(), base);
        // nodes.mutableNodeset().addNode(newDoc);  
        if (DTM.NULL != newDoc) {
            // TODO: mnl.addNodeInDocOrder(newDoc, true, xctxt); ??
            if (!mnl.contains(newDoc)) {
                mnl.addElement(newDoc);
            }
        }
        if (null == iterator || newDoc == DTM.NULL)
            break;
    }
    return nodes;
}
Also used : NodeSetDTM(org.apache.xpath.NodeSetDTM) Expression(org.apache.xpath.Expression) XMLString(org.apache.xml.utils.XMLString) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM) XMLString(org.apache.xml.utils.XMLString) XObject(org.apache.xpath.objects.XObject) XNodeSet(org.apache.xpath.objects.XNodeSet) DTMIterator(org.apache.xml.dtm.DTMIterator)

Aggregations

DTM (org.apache.xml.dtm.DTM)100 XObject (org.apache.xpath.objects.XObject)24 DTMIterator (org.apache.xml.dtm.DTMIterator)23 NodeSetDTM (org.apache.xpath.NodeSetDTM)20 TransformerException (javax.xml.transform.TransformerException)17 XPathContext (org.apache.xpath.XPathContext)16 XMLString (org.apache.xml.utils.XMLString)12 XString (org.apache.xpath.objects.XString)10 SAXException (org.xml.sax.SAXException)10 XNodeSet (org.apache.xpath.objects.XNodeSet)8 XPath (org.apache.xpath.XPath)7 IOException (java.io.IOException)6 Vector (java.util.Vector)6 TransformerImpl (org.apache.xalan.transformer.TransformerImpl)6 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 SerializationHandler (org.apache.xml.serializer.SerializationHandler)6 QName (org.apache.xml.utils.QName)6 DOMSource (javax.xml.transform.dom.DOMSource)5 Hashtable (java.util.Hashtable)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4