Java XML Transform getThreadedIdentityTransformer( boolean omitXmlDeclaration)

Here you can find the source of getThreadedIdentityTransformer( boolean omitXmlDeclaration)

Description

Get a thread-safe Transformer without an assigned transform.

License

Open Source License

Parameter

Parameter Description
omitXmlDeclaration true if you don't want it

Exception

Parameter Description
TransformerConfigurationException couldn't get it

Return

an "identity" Transformer assigned to the current thread

Declaration

public static Transformer getThreadedIdentityTransformer(
        boolean omitXmlDeclaration)
        throws TransformerConfigurationException 

Method Source Code

//package com.java2s;
/**//from   w  ww .  j a v  a2s  .  co m
 * Copyright 2006 OCLC Online Computer Library Center 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.
 */

import javax.xml.transform.*;

import javax.xml.transform.stream.StreamSource;

import java.io.InputStream;

public class Main {
    private static TransformerFactory tFactory = TransformerFactory
            .newInstance();

    /**
     * Get a thread-safe Transformer without an assigned transform. This is useful
     * for transforming a DOM Document into XML text.
     *
     * @param omitXmlDeclaration true if you don't want it
     * @return an "identity" Transformer assigned to the current thread
     * @throws TransformerConfigurationException
     *          couldn't get it
     */
    public static Transformer getThreadedIdentityTransformer(
            boolean omitXmlDeclaration)
            throws TransformerConfigurationException {
        return getTransformer(omitXmlDeclaration, null);
    }

    /**
     * Get a thread-safe Transformer.
     *
     * @param omitXmlDeclaration true if you don't want it
     * @param xslURL             todo: explain
     * @return a thread-safe Transformer
     * @throws TransformerConfigurationException
     *
     */
    public static Transformer getTransformer(boolean omitXmlDeclaration,
            String xslURL) throws TransformerConfigurationException {
        return getTransformer(omitXmlDeclaration, true, xslURL);
    }

    /**
     * @param omitXmlDeclaration
     * @param standalone
     * @param xslURL
     * @return a Transformer for the specified XSL document
     * @throws TransformerConfigurationException
     *
     */
    public static Transformer getTransformer(boolean omitXmlDeclaration,
            boolean standalone, String xslURL)
            throws TransformerConfigurationException {
        Transformer transformer = null;
        if (xslURL == null) {
            transformer = tFactory.newTransformer(); // "never null"
        } else {
            Source xslSource = null;
            if (xslURL.startsWith("file://")) {
                InputStream is = Thread.currentThread()
                        .getContextClassLoader()
                        .getResourceAsStream(xslURL.substring(6));
                xslSource = new StreamSource(is);
            } else {
                xslSource = new StreamSource(xslURL);
            }
            transformer = tFactory.newTransformer(xslSource);
        }
        //      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE,
                standalone ? "yes" : "no");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                omitXmlDeclaration ? "yes" : "no");
        return transformer;
    }
}

Related

  1. createTransformerHandler(int indent)
  2. createTXT(Transformer xf, Node doc)
  3. exceptionToString(TransformerException exception, boolean showSystemId)
  4. getDefaultTransformerFactoryThreadLocal()
  5. getNewTransformer()
  6. getThreadedIdentityTransformer(final boolean omitXmlDeclaration)
  7. getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone, final Map threadMap, final String xslURL)
  8. getTransformedText(File doc, File xsl, Object[] params)
  9. getTransformer()