Mercurial > dive4elements > gnv-client
view gnv/src/main/java/de/intevation/gnv/util/XSLTransformer.java @ 1022:28a0628b11b0
Added license file and license header.
gnv/trunk@1258 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 02 Nov 2010 17:15:08 +0000 |
parents | 1b42a86184f6 |
children |
line wrap: on
line source
/* * Copyright (c) 2010 by Intevation GmbH * * This program is free software under the LGPL (>=v2.1) * Read the file LGPL.txt coming with the software for details * or visit http://www.gnu.org/licenses/ if it does not exist. */ package de.intevation.gnv.util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.UnsupportedEncodingException; 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.TransformerFactoryConfigurationError; 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; /** * This xsl transformer is used to transform incoming xml documents with the * help of templates into a html representation. * * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> */ public class XSLTransformer { /** * the logger, used to log exceptions and additonaly information */ private static Logger log = Logger.getLogger(XSLTransformer.class); /** * The parameter that is required in the XSL sheet. */ private Map<String, String> params; /** * Constructor */ public XSLTransformer() { super(); } /** * Turns <i>toTransform</i> into another format using the template <i> * templateFileName</i> with the <i>encoding</i>. The parameters added in * {@link #addParameter(java.lang.String, java.lang.String)} are available * in the transformer. * * @param toTransform The node to be transformed. * @param encoding The encoding to be used. * @param templateFileName The template used for transformation. * @return the transformed document as string. */ public String transform(Node toTransform, String encoding, String templateFileName) { String resultValue = null; try { Source templateSource = new StreamSource(new File(templateFileName)); TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer transformer = xformFactory .newTransformer(templateSource); if (params != null) { for(Map.Entry<String, String> entry: params.entrySet()) { transformer.setParameter(entry.getKey(), entry.getValue()); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamResult scrResult = new StreamResult(baos); // log.debug(xmlUtils.writeNode2String(toTransform)); DOMSource source = new DOMSource(toTransform); transformer.transform(source, scrResult); resultValue = new String(baos.toByteArray(), encoding); } catch (TransformerConfigurationException e) { log.error(e, e); } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (TransformerFactoryConfigurationError e) { log.error(e, e); } catch (TransformerException e) { log.error(e, e); } return resultValue; } /** * Add a parameter that is required in the XSL sheet. * * @param name The name of the parameter. * @param value The value of the parameter. */ public void addParameter(String name, String value) { if (params == null) { params = new HashMap<String, String>(3); } params.put(name, value); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :