Search in sources :

Example 1 with ToXMLSAXHandler

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

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

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

the class TransformerImpl method executeChildTemplates.

/**
      * Execute each of the children of a template element.
      *
      * @param elem The ElemTemplateElement that contains the children
      * that should execute.
      * @param handler The ContentHandler to where the result events
      * should be fed.
      *
      * @throws TransformerException
      * @xsl.usage advanced
      */
public void executeChildTemplates(ElemTemplateElement elem, ContentHandler handler) throws TransformerException {
    SerializationHandler xoh = this.getSerializationHandler();
    // These may well not be the same!  In this case when calling
    // the Redirect extension, it has already set the ContentHandler
    // in the Transformer.
    SerializationHandler savedHandler = xoh;
    try {
        xoh.flushPending();
        // %REVIEW% Make sure current node is being pushed.
        LexicalHandler lex = null;
        if (handler instanceof LexicalHandler) {
            lex = (LexicalHandler) handler;
        }
        m_serializationHandler = new ToXMLSAXHandler(handler, lex, savedHandler.getEncoding());
        m_serializationHandler.setTransformer(this);
        executeChildTemplates(elem, true);
    } catch (TransformerException e) {
        throw e;
    } catch (SAXException se) {
        throw new TransformerException(se);
    } finally {
        m_serializationHandler = savedHandler;
    }
}
Also used : LexicalHandler(org.xml.sax.ext.LexicalHandler) SerializationHandler(org.apache.xml.serializer.SerializationHandler) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 4 with ToXMLSAXHandler

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

the class TransformerImpl method setContentHandler.

// ======== End Transformer Implementation ========  
/**
   * Set the content event handler.
   *
   * NEEDSDOC @param handler
   * @throws java.lang.NullPointerException If the handler
   *            is null.
   * @see org.xml.sax.XMLReader#setContentHandler
   */
public void setContentHandler(ContentHandler handler) {
    if (handler == null) {
        //"Null content handler");
        throw new NullPointerException(XSLMessages.createMessage(XSLTErrorResources.ER_NULL_CONTENT_HANDLER, null));
    } else {
        m_outputContentHandler = handler;
        if (null == m_serializationHandler) {
            ToXMLSAXHandler h = new ToXMLSAXHandler();
            h.setContentHandler(handler);
            h.setTransformer(this);
            m_serializationHandler = h;
        } else
            m_serializationHandler.setContentHandler(handler);
    }
}
Also used : ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler)

Example 5 with ToXMLSAXHandler

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

the class TransformerImpl method createSerializationHandler.

/**
     * Create a ContentHandler from a Result object and an OutputProperties.
     *
     * @param outputTarget Where the transform result should go,
     * should not be null.
     * @param format The OutputProperties object that will contain
     * instructions on how to serialize the output.
     *
     * @return A valid ContentHandler that will create the
     * result tree when it is fed SAX events.
     *
     * @throws TransformerException
     */
public SerializationHandler createSerializationHandler(Result outputTarget, OutputProperties format) throws TransformerException {
    SerializationHandler xoh;
    // If the Result object contains a Node, then create
    // a ContentHandler that will add nodes to the input node.
    org.w3c.dom.Node outputNode = null;
    if (outputTarget instanceof DOMResult) {
        outputNode = ((DOMResult) outputTarget).getNode();
        org.w3c.dom.Node nextSibling = ((DOMResult) outputTarget).getNextSibling();
        org.w3c.dom.Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument();
        } else {
            boolean isSecureProcessing = m_stylesheetRoot.isSecureProcessing();
            doc = org.apache.xml.utils.DOMHelper.createDocument(isSecureProcessing);
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            handler.setNextSibling(nextSibling);
        String encoding = format.getProperty(OutputKeys.ENCODING);
        xoh = new ToXMLSAXHandler(handler, (LexicalHandler) handler, encoding);
    } else if (outputTarget instanceof SAXResult) {
        ContentHandler handler = ((SAXResult) outputTarget).getHandler();
        if (null == handler)
            throw new IllegalArgumentException("handler can not be null for a SAXResult");
        LexicalHandler lexHandler;
        if (handler instanceof LexicalHandler)
            lexHandler = (LexicalHandler) handler;
        else
            lexHandler = null;
        String encoding = format.getProperty(OutputKeys.ENCODING);
        String method = format.getProperty(OutputKeys.METHOD);
        ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding);
        toXMLSAXHandler.setShouldOutputNSAttr(false);
        xoh = toXMLSAXHandler;
        String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
        String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM);
        if (systemID != null)
            xoh.setDoctypeSystem(systemID);
        if (publicID != null)
            xoh.setDoctypePublic(publicID);
        if (handler instanceof TransformerClient) {
            XalanTransformState state = new XalanTransformState();
            ((TransformerClient) handler).setTransformState(state);
            ((ToSAXHandler) xoh).setTransformState(state);
        }
    } else // result tree to either a stream or a writer.
    if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            SerializationHandler serializer = (SerializationHandler) SerializerFactory.getSerializer(format.getProperties());
            if (null != sresult.getWriter())
                serializer.setWriter(sresult.getWriter());
            else if (null != sresult.getOutputStream())
                serializer.setOutputStream(sresult.getOutputStream());
            else if (null != sresult.getSystemId()) {
                String fileURL = sresult.getSystemId();
                if (fileURL.startsWith("file:///")) {
                    if (fileURL.substring(8).indexOf(":") > 0)
                        fileURL = fileURL.substring(8);
                    else
                        fileURL = fileURL.substring(7);
                } else if (fileURL.startsWith("file:/")) {
                    if (fileURL.substring(6).indexOf(":") > 0)
                        fileURL = fileURL.substring(6);
                    else
                        fileURL = fileURL.substring(5);
                }
                m_outputStream = new java.io.FileOutputStream(fileURL);
                serializer.setOutputStream(m_outputStream);
                xoh = serializer;
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            // handler = serializer.asContentHandler();
            //  this.setSerializer(serializer);
            xoh = serializer;
        }//        }
         catch (IOException ioe) {
            throw new TransformerException(ioe);
        }
    } else {
        //"Can't transform to a Result of type "
        throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
    //+ outputTarget.getClass().getName()
    //+ "!");
    }
    // before we forget, lets make the created handler hold a reference
    // to the current TransformImpl object
    xoh.setTransformer(this);
    SourceLocator srcLocator = getStylesheet();
    xoh.setSourceLocator(srcLocator);
    return xoh;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SerializationHandler(org.apache.xml.serializer.SerializationHandler) DOMBuilder(org.apache.xml.utils.DOMBuilder) ContentHandler(org.xml.sax.ContentHandler) LexicalHandler(org.xml.sax.ext.LexicalHandler) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) SAXResult(javax.xml.transform.sax.SAXResult) SourceLocator(javax.xml.transform.SourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) XObject(org.apache.xpath.objects.XObject) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler)

Aggregations

ToXMLSAXHandler (org.apache.xml.serializer.ToXMLSAXHandler)8 TransformerException (javax.xml.transform.TransformerException)6 SerializationHandler (org.apache.xml.serializer.SerializationHandler)6 ContentHandler (org.xml.sax.ContentHandler)4 SAXException (org.xml.sax.SAXException)4 LexicalHandler (org.xml.sax.ext.LexicalHandler)4 IOException (java.io.IOException)2 SourceLocator (javax.xml.transform.SourceLocator)2 DOMResult (javax.xml.transform.dom.DOMResult)2 SAXResult (javax.xml.transform.sax.SAXResult)2 StreamResult (javax.xml.transform.stream.StreamResult)2 ToSAXHandler (org.apache.xml.serializer.ToSAXHandler)2 DOMBuilder (org.apache.xml.utils.DOMBuilder)2 SAXSourceLocator (org.apache.xml.utils.SAXSourceLocator)2 XPathContext (org.apache.xpath.XPathContext)2 XObject (org.apache.xpath.objects.XObject)2