Search in sources :

Example 71 with DTM

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

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

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

the class KeyTable method getRefsTable.

/**
   * @return lazy initialized refs table associating evaluation of key function
   *         with a XNodeSet
   */
private Hashtable getRefsTable() {
    if (m_refsTable == null) {
        // initial capacity set to a prime number to improve hash performance
        m_refsTable = new Hashtable(89);
        KeyIterator ki = (KeyIterator) (m_keyNodes).getContainedIter();
        XPathContext xctxt = ki.getXPathContext();
        Vector keyDecls = getKeyDeclarations();
        int nKeyDecls = keyDecls.size();
        int currentNode;
        m_keyNodes.reset();
        while (DTM.NULL != (currentNode = m_keyNodes.nextNode())) {
            try {
                for (int keyDeclIdx = 0; keyDeclIdx < nKeyDecls; keyDeclIdx++) {
                    KeyDeclaration keyDeclaration = (KeyDeclaration) keyDecls.elementAt(keyDeclIdx);
                    XObject xuse = keyDeclaration.getUse().execute(xctxt, currentNode, ki.getPrefixResolver());
                    if (xuse.getType() != xuse.CLASS_NODESET) {
                        XMLString exprResult = xuse.xstr();
                        addValueInRefsTable(xctxt, exprResult, currentNode);
                    } else {
                        DTMIterator i = ((XNodeSet) xuse).iterRaw();
                        int currentNodeInUseClause;
                        while (DTM.NULL != (currentNodeInUseClause = i.nextNode())) {
                            DTM dtm = xctxt.getDTM(currentNodeInUseClause);
                            XMLString exprResult = dtm.getStringValue(currentNodeInUseClause);
                            addValueInRefsTable(xctxt, exprResult, currentNode);
                        }
                    }
                }
            } catch (TransformerException te) {
                throw new WrappedRuntimeException(te);
            }
        }
    }
    return m_refsTable;
}
Also used : Hashtable(java.util.Hashtable) DTMIterator(org.apache.xml.dtm.DTMIterator) XNodeSet(org.apache.xpath.objects.XNodeSet) KeyDeclaration(org.apache.xalan.templates.KeyDeclaration) XPathContext(org.apache.xpath.XPathContext) XMLString(org.apache.xml.utils.XMLString) DTM(org.apache.xml.dtm.DTM) Vector(java.util.Vector) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException) WrappedRuntimeException(org.apache.xml.utils.WrappedRuntimeException)

Example 74 with DTM

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

the class NodeSorter method compare.

/**
   * Return the results of a compare of two nodes.
   * TODO: Optimize compare -- cache the getStringExpr results, key by m_selectPat + hash of node.
   *
   * @param n1 First node to use in compare
   * @param n2 Second node to use in compare
   * @param kIndex Index of NodeSortKey to use for sort
   * @param support XPath context to use
   *
   * @return The results of the compare of the two nodes.
   *
   * @throws TransformerException
   */
int compare(NodeCompareElem n1, NodeCompareElem n2, int kIndex, XPathContext support) throws TransformerException {
    int result = 0;
    NodeSortKey k = (NodeSortKey) m_keys.elementAt(kIndex);
    if (k.m_treatAsNumbers) {
        double n1Num, n2Num;
        if (kIndex == 0) {
            n1Num = ((Double) n1.m_key1Value).doubleValue();
            n2Num = ((Double) n2.m_key1Value).doubleValue();
        } else if (kIndex == 1) {
            n1Num = ((Double) n1.m_key2Value).doubleValue();
            n2Num = ((Double) n2.m_key2Value).doubleValue();
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
      double n1Num = (double)n1.m_keyValue[kIndex];
      double n2Num = (double)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1Num = r1.num();
            // Can't use NaN for compare. They are never equal. Use zero instead.
            // That way we can keep elements in document order. 
            //n1Num = Double.isNaN(d) ? 0.0 : d;
            n2Num = r2.num();
        //n2Num = Double.isNaN(d) ? 0.0 : d;
        }
        if ((n1Num == n2Num) && ((kIndex + 1) < m_keys.size())) {
            result = compare(n1, n2, kIndex + 1, support);
        } else {
            double diff;
            if (Double.isNaN(n1Num)) {
                if (Double.isNaN(n2Num))
                    diff = 0.0;
                else
                    diff = -1;
            } else if (Double.isNaN(n2Num))
                diff = 1;
            else
                diff = n1Num - n2Num;
            // process order parameter 
            result = (int) ((diff < 0.0) ? (k.m_descending ? 1 : -1) : (diff > 0.0) ? (k.m_descending ? -1 : 1) : 0);
        }
    } else // end treat as numbers 
    {
        CollationKey n1String, n2String;
        if (kIndex == 0) {
            n1String = (CollationKey) n1.m_key1Value;
            n2String = (CollationKey) n2.m_key1Value;
        } else if (kIndex == 1) {
            n1String = (CollationKey) n1.m_key2Value;
            n2String = (CollationKey) n2.m_key2Value;
        } else /* Leave this in case we decide to use an array later
      if (kIndex < maxkey)
      {
        String n1String = (String)n1.m_keyValue[kIndex];
        String n2String = (String)n2.m_keyValue[kIndex];
      }*/
        {
            // Get values dynamically
            XObject r1 = k.m_selectPat.execute(m_execContext, n1.m_node, k.m_namespaceContext);
            XObject r2 = k.m_selectPat.execute(m_execContext, n2.m_node, k.m_namespaceContext);
            n1String = k.m_col.getCollationKey(r1.str());
            n2String = k.m_col.getCollationKey(r2.str());
        }
        // Use collation keys for faster compare, but note that whitespaces 
        // etc... are treated differently from if we were comparing Strings.
        result = n1String.compareTo(n2String);
        //Process caseOrder parameter
        if (k.m_caseOrderUpper) {
            String tempN1 = n1String.getSourceString().toLowerCase();
            String tempN2 = n2String.getSourceString().toLowerCase();
            if (tempN1.equals(tempN2)) {
                //java defaults to upper case is greater.
                result = result == 0 ? 0 : -result;
            }
        }
        //Process order parameter
        if (k.m_descending) {
            result = -result;
        }
    }
    if (0 == result) {
        if ((kIndex + 1) < m_keys.size()) {
            result = compare(n1, n2, kIndex + 1, support);
        }
    }
    if (0 == result) {
        // I shouldn't have to do this except that there seems to 
        // be a glitch in the mergesort
        // if(r1.getType() == r1.CLASS_NODESET)
        // {
        // %OPT%
        DTM dtm = support.getDTM(n1.m_node);
        result = dtm.isNodeAfter(n1.m_node, n2.m_node) ? -1 : 1;
    // }
    }
    return result;
}
Also used : CollationKey(java.text.CollationKey) DTM(org.apache.xml.dtm.DTM) XObject(org.apache.xpath.objects.XObject)

Example 75 with DTM

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

the class ElemNumber method getPreviousNode.

/**
   * Get the previous node to be counted.
   *
   * @param xctxt The XPath runtime state for this.
   * @param pos The current node
   *
   * @return the previous node to be counted.
   *
   * @throws TransformerException
   */
public int getPreviousNode(XPathContext xctxt, int pos) throws TransformerException {
    XPath countMatchPattern = getCountMatchPattern(xctxt, pos);
    DTM dtm = xctxt.getDTM(pos);
    if (Constants.NUMBERLEVEL_ANY == m_level) {
        XPath fromMatchPattern = m_fromMatchPattern;
        // or the top of the tree is found.
        while (DTM.NULL != pos) {
            // Get the previous sibling, if there is no previous sibling, 
            // then count the parent, but if there is a previous sibling, 
            // dive down to the lowest right-hand (last) child of that sibling.
            int next = dtm.getPreviousSibling(pos);
            if (DTM.NULL == next) {
                next = dtm.getParent(pos);
                if ((DTM.NULL != next) && ((((null != fromMatchPattern) && (fromMatchPattern.getMatchScore(xctxt, next) != XPath.MATCH_SCORE_NONE))) || (dtm.getNodeType(next) == DTM.DOCUMENT_NODE))) {
                    // return null from function.
                    pos = DTM.NULL;
                    // from while loop
                    break;
                }
            } else {
                // dive down to the lowest right child.
                int child = next;
                while (DTM.NULL != child) {
                    child = dtm.getLastChild(next);
                    if (DTM.NULL != child)
                        next = child;
                }
            }
            pos = next;
            if ((DTM.NULL != pos) && ((null == countMatchPattern) || (countMatchPattern.getMatchScore(xctxt, pos) != XPath.MATCH_SCORE_NONE))) {
                break;
            }
        }
    } else // NUMBERLEVEL_MULTI or NUMBERLEVEL_SINGLE
    {
        while (DTM.NULL != pos) {
            pos = dtm.getPreviousSibling(pos);
            if ((DTM.NULL != pos) && ((null == countMatchPattern) || (countMatchPattern.getMatchScore(xctxt, pos) != XPath.MATCH_SCORE_NONE))) {
                break;
            }
        }
    }
    return pos;
}
Also used : XPath(org.apache.xpath.XPath) NodeSetDTM(org.apache.xpath.NodeSetDTM) DTM(org.apache.xml.dtm.DTM)

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