Search in sources :

Example 26 with XPathContext

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

the class TreeWalker2Result method startNode.

/**
   * Start traversal of the tree at the given node
   *
   *
   * @param node Starting node for traversal
   *
   * @throws TransformerException
   */
protected void startNode(int node) throws org.xml.sax.SAXException {
    XPathContext xcntxt = m_transformer.getXPathContext();
    try {
        if (DTM.ELEMENT_NODE == m_dtm.getNodeType(node)) {
            xcntxt.pushCurrentNode(node);
            if (m_startNode != node) {
                super.startNode(node);
            } else {
                String elemName = m_dtm.getNodeName(node);
                String localName = m_dtm.getLocalName(node);
                String namespace = m_dtm.getNamespaceURI(node);
                //xcntxt.pushCurrentNode(node);       
                // SAX-like call to allow adding attributes afterwards
                m_handler.startElement(namespace, localName, elemName);
                boolean hasNSDecls = false;
                DTM dtm = m_dtm;
                for (int ns = dtm.getFirstNamespaceNode(node, true); DTM.NULL != ns; ns = dtm.getNextNamespaceNode(node, ns, true)) {
                    SerializerUtils.ensureNamespaceDeclDeclared(m_handler, dtm, ns);
                }
                for (int attr = dtm.getFirstAttribute(node); DTM.NULL != attr; attr = dtm.getNextAttribute(attr)) {
                    SerializerUtils.addAttribute(m_handler, attr);
                }
            }
        } else {
            xcntxt.pushCurrentNode(node);
            super.startNode(node);
            xcntxt.popCurrentNode();
        }
    } catch (javax.xml.transform.TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM)

Example 27 with XPathContext

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

the class TransformerImpl method processSortKeys.

/**
   * Get the keys for the xsl:sort elements.
   * Note: Should this go into ElemForEach?
   *
   * @param foreach Valid ElemForEach element, not null.
   * @param sourceNodeContext The current node context in the source tree,
   * needed to evaluate the Attribute Value Templates.
   *
   * @return A Vector of NodeSortKeys, or null.
   *
   * @throws TransformerException
   * @xsl.usage advanced
   */
public Vector processSortKeys(ElemForEach foreach, int sourceNodeContext) throws TransformerException {
    Vector keys = null;
    XPathContext xctxt = m_xcontext;
    int nElems = foreach.getSortElemCount();
    if (nElems > 0)
        keys = new Vector();
    // March backwards, collecting the sort keys.
    for (int i = 0; i < nElems; i++) {
        ElemSort sort = foreach.getSortElem(i);
        String langString = (null != sort.getLang()) ? sort.getLang().evaluate(xctxt, sourceNodeContext, foreach) : null;
        String dataTypeString = sort.getDataType().evaluate(xctxt, sourceNodeContext, foreach);
        if (dataTypeString.indexOf(":") >= 0)
            System.out.println("TODO: Need to write the hooks for QNAME sort data type");
        else if (!(dataTypeString.equalsIgnoreCase(Constants.ATTRVAL_DATATYPE_TEXT)) && !(dataTypeString.equalsIgnoreCase(Constants.ATTRVAL_DATATYPE_NUMBER)))
            foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_DATATYPE, dataTypeString });
        boolean treatAsNumbers = ((null != dataTypeString) && dataTypeString.equals(Constants.ATTRVAL_DATATYPE_NUMBER)) ? true : false;
        String orderString = sort.getOrder().evaluate(xctxt, sourceNodeContext, foreach);
        if (!(orderString.equalsIgnoreCase(Constants.ATTRVAL_ORDER_ASCENDING)) && !(orderString.equalsIgnoreCase(Constants.ATTRVAL_ORDER_DESCENDING)))
            foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_ORDER, orderString });
        boolean descending = ((null != orderString) && orderString.equals(Constants.ATTRVAL_ORDER_DESCENDING)) ? true : false;
        AVT caseOrder = sort.getCaseOrder();
        boolean caseOrderUpper;
        if (null != caseOrder) {
            String caseOrderString = caseOrder.evaluate(xctxt, sourceNodeContext, foreach);
            if (!(caseOrderString.equalsIgnoreCase(Constants.ATTRVAL_CASEORDER_UPPER)) && !(caseOrderString.equalsIgnoreCase(Constants.ATTRVAL_CASEORDER_LOWER)))
                foreach.error(XSLTErrorResources.ER_ILLEGAL_ATTRIBUTE_VALUE, new Object[] { Constants.ATTRNAME_CASEORDER, caseOrderString });
            caseOrderUpper = ((null != caseOrderString) && caseOrderString.equals(Constants.ATTRVAL_CASEORDER_UPPER)) ? true : false;
        } else {
            caseOrderUpper = false;
        }
        keys.addElement(new NodeSortKey(this, sort.getSelect(), treatAsNumbers, descending, langString, caseOrderUpper, foreach));
    }
    return keys;
}
Also used : XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject) Vector(java.util.Vector) NodeVector(org.apache.xml.utils.NodeVector) ElemSort(org.apache.xalan.templates.ElemSort) AVT(org.apache.xalan.templates.AVT)

Example 28 with XPathContext

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

the class TransformerImpl method transformToRTF.

/**
   * Given a stylesheet element, create a result tree fragment from it's
   * contents.
   * @param templateParent The template element that holds the fragment.
   * @param dtmFrag The DTM to write the RTF into
   * @return the NodeHandle for the root node of the resulting RTF.
   *
   * @throws TransformerException
   * @xsl.usage advanced
   */
private int transformToRTF(ElemTemplateElement templateParent, DTM dtmFrag) throws TransformerException {
    XPathContext xctxt = m_xcontext;
    ContentHandler rtfHandler = dtmFrag.getContentHandler();
    // Obtain the ResultTreeFrag's root node.
    // NOTE: In SAX2RTFDTM, this value isn't available until after
    // the startDocument has been issued, so assignment has been moved
    // down a bit in the code.
    // not yet reliably = dtmFrag.getDocument();
    int resultFragment;
    // Save the current result tree handler.
    SerializationHandler savedRTreeHandler = this.m_serializationHandler;
    // And make a new handler for the RTF.
    ToSAXHandler h = new ToXMLSAXHandler();
    h.setContentHandler(rtfHandler);
    h.setTransformer(this);
    // Replace the old handler (which was already saved)
    m_serializationHandler = h;
    // use local variable for the current handler
    SerializationHandler rth = m_serializationHandler;
    try {
        rth.startDocument();
        // startDocument is "bottlenecked" in RTH. We need it acted upon immediately,
        // to set the DTM's state as in-progress, so that if the xsl:variable's body causes
        // further RTF activity we can keep that from bashing this DTM.
        rth.flushPending();
        try {
            // Do the transformation of the child elements.
            executeChildTemplates(templateParent, true);
            // Make sure everything is flushed!
            rth.flushPending();
            // Get the document ID. May not exist until the RTH has not only
            // received, but flushed, the startDocument, and may be invalid
            // again after the document has been closed (still debating that)
            // ... so waiting until just before the end seems simplest/safest. 
            resultFragment = dtmFrag.getDocument();
        } finally {
            rth.endDocument();
        }
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    } finally {
        // Restore the previous result tree handler.
        this.m_serializationHandler = savedRTreeHandler;
    }
    return resultFragment;
}
Also used : ToSAXHandler(org.apache.xml.serializer.ToSAXHandler) SAXException(org.xml.sax.SAXException) SerializationHandler(org.apache.xml.serializer.SerializationHandler) XPathContext(org.apache.xpath.XPathContext) ContentHandler(org.xml.sax.ContentHandler) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler) TransformerException(javax.xml.transform.TransformerException)

Example 29 with XPathContext

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

the class ElemForEach 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);
    try {
        final Vector keys = (m_sortElems == null) ? null : transformer.processSortKeys(this, sourceNode);
        // Sort if we need to.
        if (null != keys)
            sourceNodes = sortNodes(xctxt, keys, sourceNodes);
        xctxt.pushCurrentNode(DTM.NULL);
        IntStack currentNodes = xctxt.getCurrentNodeStack();
        xctxt.pushCurrentExpressionNode(DTM.NULL);
        IntStack currentExpressionNodes = xctxt.getCurrentExpressionNodeStack();
        xctxt.pushSAXLocatorNull();
        xctxt.pushContextNodeList(sourceNodes);
        transformer.pushElemTemplateElement(null);
        // pushParams(transformer, xctxt);
        // Should be able to get this from the iterator but there must be a bug.
        DTM dtm = xctxt.getDTM(sourceNode);
        int docID = sourceNode & DTMManager.IDENT_DTM_DEFAULT;
        int child;
        while (DTM.NULL != (child = sourceNodes.nextNode())) {
            currentNodes.setTop(child);
            currentExpressionNodes.setTop(child);
            if ((child & DTMManager.IDENT_DTM_DEFAULT) != docID) {
                dtm = xctxt.getDTM(child);
                docID = child & DTMManager.IDENT_DTM_DEFAULT;
            }
            //final int exNodeType = dtm.getExpandedTypeID(child);
            final int nodeType = dtm.getNodeType(child);
            // each of them.
            for (ElemTemplateElement t = this.m_firstChild; t != null; t = t.m_nextSibling) {
                xctxt.setSAXLocator(t);
                transformer.setCurrentElement(t);
                t.execute(transformer);
            }
            // FuncDocument and here.
            if (m_doc_cache_off) {
                if (DEBUG)
                    System.out.println("JJK***** CACHE RELEASE *****\n" + "\tdtm=" + dtm.getDocumentBaseURI());
                // NOTE: This will work because this is _NOT_ a shared DTM, and thus has
                // only a single Document node. If it could ever be an RTF or other
                // shared DTM, this would require substantial rework.
                xctxt.getSourceTreeManager().removeDocumentFromCache(dtm.getDocument());
                xctxt.release(dtm, false);
            }
        }
    } finally {
        xctxt.popSAXLocator();
        xctxt.popContextNodeList();
        transformer.popElemTemplateElement();
        xctxt.popCurrentExpressionNode();
        xctxt.popCurrentNode();
        sourceNodes.detach();
    }
}
Also used : IntStack(org.apache.xml.utils.IntStack) XPathContext(org.apache.xpath.XPathContext) DTM(org.apache.xml.dtm.DTM) Vector(java.util.Vector) DTMIterator(org.apache.xml.dtm.DTMIterator)

Example 30 with XPathContext

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

the class ElemIf method execute.

/**
   * Conditionally execute a sub-template.
   * The expression is evaluated and the resulting object is converted
   * to a boolean as if by a call to the boolean function. If the result
   * is true, then the content template is instantiated; otherwise, nothing
   * is created.
   *
   * @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();
    int sourceNode = xctxt.getCurrentNode();
    if (m_test.bool(xctxt, sourceNode, this)) {
        transformer.executeChildTemplates(this, true);
    }
}
Also used : XPathContext(org.apache.xpath.XPathContext)

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