KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TransformThread


1 /*
2  * Copyright 1999-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: TransformThread.java,v 1.2 2004/02/17 19:09:38 minchau Exp $
18  */

19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.transform.Result JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.dom.DOMResult JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.sax.SAXResult JavaDoc;
35 import javax.xml.transform.sax.SAXSource JavaDoc;
36 import javax.xml.transform.stream.StreamResult JavaDoc;
37 import javax.xml.transform.stream.StreamSource JavaDoc;
38
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41
42 /**
43  * What it does: this sample creates multiple threads
44  * and runs them. Each thread will be assigned a particular
45  * stylesheet. Each thread will run multiple transformations on
46  * various xml files using its own transformer.
47  *
48  * Note: the flavors used by the transformations can be
49  * configured below by changing SOURCE_FLAVOR and
50  * RESULT_FLAVOR. XSLTC can also be used by changing
51  * USE_XSLTC.
52  *
53  * Description of files included with the sample:
54  *
55  * foo0.xsl and foo1.xsl: foo0.xsl is the stylesheet used
56  * for transformations by thread #0, foo1.xsl is the stylesheet
57  * used by thread #1.
58  *
59  * foo0.xml and foo1.xml: foo0.xml and foo1.xml are the XML
60  * files used for the first and second transformations done
61  * by each thread.
62  *
63  * Output will go to *.out files in the TransformThread directory.
64  *
65  * @author <a HREF="mailto:richcao@ca.ibm.com">Richard Cao</a>
66  */

67 public class TransformThread implements Runnable JavaDoc
68 {
69   // Flavors
70
public final static int STREAM = 0;
71   public final static int SAX = 1;
72   public final static int DOM = 2;
73   public final static String JavaDoc[] flavorNames =
74     new String JavaDoc[] { "Stream", "SAX", "DOM" };
75
76   // Configurable options
77
private static int SOURCE_FLAVOR = STREAM;
78     // private static int SOURCE_FLAVOR = SAX;
79
// private static int SOURCE_FLAVOR = DOM;
80

81   private static int RESULT_FLAVOR = STREAM;
82     // private static int RESULT_FLAVOR = SAX;
83
// private static int RESULT_FLAVOR = DOM;
84

85   private static boolean USE_XSLTC = false;
86     // private static boolean useXSLTC = true;
87

88
89   // Threads
90
private final static int NUM_THREADS = 2;
91   private static TransformThread INSTANCES[] = null;
92   protected Thread JavaDoc m_thread = null;
93   
94   // Number of transformations per thread
95
private final static int NUM_TRANSFORMATIONS = 2;
96
97   // Files names and extensions
98
private final static String JavaDoc XML_IN_BASE = "foo";
99   private final static String JavaDoc XML_EXT = ".xml";
100   private final static String JavaDoc XSL_IN_BASE = "foo";
101   private final static String JavaDoc XSL_EXT = ".xsl";
102   private final static String JavaDoc FILE_OUT_BASE = "foo_";
103   private final static String JavaDoc FILE_OUT_EXT = ".out";
104
105   // Thread identifier
106
private int m_thrdNum = -1;
107
108   private InputStream JavaDoc[] m_inStream = null;
109
110   private Source JavaDoc[] m_inSource = null;
111   private Result JavaDoc[] m_outResult = null;
112
113   // One Transformer per thread since Transformers
114
// are _NOT_ thread-safe
115
private Transformer JavaDoc m_transformer = null;
116
117   /** Constructs the TransformThread object
118    * @param thrdNum a unique identifier for this object
119    */

120   public TransformThread(int thrdNum)
121   {
122     m_thrdNum = thrdNum;
123
124     m_inStream = new InputStream JavaDoc[NUM_TRANSFORMATIONS];
125     m_inSource = new Source JavaDoc[NUM_TRANSFORMATIONS];
126     m_outResult = new Result JavaDoc[NUM_TRANSFORMATIONS];
127
128     try
129     {
130       initSource();
131       initResult();
132
133       // ensure xslSourceURI is a valid URI
134
final String JavaDoc xslSourceFileName = XSL_IN_BASE + m_thrdNum + XSL_EXT;
135       final String JavaDoc xslSourceURI = (new File JavaDoc(xslSourceFileName)).toURL().toString();
136       StreamSource JavaDoc xslSource = new StreamSource JavaDoc(xslSourceFileName);
137       xslSource.setSystemId(xslSourceURI);
138       
139       // Initialize the tranformer
140
m_transformer =
141         TransformerFactory.newInstance().newTransformer(xslSource);
142       m_thread = new Thread JavaDoc(this);
143     }
144     catch (Throwable JavaDoc e)
145     {
146       e.printStackTrace();
147       System.exit(1);
148     }
149   }
150
151   /** Initialize the results (m_outResult) according
152    * to RESULT_FLAVOR
153    */

154   private void initResult()
155   {
156     try
157     {
158       for (int i = 0; i < NUM_TRANSFORMATIONS; i++)
159       {
160         switch (RESULT_FLAVOR)
161         {
162           case STREAM :
163               OutputStream JavaDoc outStream =
164                 new FileOutputStream JavaDoc(FILE_OUT_BASE + "thread_"
165                   + m_thrdNum + "_transformation_" + i + FILE_OUT_EXT);
166                 
167               m_outResult[i] = new StreamResult JavaDoc(outStream);
168             break;
169
170           case SAX :
171             DefaultHandler JavaDoc defaultHandler = new DefaultHandler JavaDoc();
172             m_outResult[i] = new SAXResult JavaDoc(defaultHandler);
173             break;
174
175           case DOM :
176             m_outResult[i] = new DOMResult JavaDoc();
177             break;
178         }
179       }
180     }
181     catch (Exception JavaDoc e)
182     {
183       e.printStackTrace();
184       System.exit(1);
185     }
186   }
187
188   /** Initialize the sources (m_inSource) according
189    * to SOURCE_FLAVOR
190    */

191   private void initSource()
192   {
193     try
194     {
195       for (int i = 0; i < NUM_TRANSFORMATIONS; i++)
196       {
197         // Ensure we get a valid URI
198
final String JavaDoc sourceXMLURI = (new File JavaDoc(XML_IN_BASE + i + XML_EXT)).toURL().toString();
199         
200         // Open for input
201
m_inStream[i] = new FileInputStream JavaDoc(XML_IN_BASE + i + XML_EXT);
202         
203         switch (SOURCE_FLAVOR)
204         {
205           case STREAM :
206             m_inSource[i] = new StreamSource JavaDoc(m_inStream[i]);
207             break;
208   
209           case SAX :
210             m_inSource[i] = new SAXSource JavaDoc(new InputSource JavaDoc(m_inStream[i]));
211             break;
212   
213           case DOM :
214             try
215             {
216               DocumentBuilderFactory JavaDoc dfactory =
217                 DocumentBuilderFactory.newInstance();
218   
219               // Must always setNamespaceAware when
220
// building xsl stylesheets
221
dfactory.setNamespaceAware(true);
222               m_inSource[i] =
223                 new DOMSource JavaDoc(dfactory.newDocumentBuilder().parse(m_inStream[i]));
224             }
225             catch (Exception JavaDoc e)
226             {
227               e.printStackTrace();
228             }
229             break;
230         }
231   
232         if (m_inSource[i] != null)
233         {
234           // If we don't do this, the transformer
235
// won't know how to resolve relative URLs
236
// in the stylesheet.
237
m_inSource[i].setSystemId(sourceXMLURI);
238         }
239       }
240     }
241     catch (Exception JavaDoc e)
242     {
243       e.printStackTrace();
244       System.exit(1);
245     }
246   }
247
248   /**
249    * @see java.lang.Runnable#run()
250    */

251   public void run()
252   {
253     try
254     {
255       // Perform multiple transformations with the same
256
// transformer
257
for (int i = 0; i < NUM_TRANSFORMATIONS; i++)
258       {
259         m_transformer.transform(m_inSource[i], m_outResult[i]);
260       }
261     }
262     catch (Exception JavaDoc e)
263     {
264       e.printStackTrace();
265       System.exit(1);
266     }
267   }
268
269   /** Creates thread instances
270    */

271   private static void initThreads()
272   {
273     INSTANCES = new TransformThread[NUM_THREADS];
274
275     for (int count = 0; count < NUM_THREADS; count++)
276     {
277       INSTANCES[count] = new TransformThread(count);
278     }
279   }
280   
281   /** Sets the appropriate system properties if XSLTC is
282    * to be used (according to USE_XSLTC)
283    */

284   private static void initSystemProperties()
285   {
286     if (USE_XSLTC)
287     {
288       // Set the TransformerFactory system property if XSLTC is required
289
// Note: To make this sample more flexible, load properties from a properties file.
290
// The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl"
291
String JavaDoc key = "javax.xml.transform.TransformerFactory";
292       String JavaDoc value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
293       Properties JavaDoc props = System.getProperties();
294       props.put(key, value);
295       System.setProperties(props);
296     }
297   }
298
299   /**
300    * Usage:
301    * java TransformThread
302    */

303   public static void main(String JavaDoc argv[])
304   {
305     try
306     {
307       initSystemProperties();
308       initThreads();
309
310       for (int count = 0; count < NUM_THREADS; count++)
311       {
312         INSTANCES[count].m_thread.start();
313       }
314     }
315     catch (Throwable JavaDoc e)
316     {
317       e.printStackTrace();
318       System.exit(1);
319     }
320   }
321 }
322
Popular Tags