Java XML Transform getXmlTransformer()

Here you can find the source of getXmlTransformer()

Description

get Xml Transformer

License

Apache License

Declaration

private static Transformer getXmlTransformer() throws Exception 

Method Source Code

//package com.java2s;
/*/* w  ww  .j a  v  a2s . c  o m*/
 * Copyright 2013 Keith D Swenson
 *
 * Licensed 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.
 *
 * Contributors Include: Shamim Quader, Sameer Pradhan, Kumar Raja, Jim Farris,
 * Sandia Yang, CY Chen, Rajiv Onat, Neal Wang, Dennis Tam, Shikha Srivastava,
 * Anamika Chaudhari, Ajay Kakkar, Rajeev Rastogi
 */

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

public class Main {
    private static TransformerFactory transformerFactory = null;
    private static final Long mutex2 = new Long(2);

    private static Transformer getXmlTransformer() throws Exception {
        /*
         * CDATA_SECTION_ELEMENTS | cdata-section-elements = expanded names.
         * DOCTYPE_PUBLIC | doctype-public = string. DOCTYPE_SYSTEM |
         * doctype-system = string. ENCODING | encoding = string. INDENT |
         * indent = "yes" | "no". MEDIA_TYPE | media-type = string. METHOD |
         * method = "xml" | "html" | "text" | expanded name.
         * OMIT_XML_DECLARATION | omit-xml-declaration = "yes" | "no".
         * STANDALONE | standalone = "yes" | "no". VERSION | version = nmtoken.
         */

        initTransformer();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        try {
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        } catch (IllegalArgumentException e) {
            // If the property is not supported, and is not qualified with a
            // namespace then
            // it throws IllegalArgumentException. we do not have to re-throw
            // this exception.
        }
        return transformer;
    }

    private static final void initTransformer()
            throws TransformerFactoryConfigurationError, TransformerConfigurationException {
        if (transformerFactory == null) {
            synchronized (mutex2) {
                transformerFactory = TransformerFactory.newInstance();
            }
        }
    }
}

Related

  1. getTransformParameterSpec(final Node transformNode, final String namespacePrefix)
  2. getTransforms(List list)
  3. getXmlSecureTransform()
  4. getXmlSecureTransformerFactory()
  5. getXMLTransformer()
  6. getXsltTransformer(File xsltFile)
  7. internalTransformWithParams(Reader doc, Templates templates, Result r, boolean trace, String[] params)
  8. newTransformer()
  9. newTransformer()