annotate 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
rev   line source
1022
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
1 /*
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
2 * Copyright (c) 2010 by Intevation GmbH
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
3 *
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
4 * This program is free software under the LGPL (>=v2.1)
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
5 * Read the file LGPL.txt coming with the software for details
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
7 */
28a0628b11b0 Added license file and license header.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1008
diff changeset
8
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
9 package de.intevation.gnv.util;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
10
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
11 import java.io.ByteArrayOutputStream;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
12 import java.io.File;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
13 import java.io.UnsupportedEncodingException;
394
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
14 import java.util.HashMap;
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
15 import java.util.Map;
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
16
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
17 import javax.xml.transform.Source;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
18 import javax.xml.transform.Transformer;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
19 import javax.xml.transform.TransformerConfigurationException;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
20 import javax.xml.transform.TransformerException;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
21 import javax.xml.transform.TransformerFactory;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
22 import javax.xml.transform.TransformerFactoryConfigurationError;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
23 import javax.xml.transform.dom.DOMSource;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
24 import javax.xml.transform.stream.StreamResult;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
25 import javax.xml.transform.stream.StreamSource;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
26
396
8330c1dbab34 Cleanup imports
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 394
diff changeset
27 import org.apache.log4j.Logger;
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
28 import org.w3c.dom.Node;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
29
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
30 /**
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
31 * This xsl transformer is used to transform incoming xml documents with the
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
32 * help of templates into a html representation.
699
af22fa5567a6 Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 690
diff changeset
33 *
684
57fa8019fbdc Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 683
diff changeset
34 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
35 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
36 public class XSLTransformer {
962
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
37
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
38 /**
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
39 * the logger, used to log exceptions and additonaly information
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
40 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
41 private static Logger log = Logger.getLogger(XSLTransformer.class);
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
42
962
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
43 /**
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
44 * The parameter that is required in the XSL sheet.
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
45 */
394
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
46 private Map<String, String> params;
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
47
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
48 /**
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
49 * Constructor
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
50 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
51 public XSLTransformer() {
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
52 super();
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
53 }
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
54
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
55 /**
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
56 * Turns <i>toTransform</i> into another format using the template <i>
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
57 * templateFileName</i> with the <i>encoding</i>. The parameters added in
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
58 * {@link #addParameter(java.lang.String, java.lang.String)} are available
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
59 * in the transformer.
699
af22fa5567a6 Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 690
diff changeset
60 *
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
61 * @param toTransform The node to be transformed.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
62 * @param encoding The encoding to be used.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
63 * @param templateFileName The template used for transformation.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
64 * @return the transformed document as string.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
65 */
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
66 public String transform(Node toTransform, String encoding,
38
fccf90761825 Format Code to max 80 Chars per Row better Formatter choosen
Tim Englich <tim.englich@intevation.de>
parents: 36
diff changeset
67 String templateFileName) {
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
68 String resultValue = null;
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
69 try {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
70 Source templateSource = new StreamSource(new File(templateFileName));
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
71 TransformerFactory xformFactory = TransformerFactory.newInstance();
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
72 Transformer transformer = xformFactory
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
73 .newTransformer(templateSource);
394
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
74
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
75 if (params != null) {
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
76 for(Map.Entry<String, String> entry: params.entrySet()) {
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
77 transformer.setParameter(entry.getKey(), entry.getValue());
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
78 }
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
79 }
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
80
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
81 ByteArrayOutputStream baos = new ByteArrayOutputStream();
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
82 StreamResult scrResult = new StreamResult(baos);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
83 // log.debug(xmlUtils.writeNode2String(toTransform));
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
84 DOMSource source = new DOMSource(toTransform);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
85 transformer.transform(source, scrResult);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
86 resultValue = new String(baos.toByteArray(), encoding);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
87 } catch (TransformerConfigurationException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
88 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
89 } catch (UnsupportedEncodingException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
90 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
91 } catch (TransformerFactoryConfigurationError e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
92 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
93 } catch (TransformerException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
94 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
95 }
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
96
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
97 return resultValue;
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
98 }
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
99
394
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
100
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
101 /**
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
102 * Add a parameter that is required in the XSL sheet.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
103 *
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
104 * @param name The name of the parameter.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
105 * @param value The value of the parameter.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
106 */
394
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
107 public void addParameter(String name, String value) {
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
108 if (params == null) {
1008
1b42a86184f6 Removed the changes of rev1206 and 1214.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1002
diff changeset
109 params = new HashMap<String, String>(3);
394
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
110 }
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
111
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
112 params.put(name, value);
14eecfde4607 Render links to step back to previous states into gui. Added controller to advance back to these states.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 38
diff changeset
113 }
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
114 }
700
89ade245ca7a Using unix line endings only.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 699
diff changeset
115 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org