KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > deployment > convertor > XslTransformer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.deployment.convertor;
23
24 import java.net.URL JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.Transformer JavaDoc;
33 import javax.xml.transform.Templates JavaDoc;
34 import javax.xml.transform.TransformerException JavaDoc;
35
36 import javax.xml.transform.stream.StreamSource JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38
39 /**
40  * XslTransformer is a utility class for XSL transformations.
41  *
42  * @author <a HREF="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
43  */

44 public class XslTransformer
45 {
46    // Attributes --------------------------------------------------------
47
/**
48      The system property that determines which Factory implementation
49      to create is named "javax.xml.transform.TransformerFactory".
50      If the property is not defined, a platform default is used.
51      An implementation of the TransformerFactory class is NOT guaranteed
52      to be thread safe.
53    */

54    private static TransformerFactory JavaDoc transformerFactory =
55       TransformerFactory.newInstance();
56
57    // Public static methods ---------------------------------------------
58
/**
59      Applies transformation.
60      Pre-compiled stylesheet should be used in a thread-safe manner grabbing
61      a new transformer before completing the transformation.
62     */

63    public static synchronized void applyTransformation(InputStream JavaDoc srcIs,
64                                                        OutputStream JavaDoc destOs,
65                                                        InputStream JavaDoc templateIs,
66                                                        Properties JavaDoc outputProps)
67    throws TransformerException JavaDoc, IOException JavaDoc
68    {
69       StreamSource JavaDoc source = new StreamSource JavaDoc(srcIs);
70       StreamResult JavaDoc result = new StreamResult JavaDoc(destOs);
71       StreamSource JavaDoc template = new StreamSource JavaDoc(templateIs);
72
73       /*
74         Pre-compile the stylesheet. This Templates object may be used
75         concurrently across multiple threads. Creating a Templates object
76         allows the TransformerFactory to do detailed performance optimization
77         of transformation instructions, without penalizing runtime
78         transformation.
79       */

80       Templates JavaDoc templates = transformerFactory.newTemplates( template );
81
82       Transformer JavaDoc transformer = templates.newTransformer();
83       if( outputProps != null )
84       {
85          transformer.setOutputProperties( outputProps );
86       }
87
88       transformer.transform( source, result );
89    }
90
91
92    /**
93     * Applies template <code>templateIs</code> to xml source
94     * <code>srcIs</code> with output properties <code>outputProps</code>
95     * and parameters <code>xslParams</code>.
96     * The resulting xml is written to <code>destOs</code>
97     */

98    public static synchronized void applyTransformation(InputStream JavaDoc srcIs,
99                                                        OutputStream JavaDoc destOs,
100                                                        InputStream JavaDoc templateIs,
101                                                        Properties JavaDoc outputProps,
102                                                        Properties JavaDoc xslParams)
103    throws TransformerException JavaDoc, IOException JavaDoc
104    {
105       StreamSource JavaDoc source = new StreamSource JavaDoc( srcIs );
106       StreamResult JavaDoc result = new StreamResult JavaDoc( destOs );
107       StreamSource JavaDoc template = new StreamSource JavaDoc( templateIs );
108
109       Templates JavaDoc templates = transformerFactory.newTemplates( template );
110
111       // set output properties
112
Transformer JavaDoc transformer = templates.newTransformer();
113       if(outputProps != null)
114       {
115          transformer.setOutputProperties(outputProps);
116       }
117
118       // set xsl parameters
119
if(xslParams != null)
120       {
121          // note, xslParams.keys() will not work properly,
122
// because it will not return the keys for default properties.
123
Enumeration JavaDoc keys = xslParams.propertyNames();
124          while( keys.hasMoreElements() )
125          {
126             String JavaDoc key = (String JavaDoc)keys.nextElement();
127             transformer.setParameter(key, xslParams.getProperty(key));
128          }
129       }
130
131       transformer.transform( source, result );
132    }
133 }
134
Popular Tags