Mercurial > dive4elements > framework
changeset 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 | bed51de8ae58 |
children | b076c9e9fdfd |
files | ChangeLog artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XSLTransformer.java |
diffstat | 2 files changed, 78 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Wed Apr 18 17:10:10 2012 +0000 +++ b/ChangeLog Thu Apr 19 18:20:55 2012 +0000 @@ -1,3 +1,8 @@ +2012-04-19 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + * artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XSLTransformer.java: + New. Added new XSLT processor similiar to old GNV one. + 2012-04-18 Sascha L. Teichmann <sascha.teichmann@intevation.de> * artifacts-common/src/main/java/de/intevation/artifacts/common/utils/JSON.java:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XSLTransformer.java Thu Apr 19 18:20:55 2012 +0000 @@ -0,0 +1,73 @@ +package de.intevation.artifacts.common.utils; + +import java.io.InputStream; +import java.io.StringWriter; + +import java.util.HashMap; +import java.util.Map; + +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; + +import javax.xml.transform.dom.DOMSource; + +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.apache.log4j.Logger; + +import org.w3c.dom.Node; + +public class XSLTransformer { + + private static Logger log = Logger.getLogger(XSLTransformer.class); + + protected Map<String, String> parameters; + + public XSLTransformer() { + } + + public String transform(Node source, InputStream transform) { + + try { + Source templateSource = new StreamSource(transform); + TransformerFactory xformFactory = + TransformerFactory.newInstance(); + Transformer transformer = + xformFactory.newTransformer(templateSource); + + if (parameters != null) { + for (Map.Entry<String, String> entry: parameters.entrySet()) { + transformer.setParameter(entry.getKey(), entry.getValue()); + } + } + + StringWriter result = new StringWriter(); + + DOMSource src = new DOMSource(source); + StreamResult dst = new StreamResult(result); + transformer.transform(src, dst); + + return result.toString(); + } + catch (TransformerConfigurationException tce) { + log.error(tce, tce); + } + catch (TransformerException te) { + log.error(te, te); + } + + return null; + } + + public void addParameter(String key, String value) { + if (parameters == null) { + parameters = new HashMap<String, String>(); + } + parameters.put(key, value); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :