KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JAXPTransletMultipleTransformations


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: JAXPTransletMultipleTransformations.java,v 1.10 2004/02/17 19:13:22 minchau Exp $
18  */

19 import java.io.FileNotFoundException JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21
22 import java.util.Properties JavaDoc;
23
24 import javax.xml.transform.Templates JavaDoc;
25 import javax.xml.transform.Transformer JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27 import javax.xml.transform.TransformerFactory JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import javax.xml.transform.stream.StreamSource JavaDoc;
30
31
32 /**
33  * Using the TrAX/JAXP 1.1 interface to compile a translet and use it
34  * to perform multiple transformations. The translet implements
35  * the Templates interface. If you want to use the translet to perform a
36  * single transformation, see JAXPTransletOneTransformation.java.
37  *
38  *
39  * @author Donald Leslie
40  */

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