Mercurial > dive4elements > framework
comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XSLTransformer.java @ 382:a94bc2491b41
Added new XSLT processor.
artifacts/trunk@4279 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 19 Apr 2012 18:20:55 +0000 |
parents | |
children | e08f1475facc |
comparison
equal
deleted
inserted
replaced
381:bed51de8ae58 | 382:a94bc2491b41 |
---|---|
1 package de.intevation.artifacts.common.utils; | |
2 | |
3 import java.io.InputStream; | |
4 import java.io.StringWriter; | |
5 | |
6 import java.util.HashMap; | |
7 import java.util.Map; | |
8 | |
9 import javax.xml.transform.Source; | |
10 import javax.xml.transform.Transformer; | |
11 import javax.xml.transform.TransformerConfigurationException; | |
12 import javax.xml.transform.TransformerException; | |
13 import javax.xml.transform.TransformerFactory; | |
14 | |
15 import javax.xml.transform.dom.DOMSource; | |
16 | |
17 import javax.xml.transform.stream.StreamResult; | |
18 import javax.xml.transform.stream.StreamSource; | |
19 | |
20 import org.apache.log4j.Logger; | |
21 | |
22 import org.w3c.dom.Node; | |
23 | |
24 public class XSLTransformer { | |
25 | |
26 private static Logger log = Logger.getLogger(XSLTransformer.class); | |
27 | |
28 protected Map<String, String> parameters; | |
29 | |
30 public XSLTransformer() { | |
31 } | |
32 | |
33 public String transform(Node source, InputStream transform) { | |
34 | |
35 try { | |
36 Source templateSource = new StreamSource(transform); | |
37 TransformerFactory xformFactory = | |
38 TransformerFactory.newInstance(); | |
39 Transformer transformer = | |
40 xformFactory.newTransformer(templateSource); | |
41 | |
42 if (parameters != null) { | |
43 for (Map.Entry<String, String> entry: parameters.entrySet()) { | |
44 transformer.setParameter(entry.getKey(), entry.getValue()); | |
45 } | |
46 } | |
47 | |
48 StringWriter result = new StringWriter(); | |
49 | |
50 DOMSource src = new DOMSource(source); | |
51 StreamResult dst = new StreamResult(result); | |
52 transformer.transform(src, dst); | |
53 | |
54 return result.toString(); | |
55 } | |
56 catch (TransformerConfigurationException tce) { | |
57 log.error(tce, tce); | |
58 } | |
59 catch (TransformerException te) { | |
60 log.error(te, te); | |
61 } | |
62 | |
63 return null; | |
64 } | |
65 | |
66 public void addParameter(String key, String value) { | |
67 if (parameters == null) { | |
68 parameters = new HashMap<String, String>(); | |
69 } | |
70 parameters.put(key, value); | |
71 } | |
72 } | |
73 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |