KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > xml > XMLTransformer


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.xml;
21
22 import org.apache.log4j.LogManager;
23 import org.apache.log4j.Logger;
24 import java.io.*;
25 import javax.xml.transform.Source JavaDoc;
26 import javax.xml.transform.Transformer JavaDoc;
27 import javax.xml.transform.TransformerException JavaDoc;
28 import javax.xml.transform.TransformerFactory JavaDoc;
29 import javax.xml.transform.stream.StreamResult JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34
35 /**
36  * This class is responsible for transformation of XML with XSL.
37  * @author Uddhab Pant <br>
38  * @version $Revision: 1.6 $ $Date: 2006/04/12 00:39:12 $ <br>
39  *
40  * TODO: not sure if we need this class. Possible to use the <x: taglib from jakarta?
41  * Save this investigation/refactoring for the QA period.
42  */

43 public class XMLTransformer {
44     // log
45
private static Logger logger = LogManager.getLogger(XMLTransformer.class);
46
47     /**
48      *
49      * @param xslString String
50      * @param xmlString String
51      * @return String Result of transformation
52      * @throws IOException
53      * @throws TransformerException
54      */

55     public static String JavaDoc transform(String JavaDoc xslString, String JavaDoc xmlString)
56         throws IOException, TransformerException JavaDoc {
57         Source JavaDoc xslt = new StreamSource JavaDoc(new StringReader(xslString));
58         Source JavaDoc xml = new StreamSource JavaDoc(new StringReader(xmlString));
59
60         return transform(xslt, xml, null);
61     }
62
63     /**
64      *
65      * @param xslInputStream InputStream
66      * @param xmlInputStream InputStream
67      * @return String Result of transformation
68      * @throws IOException
69      * @throws TransformerException
70      */

71     public static String JavaDoc transform(InputStream xslInputStream,
72         InputStream xmlInputStream) throws IOException, TransformerException JavaDoc {
73         Source JavaDoc xslt = new StreamSource JavaDoc(new InputStreamReader(xslInputStream));
74         Source JavaDoc xml = new StreamSource JavaDoc(new InputStreamReader(xmlInputStream));
75
76         return transform(xslt, xml, null);
77     }
78
79     /**
80      *
81      * @param xslString String
82      * @param xmlString String
83      * @param xslParam Map
84      * @return String
85      * @throws IOException
86      * @throws TransformerException
87      */

88     public static String JavaDoc transform(String JavaDoc xslString, String JavaDoc xmlString, Map JavaDoc xslParam)
89         throws IOException, TransformerException JavaDoc {
90         Source JavaDoc xslt = new StreamSource JavaDoc(new StringReader(xslString));
91         Source JavaDoc xml = new StreamSource JavaDoc(new StringReader(xmlString));
92
93         return transform(xslt, xml, xslParam);
94     }
95
96     /**
97      *
98      * @param xslInputStream InputStream
99      * @param xmlInputStream InputStream
100      * @param xslParam Map
101      * @return String
102      * @throws IOException
103      * @throws TransformerException
104      */

105     public static String JavaDoc transform(InputStream xslInputStream,
106         InputStream xmlInputStream, Map JavaDoc xslParam) throws IOException, TransformerException JavaDoc {
107         Source JavaDoc xslt = new StreamSource JavaDoc(new InputStreamReader(xslInputStream));
108         Source JavaDoc xml = new StreamSource JavaDoc(new InputStreamReader(xmlInputStream));
109
110         return transform(xslt, xml, xslParam);
111     }
112
113     /**
114      *
115      * @param xslt Source
116      * @param xml Source
117      * @return String
118      * @throws TransformerException
119      * @throws IOException
120      */

121     public static String JavaDoc transform(Source JavaDoc xslt, Source JavaDoc xml) throws
122             TransformerException JavaDoc, IOException {
123         return transform(xslt, xml, null);
124     }
125
126
127     /**
128      *
129      * @param xslt Source
130      * @param xml Source
131      * @param xslParam Map parameters to xsl
132      * @return String result of transformation
133      * @throws IOException
134      * @throws TransformerException
135      */

136     public static String JavaDoc transform(Source JavaDoc xslt, Source JavaDoc xml, Map JavaDoc xslParam)
137         throws IOException, TransformerException JavaDoc {
138         String JavaDoc result = "";
139         long start = System.currentTimeMillis();
140
141         TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
142
143         Transformer JavaDoc transformer = tFactory.newTransformer(xslt);
144
145         if(xslParam != null){
146             Iterator JavaDoc params = xslParam.entrySet().iterator();
147             while(params.hasNext()){
148                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) params.next();
149                 transformer.setParameter((String JavaDoc)entry.getKey(), entry.getValue());
150             }
151         }
152
153
154         StringWriter resultWriter = new StringWriter();
155
156         transformer.transform(xml, new StreamResult JavaDoc(resultWriter));
157
158         resultWriter.flush();
159
160         result = resultWriter.getBuffer().toString();
161
162         long elapsed = System.currentTimeMillis() - start;
163         logger.info("Elapsed Time " + elapsed + " ms");
164
165         return result;
166     }
167 }
168
Popular Tags