Java XML Transform getThreadedIdentityTransformer(final boolean omitXmlDeclaration)

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

Description

Get a thread-safe Transformer without an assigned transform.

License

Open Source License

Parameter

Parameter Description
omitXmlDeclaration a parameter

Exception

Parameter Description
TransformerConfigurationException an exception

Return

an "identity" Transformer assigned to the current thread

Declaration

public static Transformer getThreadedIdentityTransformer(final boolean omitXmlDeclaration)
        throws TransformerConfigurationException 

Method Source Code

//package com.java2s;
/**//from  w ww .j  a v a2  s  .  c o  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 java.util.Map;

import javax.xml.transform.OutputKeys;

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

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.stream.StreamSource;

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
     * @return an "identity" Transformer assigned to the current thread
     * @throws TransformerConfigurationException
     */
    public static Transformer getThreadedIdentityTransformer(final boolean omitXmlDeclaration)
            throws TransformerConfigurationException {
        return getThreadedTransformer(omitXmlDeclaration, null, (String) null);
    }

    /**
     * Get a thread-safe Transformer without an assigned transform. This is useful for transforming a DOM Document into
     * XML text.
     * 
     * @param omitXmlDeclaration
     * @param standalone
     * @return an Identity Transformer
     * @throws TransformerConfigurationException
     */
    public static Transformer getThreadedIdentityTransformer(final boolean omitXmlDeclaration,
            final boolean standalone) throws TransformerConfigurationException {
        return getThreadedTransformer(omitXmlDeclaration, standalone, null, (String) null);
    }

    private static Transformer getThreadedTransformer(final boolean omitXmlDeclaration, final boolean standalone,
            final Map<Thread, Transformer> threadMap, final String xslURL)
            throws TransformerConfigurationException {
        final Thread currentThread = Thread.currentThread();
        Transformer transformer = null;
        if (threadMap != null) {
            transformer = threadMap.get(currentThread);
        }
        if (transformer == null) {
            if (xslURL == null) {
                transformer = tFactory.newTransformer(); // "never null"
            } else {
                transformer = tFactory.newTransformer(new StreamSource(xslURL));
            }
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            if (threadMap != null) {
                threadMap.put(currentThread, transformer);
            }
        }
        transformer.setOutputProperty(OutputKeys.STANDALONE, standalone ? "yes" : "no");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
        return transformer;
    }

    /**
     * Get a thread-safe Transformer.
     * 
     * @param omitXmlDeclaration
     * @param threadMap
     * @param xslURL
     * @return a thread-safe Transformer
     * @throws TransformerConfigurationException
     */
    public static Transformer getThreadedTransformer(final boolean omitXmlDeclaration,
            final Map<Thread, Transformer> threadMap, final String xslURL)
            throws TransformerConfigurationException {
        return getThreadedTransformer(omitXmlDeclaration, true, threadMap, xslURL);
    }
}

Related

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