Java tutorial
//package com.java2s; /** * Copyright (C) 2007 Asterios Raptis * * 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.io.File; import java.io.OutputStream; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class Main { /** The Constant TRANSFORMER_FACTORY. */ private static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance(); /** * Transform. * * @param xmlInputFile * the xml input file * @param xsltInputFile * the xslt input file * @param outputStream * the output stream * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception * @throws TransformerException * the transformer exception */ public static void transform(String xmlInputFile, String xsltInputFile, OutputStream outputStream) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { File xmlFile = new File(xmlInputFile); File xsltFile = new File(xsltInputFile); transform(xmlFile, xsltFile, outputStream); } /** * Transform. * * @param xmlFile * the xml file * @param xsltFile * the xslt file * @param outputStream * the output stream * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception * @throws TransformerException * the transformer exception */ public static void transform(File xmlFile, File xsltFile, OutputStream outputStream) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { Source xmlSource = new StreamSource(xmlFile); Source xsltSource = new StreamSource(xsltFile); transform(xmlSource, xsltSource, outputStream); } /** * Transform. * * @param xmlSource * the xml source * @param xsltSource * the xslt source * @param outputStream * the output stream * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception * @throws TransformerException * the transformer exception */ public static void transform(Source xmlSource, Source xsltSource, OutputStream outputStream) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { Transformer transformer = getTransformer(xsltSource); transformer.transform(xmlSource, new StreamResult(outputStream)); } /** * Gets a new instance from a Transformer object. * * @param xsltSource * the xslt source * @return the transformer * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception */ public static Transformer getTransformer(Source xsltSource) throws TransformerFactoryConfigurationError, TransformerConfigurationException { return TRANSFORMER_FACTORY.newTransformer(xsltSource); } /** * Gets the transformer. * * @param xsltFile * the xslt file * @return the transformer * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception */ public static Transformer getTransformer(File xsltFile) throws TransformerFactoryConfigurationError, TransformerConfigurationException { return getTransformer(new StreamSource(xsltFile)); } /** * Gets the transformer. * * @param xsltInputFile * the xslt input file * @return the transformer * @throws TransformerFactoryConfigurationError * the transformer factory configuration error * @throws TransformerConfigurationException * the transformer configuration exception */ public static Transformer getTransformer(String xsltInputFile) throws TransformerFactoryConfigurationError, TransformerConfigurationException { return getTransformer(new File(xsltInputFile)); } }