KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JAXPTransletOneTransformation


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
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 /*
17  * $Id: JAXPTransletOneTransformation.java,v 1.11 2004/02/17 19:13:22 minchau Exp $
18  */

19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import java.util.Properties JavaDoc;
24
25 import javax.xml.parsers.ParserConfigurationException 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 import org.xml.sax.SAXException JavaDoc;
34
35
36 /**
37  * Using the TrAX/JAXP 1.1 interface to compile and run a translet. The translet
38  * extends the abstract Transformer class and is used to perform a single
39  * transformation. If you want to use the translet to perform multiple
40  * transformations, see JAXPTransletMultipleTransformations.java.
41  *
42  * @author Donald Leslie
43  */

44 public class JAXPTransletOneTransformation
45 {
46   public static void main(String JavaDoc argv[])
47           throws TransformerException JavaDoc, TransformerConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc,
48                  ParserConfigurationException JavaDoc, FileNotFoundException JavaDoc
49   {
50     // Set the TransformerFactory system property to generate and use a translet.
51
// Note: To make this sample more flexible, load properties from a properties file.
52
// The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl"
53
String JavaDoc key = "javax.xml.transform.TransformerFactory";
54     String JavaDoc value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
55     Properties JavaDoc props = System.getProperties();
56     props.put(key, value);
57     System.setProperties(props);
58
59     String JavaDoc xslInURI = "todo.xsl";
60     String JavaDoc xmlInURI = "todo.xml";
61     String JavaDoc htmlOutURI = "todo.html";
62     try
63     {
64       // Instantiate the TransformerFactory, and use it along with a SteamSource
65
// XSL stylesheet to create a Transformer.
66
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
67       Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc(xslInURI));
68       // Perform the transformation from a StreamSource to a StreamResult;
69
transformer.transform(new StreamSource JavaDoc(xmlInURI),
70                             new StreamResult JavaDoc(new FileOutputStream JavaDoc(htmlOutURI)));
71       System.out.println("Produced todo.html");
72     }
73     catch (Exception JavaDoc e)
74     {
75      System.out.println(e.toString());
76      e.printStackTrace();
77     }
78   }
79 }
80
Popular Tags