Java XML Transform applyXSLTTransform(final String xslt, final Reader input, final Writer output)

Here you can find the source of applyXSLTTransform(final String xslt, final Reader input, final Writer output)

Description

apply XSLT Transform

License

Open Source License

Declaration

public final static void applyXSLTTransform(final String xslt, final Reader input, final Writer output)
            throws TransformerException 

Method Source Code

//package com.java2s;
/**//from   w w  w .  ja  v a 2 s .c  o m
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

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

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import java.io.*;
import java.util.*;

public class Main {
    public final static String OUTPUT_PROPERTY_YES = "yes";
    public final static String OUTPUT_PROPERTY_NO = "no";
    private final static TransformerFactory transformerFactory = TransformerFactory.newInstance();

    public final static void applyXSLTTransform(final String xslt, final Reader input, final Writer output)
            throws TransformerException {
        applyXSLTTransform(new StringReader(xslt), input, output);
    }

    public final static void applyXSLTTransform(final Reader xslt, final Reader input, final Writer output)
            throws TransformerException {
        final Transformer transformer = transformerFactory.newTransformer(new StreamSource(xslt));

        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OUTPUT_PROPERTY_YES);
        transformer.setOutputProperty(OutputKeys.INDENT, OUTPUT_PROPERTY_NO);

        transformer.transform(new StreamSource(input), new StreamResult(output));
    }

    public final static void setOutputProperty(final Properties prop, final String key, final boolean b) {
        prop.setProperty(key, toOutputPropertyValue(b));
    }

    public final static void setOutputProperty(final Transformer t, final String key, final boolean b) {
        t.setOutputProperty(key, toOutputPropertyValue(b));
    }

    public final static String toOutputPropertyValue(final boolean b) {
        return b ? OUTPUT_PROPERTY_YES : OUTPUT_PROPERTY_NO;
    }
}

Related

  1. buildXmlTransformer()
  2. createIndentingTransformer()
  3. createOutputTransformer(String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount)
  4. createPrettyTransformer(int indent)