Java XML Transform getTransformer(boolean omitXmlDeclaration, String xslURL)

Here you can find the source of getTransformer(boolean omitXmlDeclaration, String xslURL)

Description

Get a thread-safe Transformer.

License

Open Source License

Parameter

Parameter Description
omitXmlDeclaration true if you don't want it
xslURL todo: explain

Exception

Parameter Description
TransformerConfigurationException an exception

Return

a thread-safe Transformer

Declaration

public static Transformer getTransformer(boolean omitXmlDeclaration,
        String xslURL) throws TransformerConfigurationException 

Method Source Code

//package com.java2s;
/**//from   ww  w .j av  a  2 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 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.
     *
     * @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. getTransformer()
  2. getTransformer()
  3. getTransformer()
  4. getTransformer()
  5. getTransformer()
  6. getTransformer(boolean standalone, boolean indent, int indentNumber, boolean omitXmlDeclaration)
  7. getTransformer(final InputStream xsl)
  8. getTransformer(Source xsltSource)
  9. getTransformer(StreamSource streamSource)