KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > utils > TransformUtils


1 /* *****************************************************************************
2  * TransformUtils.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.utils;
11 import java.io.*;
12 import java.util.*;
13 import javax.xml.transform.*;
14
15 public abstract class TransformUtils {
16     /** {Pathname -> Templates} */
17     static private Map sTemplatesMap = new HashMap();
18     /** {Pathname -> lastModified} */
19     static private Map sTemplatesLastModified = new HashMap();
20     
21     static public Templates getTemplates(String JavaDoc styleSheetPathname)
22         throws IOException
23     {
24         synchronized (sTemplatesMap) {
25             Templates templates = (Templates) sTemplatesMap.get(styleSheetPathname);
26             Long JavaDoc lastModified = new Long JavaDoc(new File(styleSheetPathname).lastModified());
27             if (templates != null &&
28                 !sTemplatesLastModified.get(styleSheetPathname).equals(lastModified))
29                 templates = null;
30             if (templates == null) {
31                 CollectingErrorListener errorListener =
32                     new CollectingErrorListener();
33                 // name the class instead of using
34
// TransformerFactory.newInstance(), to insure that we
35
// get saxon and thereby work around bug 3924
36
TransformerFactory factory =
37                     new com.icl.saxon.TransformerFactoryImpl();
38                 factory.setErrorListener(errorListener);
39                 java.io.InputStream JavaDoc xslInput =
40                     new java.net.URL JavaDoc("file", "", styleSheetPathname).openStream();
41                 try {
42                     Source xslSource =
43                         new javax.xml.transform.stream.StreamSource JavaDoc(xslInput);
44                     templates = factory.newTemplates(xslSource);
45                 } catch (TransformerConfigurationException e) {
46                     if (errorListener.isEmpty())
47                         throw new ChainedException(e);
48                     else
49                         throw new ChainedException(errorListener.getMessage(), e);
50                 } finally {
51                     xslInput.close();
52                 }
53                 sTemplatesMap.put(styleSheetPathname, templates);
54                 sTemplatesLastModified.put(styleSheetPathname, lastModified);
55             }
56             return templates;
57         }
58     }
59
60     public static void applyTransform(String JavaDoc styleSheetPathname,
61                                       String JavaDoc xmlString,
62                                       OutputStream out)
63         throws IOException
64     {
65         applyTransform(styleSheetPathname, new Properties(), xmlString, out);
66     }
67     
68     // http://xml.apache.org/xalan-j/usagepatterns.html#multithreading
69
// describes this usage pattern.
70
static public void applyTransform(String JavaDoc styleSheetPathname,
71                                       Properties properties,
72                                       String JavaDoc xmlString,
73                                       OutputStream out)
74         throws IOException
75     {
76         PrintWriter writer = new PrintWriter(out);
77         CollectingErrorListener errorListener =
78             new CollectingErrorListener();
79         try {
80             Templates template = getTemplates(styleSheetPathname);
81             Transformer transformer = template.newTransformer();
82             transformer.setErrorListener(errorListener);
83             Source xmlSource =
84                 new javax.xml.transform.stream.StreamSource JavaDoc(
85                     new StringReader(xmlString));
86             for (Iterator iter = properties.keySet().iterator();
87                  iter.hasNext(); ) {
88                 String JavaDoc key = (String JavaDoc) iter.next();
89                 String JavaDoc value = properties.getProperty(key);
90                 transformer.setParameter(key, value);
91             }
92             // Perform the transformation, sending the output to the response.
93
transformer.transform(xmlSource,
94                                   new javax.xml.transform.stream.StreamResult JavaDoc(writer));
95             writer.close();
96         } catch (TransformerConfigurationException e) {
97             if (errorListener.isEmpty())
98                 throw new ChainedException(e);
99             else
100                 throw new ChainedException(errorListener.getMessage(), e);
101         } catch (TransformerException e) {
102             if (errorListener.isEmpty())
103                 throw new ChainedException(e);
104             else
105                 throw new ChainedException(errorListener.getMessage(), e);
106         }
107     }
108 }
109
110 class CollectingErrorListener implements javax.xml.transform.ErrorListener JavaDoc {
111     protected StringBuffer JavaDoc messageBuffer = new StringBuffer JavaDoc();
112     protected String JavaDoc separator = "";
113     protected int messageCount = 0;
114     
115     private void appendErrorString(TransformerException exception) {
116         messageBuffer.append(separator);
117         separator = "\n";
118         messageBuffer.append(exception.getMessageAndLocation());
119         messageCount++;
120     }
121
122     public void error(TransformerException exception) {
123         appendErrorString(exception);
124     }
125     
126     public void warning(TransformerException exception) {
127         appendErrorString(exception);
128     }
129     
130     public void fatalError(TransformerException exception) {
131         appendErrorString(exception);
132     }
133     
134     boolean isEmpty() {
135         return messageCount == 0;
136     }
137
138     String JavaDoc getMessage() {
139         return messageBuffer.toString();
140     }
141 }
142
Popular Tags