Search in sources :

Example 11 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler in project j2objc by google.

the class TransformerHandlerImpl method setResult.

////////////////////////////////////////////////////////////////////
// Implementation of javax.xml.transform.sax.TransformerHandler.
////////////////////////////////////////////////////////////////////
/**
   * Enables the user of the TransformerHandler to set the
   * to set the Result for the transformation.
   *
   * @param result A Result instance, should not be null.
   *
   * @throws IllegalArgumentException if result is invalid for some reason.
   */
public void setResult(Result result) throws IllegalArgumentException {
    if (null == result)
        //"result should not be null");
        throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_NULL, null));
    try {
        //      ContentHandler handler =
        //        m_transformer.createResultContentHandler(result);
        //      m_transformer.setContentHandler(handler);
        SerializationHandler xoh = m_transformer.createSerializationHandler(result);
        m_transformer.setSerializationHandler(xoh);
    } catch (javax.xml.transform.TransformerException te) {
        //"result could not be set");
        throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_COULD_NOT_BE_SET, null));
    }
    m_result = result;
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler)

Example 12 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler 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.
   * @param outputTarget The output source target.
   *
   * @throws TransformerException
   */
public void transformNode(int node, Result outputTarget) throws TransformerException {
    SerializationHandler xoh = createSerializationHandler(outputTarget);
    this.setSerializationHandler(xoh);
    m_outputTarget = outputTarget;
    transformNode(node);
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler)

Example 13 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler 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 14 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler in project j2objc by google.

the class ElemLiteralResult method execute.

/**
   * Copy a Literal Result Element into the Result tree, copy the
   * non-excluded namespace attributes, copy the attributes not
   * of the XSLT namespace, and execute the children of the LRE.
   * @see <a href="http://www.w3.org/TR/xslt#literal-result-element">literal-result-element in XSLT Specification</a>
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    SerializationHandler rhandler = transformer.getSerializationHandler();
    try {
        // JJK Bugzilla 3464, test namespace85 -- make sure LRE's
        // namespace is asserted even if default, since xsl:element
        // may have changed the context.
        rhandler.startPrefixMapping(getPrefix(), getNamespace());
        // Add namespace declarations.
        executeNSDecls(transformer);
        rhandler.startElement(getNamespace(), getLocalName(), getRawName());
    } catch (SAXException se) {
        throw new TransformerException(se);
    }
    /*
         * If we make it to here we have done a successful startElement()
         * we will do an endElement() call for balance, no matter what happens
         * in the middle.  
         */
    // tException remembers if we had an exception "in the middle"
    TransformerException tException = null;
    try {
        // Process any possible attributes from xsl:use-attribute-sets first
        super.execute(transformer);
        // Process the list of avts next
        if (null != m_avts) {
            int nAttrs = m_avts.size();
            for (int i = (nAttrs - 1); i >= 0; i--) {
                AVT avt = (AVT) m_avts.get(i);
                XPathContext xctxt = transformer.getXPathContext();
                int sourceNode = xctxt.getCurrentNode();
                String stringedValue = avt.evaluate(xctxt, sourceNode, this);
                if (null != stringedValue) {
                    // Important Note: I'm not going to check for excluded namespace 
                    // prefixes here.  It seems like it's too expensive, and I'm not 
                    // even sure this is right.  But I could be wrong, so this needs 
                    // to be tested against other implementations.
                    rhandler.addAttribute(avt.getURI(), avt.getName(), avt.getRawName(), "CDATA", stringedValue, false);
                }
            }
        // end for
        }
        // Now process all the elements in this subtree
        // TODO: Process m_extensionElementPrefixes && m_attributeSetsNames
        transformer.executeChildTemplates(this, true);
    } catch (TransformerException te) {
        // thrown in finally to prevent original exception consumed by subsequent exceptions
        tException = te;
    } catch (SAXException se) {
        tException = new TransformerException(se);
    }
    try {
        /* we need to do this endElement() to balance the
             * successful startElement() call even if 
             * there was an exception in the middle.
             * Otherwise an exception in the middle could cause a system to hang.
             */
        rhandler.endElement(getNamespace(), getLocalName(), getRawName());
    } catch (SAXException se) {
        /* we did call endElement(). If thee was an exception
             * in the middle throw that one, otherwise if there
             * was an exception from endElement() throw that one.
             */
        if (tException != null)
            throw tException;
        else
            throw new TransformerException(se);
    }
    /* If an exception was thrown in the middle but not with startElement() or
         * or endElement() then its time to let it percolate.
         */
    if (tException != null)
        throw tException;
    unexecuteNSDecls(transformer);
    // JJK Bugzilla 3464, test namespace85 -- balance explicit start.
    try {
        rhandler.endPrefixMapping(getPrefix());
    } catch (SAXException se) {
        throw new TransformerException(se);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) XPathContext(org.apache.xpath.XPathContext) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 15 with SerializationHandler

use of org.apache.xml.serializer.SerializationHandler in project j2objc by google.

the class ElemTextLiteral method execute.

/**
   * Copy the text literal to the result tree.
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    try {
        SerializationHandler rth = transformer.getResultTreeHandler();
        if (m_disableOutputEscaping) {
            rth.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");
        }
        rth.characters(m_ch, 0, m_ch.length);
        if (m_disableOutputEscaping) {
            rth.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");
        }
    } catch (SAXException se) {
        throw new TransformerException(se);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Aggregations

SerializationHandler (org.apache.xml.serializer.SerializationHandler)38 TransformerException (javax.xml.transform.TransformerException)28 SAXException (org.xml.sax.SAXException)20 XPathContext (org.apache.xpath.XPathContext)14 DTM (org.apache.xml.dtm.DTM)6 ToXMLSAXHandler (org.apache.xml.serializer.ToXMLSAXHandler)6 XObject (org.apache.xpath.objects.XObject)6 DTMIterator (org.apache.xml.dtm.DTMIterator)5 IOException (java.io.IOException)4 ContentHandler (org.xml.sax.ContentHandler)4 LexicalHandler (org.xml.sax.ext.LexicalHandler)4 StreamResult (javax.xml.transform.stream.StreamResult)3 StringWriter (java.io.StringWriter)2 Vector (java.util.Vector)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SourceLocator (javax.xml.transform.SourceLocator)2 DOMResult (javax.xml.transform.dom.DOMResult)2 SAXResult (javax.xml.transform.sax.SAXResult)2 ElemTemplateElement (org.apache.xalan.templates.ElemTemplateElement)2 ElemTextLiteral (org.apache.xalan.templates.ElemTextLiteral)2