com.squeakysand.commons.xml.XsltHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of squeakysand-commons Show documentation
Show all versions of squeakysand-commons Show documentation
Classes, interfaces and enums that assist with everyday Java development tasks.
The newest version!
/*
* Copyright 2010-2012 Craig S. Dickson (http://craigsdickson.com)
*
* 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.
*/
package com.squeakysand.commons.xml;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class XsltHelper {
private static final Logger LOG = LoggerFactory.getLogger(XsltHelper.class);
public static void transform(File xslFile, File xmlFile, File outputFile) throws XmlProcessingException {
InputStream xslInput = null;
InputStream xmlInput = null;
OutputStream output = null;
try {
xslInput = new FileInputStream(xslFile);
xmlInput = new FileInputStream(xmlFile);
output = new FileOutputStream(outputFile);
transform(xslInput, xmlInput, output);
} catch (FileNotFoundException e) {
throw new XmlProcessingException(e);
} finally {
if (xslInput != null) {
try {
xslInput.close();
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
}
}
if (xmlInput != null) {
try {
xmlInput.close();
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
}
}
}
}
public static void transform(InputStream xslInput, InputStream xmlInput, OutputStream output) throws XmlProcessingException {
try {
StreamSource xslSource = new StreamSource(xslInput);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslSource);
StreamSource xmlSource = new StreamSource(xmlInput);
OutputStreamWriter out = new OutputStreamWriter(output, "UTF-8");
StreamResult result = new StreamResult(out);
transformer.transform(xmlSource, result);
} catch (TransformerConfigurationException e) {
throw new XmlProcessingException(e);
} catch (UnsupportedEncodingException e) {
throw new XmlProcessingException(e);
} catch (TransformerException e) {
throw new XmlProcessingException(e);
}
}
private XsltHelper() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy