01: /*
02: * Copyright 2005 Sun Microsystems, Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.roller.util;
17:
18: import java.io.FileInputStream;
19: import java.io.FileNotFoundException;
20: import java.io.FileOutputStream;
21:
22: import javax.xml.transform.Result;
23: import javax.xml.transform.Source;
24: import javax.xml.transform.SourceLocator;
25: import javax.xml.transform.Templates;
26: import javax.xml.transform.Transformer;
27: import javax.xml.transform.TransformerConfigurationException;
28: import javax.xml.transform.TransformerException;
29: import javax.xml.transform.TransformerFactory;
30: import javax.xml.transform.stream.StreamResult;
31: import javax.xml.transform.stream.StreamSource;
32:
33: /**
34: * Run an XSL transform (from the Java Almanac)
35: * @author Dave Johnson
36: */
37: public class XSLTransform {
38: public static void main(String[] args) {
39: if (args.length < 3) {
40: System.out.println("USAGE: infile outfile xslfile");
41: System.exit(-1);
42: }
43: String inFilename = args[0];
44: String outFilename = args[1];
45: String xslFilename = args[2];
46: try {
47: // Create transformer factory
48: TransformerFactory factory = TransformerFactory
49: .newInstance();
50:
51: // Use the factory to create a template containing the xsl file
52: Templates template = factory.newTemplates(new StreamSource(
53: new FileInputStream(xslFilename)));
54:
55: // Use the template to create a transformer
56: Transformer xformer = template.newTransformer();
57:
58: // Prepare the input and output files
59: Source source = new StreamSource(new FileInputStream(
60: inFilename));
61: Result result = new StreamResult(new FileOutputStream(
62: outFilename));
63:
64: // Apply the xsl file to the source file and write the result to the
65: // output file
66: xformer.transform(source, result);
67: } catch (FileNotFoundException e) {
68: e.printStackTrace();
69: } catch (TransformerConfigurationException e) {
70: // An error occurred in the XSL file
71: e.printStackTrace();
72: } catch (TransformerException e) {
73: // An error occurred while applying the XSL file
74: // Get location of error in input file
75: SourceLocator locator = e.getLocator();
76: int col = locator.getColumnNumber();
77: int line = locator.getLineNumber();
78: String publicId = locator.getPublicId();
79: String systemId = locator.getSystemId();
80: System.out.println("ERROR: line = " + line + " col = "
81: + col);
82: }
83: }
84: }
|