Java XML Transform createTransformer()

Here you can find the source of createTransformer()

Description

Create and returns a new Transformer.

License

Open Source License

Declaration

public static Transformer createTransformer() 

Method Source Code

//package com.java2s;
/*/*from ww  w.j  a  v a 2  s .  co  m*/
 * Copyright 2010, 2011 Institut Pasteur.
 * 
 * This file is part of ICY.
 * 
 * ICY is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ICY is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
 */

import javax.xml.parsers.DocumentBuilderFactory;

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

public class Main {
    private static DocumentBuilderFactory docBuilderFactory = null;
    private static TransformerFactory transformerFactory = null;

    /**
     * Create and returns a new Transformer.
     */
    public static Transformer createTransformer() {
        initFactories();

        final Transformer result;

        try {
            result = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            return null;
        }

        result.setOutputProperty(OutputKeys.METHOD, "xml");
        result.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        result.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        result.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        result.setOutputProperty(OutputKeys.INDENT, "yes");

        return result;
    }

    private static synchronized void initFactories() {
        // initialize static factories
        if (docBuilderFactory == null)
            docBuilderFactory = DocumentBuilderFactory.newInstance();
        if (transformerFactory == null)
            transformerFactory = TransformerFactory.newInstance();
    }
}

Related

  1. buildXmlTransformer()
  2. createIndentingTransformer()
  3. createOutputTransformer(String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount)
  4. createPrettyTransformer(int indent)
  5. createTransformer()
  6. createTransformer()
  7. createTransformer()
  8. createTransformer(final File xsltFile)
  9. createTransformerHandler(int indent)