# HG changeset patch # User Sascha L. Teichmann # Date 1334859655 0 # Node ID a94bc2491b418abaf7815037d483e5d830126aa5 # Parent bed51de8ae589144595c6012ac5396dd35e4b4a4 Added new XSLT processor. artifacts/trunk@4279 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r bed51de8ae58 -r a94bc2491b41 ChangeLog --- 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 + + * 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 * artifacts-common/src/main/java/de/intevation/artifacts/common/utils/JSON.java: diff -r bed51de8ae58 -r a94bc2491b41 artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XSLTransformer.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 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 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(); + } + parameters.put(key, value); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :