Search in sources :

Example 1 with ToSAXHandler

use of org.apache.xml.serializer.ToSAXHandler in project robovm by robovm.

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

use of org.apache.xml.serializer.ToSAXHandler 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)

Aggregations

TransformerException (javax.xml.transform.TransformerException)2 SerializationHandler (org.apache.xml.serializer.SerializationHandler)2 ToSAXHandler (org.apache.xml.serializer.ToSAXHandler)2 ToXMLSAXHandler (org.apache.xml.serializer.ToXMLSAXHandler)2 XPathContext (org.apache.xpath.XPathContext)2 ContentHandler (org.xml.sax.ContentHandler)2 SAXException (org.xml.sax.SAXException)2