1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.fhw.cabaweb.export.transform;
19
20 import java.io.BufferedWriter;
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.io.Writer;
25 import java.net.URL;
26 import java.util.Properties;
27
28 import javax.xml.transform.Result;
29 import javax.xml.transform.Source;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.sax.SAXResult;
33 import javax.xml.transform.stream.StreamResult;
34 import javax.xml.transform.stream.StreamSource;
35
36 import org.apache.avalon.framework.logger.NullLogger;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.fop.apps.Driver;
40 import org.apache.fop.messaging.MessageHandler;
41 import org.dom4j.Document;
42 import org.dom4j.io.OutputFormat;
43 import org.dom4j.io.XMLWriter;
44
45 import org.jfor.jfor.converter.Converter;
46
47 import org.fhw.cabaweb.export.databojects.CachableByteArrayOutputStream;
48 import org.fhw.cabaweb.tools.StringUtilities;
49
50 /***
51 * Die Methoden der Klasse transformieren das übergebene XML Dokument in verschiedene Formate.
52 *
53 * @author <a href="mailto:thomas.vogt@tvc-software.com">Thomas Vogt</a>
54 * @version Version 1.0 15.09.2004
55 */
56 public class ReportTransform
57 {
58 /*** Commons Logging Instanz */
59 private static Log log = LogFactory.getLog("org.fhw.cabaweb.export.transform.ReportTransform");
60
61 /***
62 *
63 */
64 public ReportTransform()
65 {
66 super();
67 }
68
69 public final static CachableByteArrayOutputStream writeXML(Document document) throws IOException
70 {
71 CachableByteArrayOutputStream baos = new CachableByteArrayOutputStream();
72
73 if (log.isDebugEnabled())
74 {
75 log.debug(" WRITE XML");
76 }
77
78 OutputFormat format = OutputFormat.createPrettyPrint();
79 format.setEncoding("ISO-8859-1");
80 XMLWriter writer = new XMLWriter(baos, format);
81
82 writer.setEscapeText(false);
83 writer.write(document);
84 writer.close();
85
86 return baos;
87 }
88
89 public final static CachableByteArrayOutputStream writePDF(Document document, String xslPDF) throws IOException
90 {
91 CachableByteArrayOutputStream baos = new CachableByteArrayOutputStream();
92
93 if (log.isDebugEnabled())
94 {
95 log.debug(" WRITE XML");
96 }
97
98 OutputFormat format = OutputFormat.createPrettyPrint();
99 format.setEncoding("ISO-8859-1");
100 XMLWriter writer = new XMLWriter(baos, format);
101
102 writer.setEscapeText(false);
103 writer.write(document);
104 writer.close();
105
106 return ReportTransform.writePDF(baos, xslPDF);
107 }
108
109 public final static CachableByteArrayOutputStream writePDF(CachableByteArrayOutputStream xmlStream, String xslPDF) throws IOException
110 {
111 Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
112
113 String proxyHost = properties.getProperty("ProxyHost");
114 String proxyPort = properties.getProperty("ProxyPort");
115
116 if(proxyHost != null && proxyHost != "" && proxyPort != null && proxyPort != "" )
117 {
118 Properties systemSettings = System.getProperties();
119 systemSettings.put("proxySet", "true");
120 systemSettings.put("proxyHost", proxyHost);
121 systemSettings.put("proxyPort", proxyPort);
122 System.setProperties(systemSettings);
123 }
124
125 if (log.isDebugEnabled())
126 {
127 log.debug(" STREAM: " + xslPDF);
128 }
129
130 NullLogger nulllogger = new NullLogger();
131 MessageHandler.setScreenLogger(nulllogger);
132 Driver driver = new Driver();
133 driver.setLogger(nulllogger);
134 driver.setRenderer(Driver.RENDER_PDF);
135
136
137 CachableByteArrayOutputStream out = new CachableByteArrayOutputStream();
138
139
140 try
141 {
142 driver.setOutputStream(out);
143
144 if (log.isDebugEnabled())
145 {
146 log.debug(" TRANSFORMER STREAM OPEN");
147 }
148
149
150 TransformerFactory factory = TransformerFactory.newInstance();
151 Transformer transformer = factory.newTransformer(new StreamSource(new URL(xslPDF).openStream()));
152
153 if (log.isDebugEnabled())
154 {
155 log.debug(" STREAM TRANSFORM TO INPUT STREAM");
156 }
157
158
159 ByteArrayInputStream bais = new ByteArrayInputStream(xmlStream.toByteArray());
160 Source src = new StreamSource(bais);
161
162 if (log.isDebugEnabled())
163 {
164 log.debug(" RESULT CREATION");
165 }
166
167
168 Result res = new SAXResult(driver.getContentHandler());
169
170 if (log.isDebugEnabled())
171 {
172 log.debug(" TRANSFORM 2 PDF");
173 }
174
175
176 transformer.transform(src, res);
177 }
178 catch (Exception e)
179 {
180 log.error(e.getMessage(), e);
181 }
182 finally
183 {
184 out.close();
185 }
186
187 return out;
188 }
189
190 public final static CachableByteArrayOutputStream writeRTF(Document document, String xslRTF) throws IOException
191 {
192 CachableByteArrayOutputStream baos = new CachableByteArrayOutputStream();
193
194 if (log.isDebugEnabled())
195 {
196 log.debug(" WRITE XML");
197 }
198
199 OutputFormat format = OutputFormat.createPrettyPrint();
200 format.setEncoding("ISO-8859-1");
201 XMLWriter writer = new XMLWriter(baos, format);
202
203 writer.setEscapeText(false);
204 writer.write(document);
205 writer.close();
206
207 return ReportTransform.writeRTF(baos, xslRTF);
208 }
209
210 public final static CachableByteArrayOutputStream writeRTF(CachableByteArrayOutputStream xmlStream, String xslRTF) throws IOException
211 {
212 Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
213
214 String proxyHost = properties.getProperty("ProxyHost");
215 String proxyPort = properties.getProperty("ProxyPort");
216
217 if(proxyHost != null && proxyHost != "" && proxyPort != null && proxyPort != "" )
218 {
219 Properties systemSettings = System.getProperties();
220 systemSettings.put("proxySet", "true");
221 systemSettings.put("proxyHost", proxyHost);
222 systemSettings.put("proxyPort", proxyPort);
223 System.setProperties(systemSettings);
224 }
225
226 if (log.isDebugEnabled())
227 {
228 log.debug(" STREAM: " + xslRTF);
229 }
230
231 NullLogger nulllogger = new NullLogger();
232 Driver driver = new Driver();
233 driver.setLogger(nulllogger);
234 driver.setRenderer(Driver.RENDER_PDF);
235 MessageHandler.setScreenLogger(nulllogger);
236
237
238 CachableByteArrayOutputStream out = new CachableByteArrayOutputStream();
239
240 Writer rtfWriter = null;
241 Converter handler = null;
242
243
244 try
245 {
246 rtfWriter = new BufferedWriter(new OutputStreamWriter(out));
247
248 if (log.isDebugEnabled())
249 {
250 log.debug(" TRANSFORMER STREAM OPEN");
251 }
252
253
254 TransformerFactory factory = TransformerFactory.newInstance();
255 Transformer transformer = factory.newTransformer(new StreamSource(new URL(xslRTF).openStream()));
256
257 if (log.isDebugEnabled())
258 {
259 log.debug(" STREAM TRANSFORM TO INPUT STREAM");
260 }
261
262
263 ByteArrayInputStream bais = new ByteArrayInputStream(xmlStream.toByteArray());
264 Source src = new StreamSource(bais);
265
266 if (log.isDebugEnabled())
267 {
268 log.debug(" RESULT CREATION");
269 }
270
271 handler = new Converter(rtfWriter, Converter.createConverterOption(System.out));
272
273
274 Result res = new SAXResult(handler);
275
276 if (log.isDebugEnabled())
277 {
278 log.debug(" TRANSFORM 2 RTF");
279 }
280
281
282 transformer.transform(src, res);
283 }
284 catch (Exception e)
285 {
286 log.error(e.getMessage(), e);
287 }
288 finally
289 {
290 out.close();
291 }
292
293 return out;
294 }
295
296 public final static CachableByteArrayOutputStream writeHTML(Document document, String xslHTML) throws IOException
297 {
298 CachableByteArrayOutputStream baos = new CachableByteArrayOutputStream();
299
300 if (log.isDebugEnabled())
301 {
302 log.debug(" WRITE XML");
303 }
304
305 OutputFormat format = OutputFormat.createPrettyPrint();
306 format.setEncoding("ISO-8859-1");
307 XMLWriter writer = new XMLWriter(baos, format);
308
309 writer.setEscapeText(false);
310 writer.write(document);
311 writer.close();
312
313 return ReportTransform.writeHTML(baos, xslHTML);
314 }
315
316 public final static CachableByteArrayOutputStream writeHTML(CachableByteArrayOutputStream xmlStream, String xslHTML) throws IOException
317 {
318 Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
319
320 String proxyHost = properties.getProperty("ProxyHost");
321 String proxyPort = properties.getProperty("ProxyPort");
322
323 if(proxyHost != null && proxyHost != "" && proxyPort != null && proxyPort != "" )
324 {
325 Properties systemSettings = System.getProperties();
326 systemSettings.put("proxySet", "true");
327 systemSettings.put("proxyHost", proxyHost);
328 systemSettings.put("proxyPort", proxyPort);
329 System.setProperties(systemSettings);
330 }
331
332 if (log.isDebugEnabled())
333 {
334 log.debug(" STREAM: " + xslHTML);
335 }
336
337
338 CachableByteArrayOutputStream out = new CachableByteArrayOutputStream();
339
340
341 try
342 {
343 if (log.isDebugEnabled())
344 {
345 log.debug(" TRANSFORMER STREAM OPEN");
346 }
347
348
349 TransformerFactory factory = TransformerFactory.newInstance();
350 Transformer transformer = factory.newTransformer(new StreamSource(new URL(xslHTML).openStream()));
351
352 if (log.isDebugEnabled())
353 {
354 log.debug(" STREAM TRANSFORM TO INPUT STREAM");
355 }
356
357
358 ByteArrayInputStream bais = new ByteArrayInputStream(xmlStream.toByteArray());
359 Source src = new StreamSource(bais);
360
361 if (log.isDebugEnabled())
362 {
363 log.debug(" RESULT CREATION");
364 }
365
366
367 StreamResult res = new StreamResult(out);
368
369 if (log.isDebugEnabled())
370 {
371 log.debug(" TRANSFORM 2 HTML");
372 }
373
374
375 transformer.transform(src, res);
376 }
377 catch (Exception e)
378 {
379 log.error(e.getMessage(), e);
380 }
381 finally
382 {
383 out.close();
384 }
385
386 return out;
387 }
388
389 public final static CachableByteArrayOutputStream writeXSLFO(CachableByteArrayOutputStream xmlStream, String xslPDF) throws IOException
390 {
391 Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
392
393 String proxyHost = properties.getProperty("ProxyHost");
394 String proxyPort = properties.getProperty("ProxyPort");
395
396 if(proxyHost != null && proxyHost != "" && proxyPort != null && proxyPort != "" )
397 {
398 Properties systemSettings = System.getProperties();
399 systemSettings.put("proxySet", "true");
400 systemSettings.put("proxyHost", proxyHost);
401 systemSettings.put("proxyPort", proxyPort);
402 System.setProperties(systemSettings);
403 }
404
405 if (log.isDebugEnabled())
406 {
407 log.debug(" STREAM: " + xslPDF);
408 }
409
410
411 CachableByteArrayOutputStream out = new CachableByteArrayOutputStream();
412
413
414 try
415 {
416 if (log.isDebugEnabled())
417 {
418 log.debug(" TRANSFORMER STREAM OPEN");
419 }
420
421
422 TransformerFactory factory = TransformerFactory.newInstance();
423 Transformer transformer = factory.newTransformer(new StreamSource(new URL(xslPDF).openStream()));
424
425 if (log.isDebugEnabled())
426 {
427 log.debug(" STREAM TRANSFORM TO INPUT STREAM");
428 }
429
430
431 ByteArrayInputStream bais = new ByteArrayInputStream(xmlStream.toByteArray());
432 Source src = new StreamSource(bais);
433
434 if (log.isDebugEnabled())
435 {
436 log.debug(" RESULT CREATION");
437 }
438
439
440 StreamResult res = new StreamResult(out);
441
442 if (log.isDebugEnabled())
443 {
444 log.debug(" TRANSFORM 2 PDF");
445 }
446
447
448 transformer.transform(src, res);
449 }
450 catch (Exception e)
451 {
452 log.error(e.getMessage(), e);
453 }
454 finally
455 {
456 out.close();
457 }
458
459 return out;
460 }
461 }