Java XML Transform newTransformerHandler(final SAXTransformerFactory tf)

Here you can find the source of newTransformerHandler(final SAXTransformerFactory tf)

Description

Set up transformer to output full XML doc.

License

Apache License

Parameter

Parameter Description
tf see #saxTransformerFactory()

Return

Transformer that is fully set up. But here we get TransformerHandler first from TransformerFactory.

Declaration

public static TransformerHandler newTransformerHandler(final SAXTransformerFactory tf)
        throws TransformerConfigurationException 

Method Source Code


//package com.java2s;
/*//from   w  w w.  j  a va2  s .  c  om
  This file is licensed to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

import java.nio.charset.StandardCharsets;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;

public class Main {
    /** ask to pretty-print XML (indentation) */
    public static final String XSLT_INDENT_PROP = "{http://xml.apache.org/xslt}indent-amount";

    /** Set up transformer to output full XML doc.
     *
     * @param tf see {@link #saxTransformerFactory()}
     * @return Transformer that is fully set up. But here we get TransformerHandler first from TransformerFactory.
     */
    public static TransformerHandler newTransformerHandler(final SAXTransformerFactory tf)
            throws TransformerConfigurationException {
        final TransformerHandler resultHandler = tf.newTransformerHandler();
        final Transformer transformer = resultHandler.getTransformer();
        setUtfEncoding(transformer);
        setIndentFlag(transformer);
        setTransformerIndent(transformer);
        return resultHandler;
    }

    /** ask transformer to use UTF-8
     * @param transformer will encode output as UTF-8
     */
    public static void setUtfEncoding(final Transformer transformer) {
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
    }

    /** ask transformer to pretty-print the output
     * @param transformer will indent output
     */
    public static void setIndentFlag(final Transformer transformer) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // see http://www.w3.org/TR/xslt#output
    }

    /** ask transformer to pretty-print the output: works with Java built-in XML engine
     * @param transformer will add indent property
     */
    public static void setTransformerIndent(final Transformer transformer) {
        try {
            transformer.setOutputProperty(XSLT_INDENT_PROP, "4");
        } catch (final IllegalArgumentException e) {
            System.err.println("indent-amount not supported: {}" + e.toString()); // ignore error, don't print stack-trace
        }
    }
}

Related

  1. newTransformerFactory()
  2. newTransformerFactory()
  3. newTransformerFactory()
  4. newTransformerHandler()
  5. newTransformerHandler()
  6. serializeXML(Transformer transformer, Source input)
  7. transform(byte[] doc, URL xsltUrl, OutputStream out)
  8. transform(byte[] source, InputStream xslInputStream, File output)
  9. transform(File inputFile, Transformer transformer, Writer out)