KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > XSLTransform


1 /*
2  * Copyright 2005 Sun Microsystems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21
22 import javax.xml.transform.Result JavaDoc;
23 import javax.xml.transform.Source JavaDoc;
24 import javax.xml.transform.SourceLocator JavaDoc;
25 import javax.xml.transform.Templates JavaDoc;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerConfigurationException JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32
33 /**
34  * Run an XSL transform (from the Java Almanac)
35  * @author Dave Johnson
36  */

37 public class XSLTransform
38 {
39     public static void main(String JavaDoc[] args)
40     {
41         if (args.length < 3)
42         {
43             System.out.println("USAGE: infile outfile xslfile");
44             System.exit(-1);
45         }
46         String JavaDoc inFilename = args[0];
47         String JavaDoc outFilename = args[1];
48         String JavaDoc xslFilename = args[2];
49         try
50         {
51             // Create transformer factory
52
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
53
54             // Use the factory to create a template containing the xsl file
55
Templates JavaDoc template = factory.newTemplates(new StreamSource JavaDoc(
56                     new FileInputStream JavaDoc(xslFilename)));
57
58             // Use the template to create a transformer
59
Transformer JavaDoc xformer = template.newTransformer();
60
61             // Prepare the input and output files
62
Source JavaDoc source = new StreamSource JavaDoc(new FileInputStream JavaDoc(inFilename));
63             Result JavaDoc result = new StreamResult JavaDoc(new FileOutputStream JavaDoc(outFilename));
64
65             // Apply the xsl file to the source file and write the result to the
66
// output file
67
xformer.transform(source, result);
68         }
69         catch (FileNotFoundException JavaDoc e)
70         {
71             e.printStackTrace();
72         }
73         catch (TransformerConfigurationException JavaDoc e)
74         {
75             // An error occurred in the XSL file
76
e.printStackTrace();
77         }
78         catch (TransformerException JavaDoc e)
79         {
80             // An error occurred while applying the XSL file
81
// Get location of error in input file
82
SourceLocator JavaDoc locator = e.getLocator();
83             int col = locator.getColumnNumber();
84             int line = locator.getLineNumber();
85             String JavaDoc publicId = locator.getPublicId();
86             String JavaDoc systemId = locator.getSystemId();
87             System.out.println("ERROR: line = "+line+" col = "+col);
88         }
89     }
90 }
Popular Tags