Search in sources :

Example 16 with Serializer

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

the class SerializerSwitcher method switchSerializerIfHTML.

/**
   * Switch to HTML serializer if element is HTML
   *
   *
   * @param transformer Non-null transformer instance
   * @param ns Namespace URI of the element
   * @param localName Local part of name of element
   *
   * @throws TransformerException
   */
public static void switchSerializerIfHTML(TransformerImpl transformer, String ns, String localName) throws TransformerException {
    if (null == transformer)
        return;
    if (((null == ns) || (ns.length() == 0)) && localName.equalsIgnoreCase("html")) {
        // Access at level of hashtable to see if the method has been set.
        if (null != transformer.getOutputPropertyNoDefault(OutputKeys.METHOD))
            return;
        // Getting the output properties this way won't cause a clone of 
        // the properties.
        Properties prevProperties = transformer.getOutputFormat().getProperties();
        // We have to make sure we get an output properties with the proper 
        // defaults for the HTML method.  The easiest way to do this is to 
        // have the OutputProperties class do it.
        OutputProperties htmlOutputProperties = new OutputProperties(Method.HTML);
        htmlOutputProperties.copyFrom(prevProperties, true);
        Properties htmlProperties = htmlOutputProperties.getProperties();
        try {
            //        Serializer oldSerializer = transformer.getSerializer();
            Serializer oldSerializer = null;
            if (null != oldSerializer) {
                Serializer serializer = SerializerFactory.getSerializer(htmlProperties);
                Writer writer = oldSerializer.getWriter();
                if (null != writer)
                    serializer.setWriter(writer);
                else {
                    OutputStream os = oldSerializer.getOutputStream();
                    if (null != os)
                        serializer.setOutputStream(os);
                }
                //          transformer.setSerializer(serializer);
                ContentHandler ch = serializer.asContentHandler();
                transformer.setContentHandler(ch);
            }
        } catch (java.io.IOException e) {
            throw new TransformerException(e);
        }
    }
}
Also used : OutputStream(java.io.OutputStream) OutputProperties(org.apache.xalan.templates.OutputProperties) Properties(java.util.Properties) OutputProperties(org.apache.xalan.templates.OutputProperties) Writer(java.io.Writer) ContentHandler(org.xml.sax.ContentHandler) TransformerException(javax.xml.transform.TransformerException) Serializer(org.apache.xml.serializer.Serializer)

Aggregations

Serializer (org.apache.xml.serializer.Serializer)16 OutputStream (java.io.OutputStream)8 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)6 StringWriter (java.io.StringWriter)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Writer (java.io.Writer)6 DOM3Serializer (org.apache.xml.serializer.DOM3Serializer)6 DOMException (org.w3c.dom.DOMException)6 LSException (org.w3c.dom.ls.LSException)6 LSSerializer (org.w3c.dom.ls.LSSerializer)6 FileOutputStream (java.io.FileOutputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 URLConnection (java.net.URLConnection)4 Properties (java.util.Properties)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 OutputProperties (org.apache.xalan.templates.OutputProperties)4 DTDHandler (org.xml.sax.DTDHandler)4 SAXException (org.xml.sax.SAXException)4