KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > template > XslTransform


1 /*
2  * $Id: XslTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util.template;
25
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.io.StringReader JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32 import java.io.InputStream JavaDoc;
33
34 import org.ofbiz.base.util.UtilValidate;
35 import org.ofbiz.base.util.UtilXml;
36 import org.ofbiz.base.util.URLConnector;
37 import org.ofbiz.base.util.cache.UtilCache;
38 import org.ofbiz.base.location.FlexibleLocation;
39 import javax.xml.transform.Transformer JavaDoc;
40 import javax.xml.transform.TransformerFactory JavaDoc;
41 import javax.xml.transform.Templates JavaDoc;
42 import javax.xml.transform.Source JavaDoc;
43 import javax.xml.transform.dom.DOMSource JavaDoc;
44 import javax.xml.transform.dom.DOMResult JavaDoc;
45 import javax.xml.transform.TransformerConfigurationException JavaDoc;
46 import javax.xml.transform.TransformerException JavaDoc;
47 import org.w3c.dom.Document JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49 import org.ofbiz.base.util.GeneralException;
50 import java.io.IOException JavaDoc;
51
52 import javax.xml.transform.stream.StreamSource JavaDoc;
53
54 /**
55  * XslTransform
56  *
57  * This utility takes an input document and a XSL stylesheet and performs the
58  * transform, returning the output document.
59  * The input for both the input document and stylesheet can be in one of three forms
60  * - a URL to the doc, the doc in string form and the doc in DOM Document form.
61  * It keeps its own cache for storing the compiled transforms.
62  *
63  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
64  * @version $Rev: 5462 $
65  * @since 3.2
66  */

67 public final class XslTransform {
68
69     public static final String JavaDoc module = XslTransform.class.getName();
70     public static UtilCache xslTemplatesCache = new UtilCache("XsltTemplates", 0, 0);
71
72     public static Document JavaDoc transform(Map JavaDoc context, Map JavaDoc params)
73         throws GeneralException, IOException JavaDoc, TransformerConfigurationException JavaDoc, TransformerException JavaDoc {
74         Document JavaDoc outputDocument = null;
75         TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
76         Templates JavaDoc translet = null;
77         String JavaDoc templateName = (String JavaDoc)context.get("templateName");
78         if (UtilValidate.isNotEmpty(templateName)) {
79             translet = (Templates JavaDoc) xslTemplatesCache.get(templateName);
80         }
81
82         if (translet == null ) {
83             String JavaDoc templateUrl = (String JavaDoc)context.get("templateUrl");
84             String JavaDoc templateString = (String JavaDoc)context.get("templateString");
85             Document JavaDoc templateDocument = (Document JavaDoc)context.get("templateDocument");
86             Source JavaDoc templateSource = getSource(templateDocument, templateUrl, templateString);
87             translet = tFactory.newTemplates(templateSource);
88             if (UtilValidate.isNotEmpty(templateName)) {
89                     xslTemplatesCache.put(templateName, translet);
90             }
91         }
92         if (translet != null ) {
93             Transformer JavaDoc transformer = translet.newTransformer();
94             if (params != null) {
95                Set JavaDoc entrySet = params.entrySet();
96                Iterator JavaDoc iter = entrySet.iterator();
97                while (iter.hasNext()) {
98                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
99                     String JavaDoc key = (String JavaDoc)entry.getKey();
100                     Object JavaDoc val = entry.getValue();
101                     transformer.setParameter(key, val);
102                }
103             }
104             
105             DOMResult JavaDoc outputResult = new DOMResult JavaDoc(UtilXml.makeEmptyXmlDocument());
106             
107             String JavaDoc inputUrl = (String JavaDoc)context.get("inputUrl");
108             String JavaDoc inputString = (String JavaDoc)context.get("inputString");
109             Document JavaDoc inputDocument = (Document JavaDoc)context.get("inputDocument");
110             Source JavaDoc inputSource = getSource(inputDocument, inputUrl, inputString);
111             
112             transformer.transform(inputSource, outputResult);
113             Node JavaDoc nd = outputResult.getNode();
114             outputDocument = (Document JavaDoc)nd;
115         }
116
117         return outputDocument;
118     }
119     
120     private static Source JavaDoc getSource(Document JavaDoc inputDocument, String JavaDoc inputUrl, String JavaDoc inputString) throws GeneralException, IOException JavaDoc {
121         Source JavaDoc source = null;
122         if (inputDocument != null) {
123             source = new DOMSource JavaDoc(inputDocument);
124         } else if (UtilValidate.isNotEmpty(inputString)) {
125             source = new StreamSource JavaDoc(new StringReader JavaDoc(inputString));
126         } else if (UtilValidate.isNotEmpty(inputUrl)) {
127             URL JavaDoc url = FlexibleLocation.resolveLocation(inputUrl);
128             URLConnection JavaDoc conn = URLConnector.openConnection(url);
129             InputStream JavaDoc in = conn.getInputStream();
130             source = new StreamSource JavaDoc(in);
131         }
132         return source;
133     }
134 }
135
Popular Tags