Search in sources :

Example 26 with SerializationHandler

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

the class TransformerImpl method transform.

/**
   * Process the source tree to the output result.
   * @param xmlSource  The input for the source tree.
   * @param outputTarget The output source target.
   * @param shouldRelease  Flag indicating whether to release DTMManager. 
   *
   * @throws TransformerException
   */
public void transform(Source xmlSource, Result outputTarget, boolean shouldRelease) throws TransformerException {
    synchronized (m_reentryGuard) {
        SerializationHandler xoh = createSerializationHandler(outputTarget);
        this.setSerializationHandler(xoh);
        m_outputTarget = outputTarget;
        transform(xmlSource, shouldRelease);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler)

Example 27 with SerializationHandler

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

Example 28 with SerializationHandler

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

the class TransformerImpl method transformToString.

/**
   * Take the contents of a template element, process it, and
   * convert it to a string.
   *
   * @param elem The parent element whose children will be output
   * as a string.
   *
   * @return The stringized result of executing the elements children.
   *
   * @throws TransformerException
   * @xsl.usage advanced
   */
public String transformToString(ElemTemplateElement elem) throws TransformerException {
    ElemTemplateElement firstChild = elem.getFirstChildElem();
    if (null == firstChild)
        return "";
    if (elem.hasTextLitOnly() && m_optimizer) {
        return ((ElemTextLiteral) firstChild).getNodeValue();
    }
    // Save the current result tree handler.
    SerializationHandler savedRTreeHandler = this.m_serializationHandler;
    // Create a Serializer object that will handle the SAX events 
    // and build the ResultTreeFrag nodes.
    StringWriter sw = (StringWriter) m_stringWriterObjectPool.getInstance();
    m_serializationHandler = (ToTextStream) m_textResultHandlerObjectPool.getInstance();
    if (null == m_serializationHandler) {
        // if we didn't get one from the pool, go make a new one
        Serializer serializer = org.apache.xml.serializer.SerializerFactory.getSerializer(m_textformat.getProperties());
        m_serializationHandler = (SerializationHandler) serializer;
    }
    m_serializationHandler.setTransformer(this);
    m_serializationHandler.setWriter(sw);
    String result;
    try {
        /* Don't call startDocument, the SerializationHandler  will
         * generate its own internal startDocument call anyways
         */
        // this.m_serializationHandler.startDocument();
        // Do the transformation of the child elements.
        executeChildTemplates(elem, true);
        this.m_serializationHandler.endDocument();
        result = sw.toString();
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    } finally {
        sw.getBuffer().setLength(0);
        try {
            sw.close();
        } catch (Exception ioe) {
        }
        m_stringWriterObjectPool.freeInstance(sw);
        m_serializationHandler.reset();
        m_textResultHandlerObjectPool.freeInstance(m_serializationHandler);
        // Restore the previous result tree handler.
        m_serializationHandler = savedRTreeHandler;
    }
    return result;
}
Also used : ElemTextLiteral(org.apache.xalan.templates.ElemTextLiteral) StringWriter(java.io.StringWriter) SAXException(org.xml.sax.SAXException) SerializationHandler(org.apache.xml.serializer.SerializationHandler) ElemTemplateElement(org.apache.xalan.templates.ElemTemplateElement) TransformerException(javax.xml.transform.TransformerException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Serializer(org.apache.xml.serializer.Serializer)

Example 29 with SerializationHandler

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

the class TransformerImpl method transform.

/**
   * Process the source tree to the output result.
   * @param xmlSource  The input for the source tree.
   * @param outputTarget The output source target.
   * @param shouldRelease  Flag indicating whether to release DTMManager. 
   *
   * @throws TransformerException
   */
public void transform(Source xmlSource, Result outputTarget, boolean shouldRelease) throws TransformerException {
    synchronized (m_reentryGuard) {
        SerializationHandler xoh = createSerializationHandler(outputTarget);
        this.setSerializationHandler(xoh);
        m_outputTarget = outputTarget;
        transform(xmlSource, shouldRelease);
    }
}
Also used : SerializationHandler(org.apache.xml.serializer.SerializationHandler)

Example 30 with SerializationHandler

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

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

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