1
2
3
4
5
6
7
8
9
10
11 package org.opensync.tools;
12
13 import org.opensync.tools.StylesheetCache;
14
15 import java.io.*;
16 import java.util.*;
17 import org.xml.sax.*;
18 import java.util.jar.*;
19 import java.net.MalformedURLException;
20
21
22 import javax.xml.transform.*;
23 import javax.xml.transform.stream.*;
24
25
26
27
28
29
30 public class ViewTransform {
31
32
33 static protected TransformerFactory factory = TransformerFactory.newInstance();
34
35
36 protected String xalanVer = null;
37 protected String path;
38 protected String servletServerName;
39 protected Hashtable xslInputs = new Hashtable();
40
41
42 static protected ViewTransform instance;
43
44 void initXalanVer() {
45 xalanVer = "1.0";
46 try {
47
48 JarFile jarfile = new JarFile(path+"/WEB-INF/lib/xalan1.jar");
49
50
51 Manifest manifest = jarfile.getManifest();
52
53
54 Map map = manifest.getEntries();
55
56
57 for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
58
59
60 String entryName = (String)it.next();
61
62 if ((entryName != null) && (entryName.equals("org/apache/xalan/xslt/"))) {
63
64
65 java.util.jar.Attributes attrs = (java.util.jar.Attributes)map.get(entryName);
66
67
68 for (Iterator it2=attrs.keySet().iterator(); it2.hasNext(); ) {
69
70 java.util.jar.Attributes.Name attrName = (java.util.jar.Attributes.Name)it2.next();
71
72
73 String attrValue = attrs.getValue(attrName);
74 if ((attrName != null) && (attrName.toString().equals("Implementation-Version"))) xalanVer = attrValue;
75 }
76 }
77 }
78 } catch (IOException e) {
79 }
80 }
81
82 protected ViewTransform(String path) {
83 this.path = path;
84 }
85 public static ViewTransform getInstance(String path){
86 if(instance == null){
87 setInstance(path);
88 }
89 return instance;
90 }
91 public static ViewTransform getInstance(){
92 if(instance == null){
93 throw new IllegalStateException("Use getInstance(String path) first");
94 }
95 return instance;
96 }
97 protected static synchronized void setInstance(String path){
98 if(instance == null){
99 instance = new ViewTransform(path);
100 }
101 }
102
103 synchronized public void buildHtml (String xmlInput, String xslInputFile, Writer out) throws SAXException
104
105 {
106
107
108
109
110
111
112
113 try {
114
115 StreamSource xslSource;
116
117 synchronized(xslInputs){
118 xslSource = (StreamSource) xslInputs.get(xslInputFile);
119 if(xslSource == null) {
120 xslSource = new StreamSource(new File(path + xslInputFile).toURL().toExternalForm());
121 xslInputs.put(xslInputFile,xslSource);
122 }
123 }
124
125 Transformer transformer = StylesheetCache.newTransformer(path + xslInputFile);
126 StreamSource xmlSource = new StreamSource(new StringReader(xmlInput));
127 StreamResult result = new StreamResult(out);
128 transformer.transform(xmlSource, result);
129 }
130 catch (java.net.MalformedURLException mue) {
131 throw new SAXException(mue);
132 }
133 catch (TransformerConfigurationException tce) {
134 throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation()+"\n Inputs:\n"+xmlInput);
135 }
136 catch (TransformerException te) {
137 throw new SAXException(te.getLocalizedMessage());
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 }
158
159
160
161 public void setServletServerName(String _servletServerName) {
162 servletServerName = _servletServerName;
163 }
164
165 public void buildHtmlFromFile (String xmlInputFile, String xslInputFile, PrintWriter out)
166 throws FileNotFoundException, SAXException{
167
168 try {
169 StreamSource xslSource;
170
171 synchronized(xslInputs){
172 xslSource = (StreamSource) xslInputs.get(xslInputFile);
173
174 if(xslSource == null) {
175 if (new File(path + xslInputFile).exists()){
176 xslSource = new StreamSource(new File(path + xslInputFile).toURL().toExternalForm());
177 }else{
178 xslSource = new StreamSource(path + xslInputFile);
179 }
180
181 xslInputs.put(xslInputFile,xslSource);
182 }
183 }
184
185 Transformer transformer = factory.newTransformer(xslSource);
186 StreamSource xmlSource=null;
187 if(new File(path + xmlInputFile).exists()){
188 xmlSource = new StreamSource(new File(path + xmlInputFile).toURL().toExternalForm());
189 }else{
190 xmlSource = new StreamSource(path + xmlInputFile);
191 }
192 StreamResult result = new StreamResult(out);
193 transformer.transform(xmlSource, result);
194
195
196
197
198
199
200
201
202
203 }
204 catch (MalformedURLException mue) {
205 throw new SAXException(mue.getMessage());
206 } catch (TransformerConfigurationException tce) {
207 throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation());
208 }
209 catch (TransformerException te) {
210 throw new SAXException(te.getLocalizedMessage());
211 }
212 }
213
214
215
216
217 public void buildHtml (String xmlInput, String xslInputFile, Writer out, HashMap hm) throws SAXException {
218 try {
219
220 StreamSource xslSource;
221
222 synchronized(xslInputs){
223 xslSource = (StreamSource) xslInputs.get(xslInputFile);
224 if(xslSource == null) {
225 xslSource = new StreamSource(path + xslInputFile);
226 xslInputs.put(xslInputFile,xslSource);
227 }
228 }
229 Transformer transformer = StylesheetCache.newTransformer(path + xslInputFile);
230
231
232 Set keys = hm.keySet();
233 String[] array = (String[])keys.toArray(new String[keys.size()]);
234 for (int i=0;i<keys.size();i++) {
235 String [] values = (String[])hm.get(array[i]);
236 transformer.setParameter(array[i], values[0]);
237 }
238
239 StreamSource xmlSource = new StreamSource(new StringReader(xmlInput));
240 StreamResult result = new StreamResult(out);
241 transformer.transform(xmlSource, result);
242 } catch (MalformedURLException mue) {
243 throw new SAXException(mue.getMessage());
244 } catch (TransformerConfigurationException tce) {
245 throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation());
246 }
247 catch (TransformerException te) {
248 throw new SAXException(te.getLocalizedMessage());
249 }
250 }
251
252
253
254 public void buildHtmlFromFile (String xmlInputFile, String xslInputFile, PrintWriter out, HashMap hm)
255 throws FileNotFoundException, SAXException{
256
257 try {
258 StreamSource xslSource;
259 synchronized(xslInputs){
260 xslSource = (StreamSource) xslInputs.get(xslInputFile);
261 if(xslSource == null) {
262 xslSource = new StreamSource(new File(path + xslInputFile).toURL().toExternalForm());
263 xslInputs.put(xslInputFile,xslSource);
264 }
265 }
266
267 Transformer transformer = factory.newTransformer(xslSource);
268
269
270 Set keys = hm.keySet();
271 String[] array = (String[])keys.toArray(new String[keys.size()]);
272 for (int i=0;i<keys.size();i++) {
273 String [] values = (String[])hm.get(array[i]);
274 transformer.setParameter(array[i], values[0]);
275 }
276
277 StreamSource xmlSource = new StreamSource(new File(path + xmlInputFile).toURL().toExternalForm());
278 StreamResult result = new StreamResult(out);
279 transformer.transform(xmlSource, result);
280
281 } catch (MalformedURLException mue) {
282 throw new SAXException(mue.getMessage());
283 } catch (TransformerConfigurationException tce) {
284 throw new SAXException("Line: "+tce.getLocationAsString()+"; Error: "+tce.getMessageAndLocation());
285 }
286 catch (TransformerException te) {
287 throw new SAXException(te.getLocalizedMessage());
288 }
289 }
290 }