Java XML Transform Usage xsl(File inFile, File outFile, InputStream xslStream)

Here you can find the source of xsl(File inFile, File outFile, InputStream xslStream)

Description

xsl

License

Apache License

Declaration

public static void xsl(File inFile, File outFile, InputStream xslStream)
            throws Exception 

Method Source Code

//package com.java2s;
/*/*  w  ww  .ja v a 2 s. co m*/
 * Copyright 2005-2009 the original author or authors.
 * 
 * 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.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.SourceLocator;
import javax.xml.transform.Templates;
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;

public class Main {
    public static void xsl(File inFile, File outFile, InputStream xslStream)
            throws Exception {
        try {
            // Create transformer factory
            TransformerFactory factory = TransformerFactory.newInstance();

            // Use the factory to create a template containing the xsl file
            Templates template = factory.newTemplates(new StreamSource(
                    xslStream));

            // Use the template to create a transformer
            Transformer xformer = template.newTransformer();

            // Prepare the input and output files
            Source source = new StreamSource(new FileInputStream(inFile));
            Result result = new StreamResult(new FileOutputStream(outFile));

            // Apply the xsl file to the source file and write the result to the
            // output file
            xformer.transform(source, result);
        } catch (TransformerException e) {
            // An error occurred while applying the XSL file
            // Get location of error in input file
            SourceLocator locator = e.getLocator();
            int col = locator.getColumnNumber();
            int line = locator.getLineNumber();
            throw new Exception(String.format(
                    "XSL exception line %d col %d message: %s", line, col,
                    e.getMessage()));
        }
    }
}

Related

  1. toXMLInputSource(StreamSource in)
  2. unwrapException(Throwable t)
  3. verifySignature(Element element, PublicKey validatingKey)
  4. writeElementContent(XMLInputFactory inputFactory, XMLStreamWriter writer, Element element)
  5. writeXhtml(InputStream inputStream, Writer writer, Templates... templates)