annotate gnv/src/main/java/de/intevation/gnv/util/XSLTransformer.java @ 998:27029f0ec7e1

Reverted the changes of rev1194. gnv/trunk@1199 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 11 Jun 2010 10:28:50 +0000
parents 9b126bceb0b2
children b1eea43ba133
rev   line source
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
1 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
2
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
3 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
4 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
5 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
6 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
7 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
8
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
9 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
10 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
11 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
12 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
13 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
14 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
15 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
16 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
17 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
18
396
8330c1dbab34 Cleanup imports
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 394
diff changeset
19 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
20 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
21
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
22 /**
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
23 * 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
24 * help of templates into a html representation.
699
af22fa5567a6 Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 690
diff changeset
25 *
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
26 * @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
27 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
28 public class XSLTransformer {
962
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
29
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
30 /**
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
31 * 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
32 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
33 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
34
962
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
35 /**
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
36 * The parameter that is required in the XSL sheet.
e7fda0ae8b92 Add more Javadocs
Tim Englich <tim.englich@intevation.de>
parents: 700
diff changeset
37 */
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
38 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
39
11
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 * Constructor
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
42 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
43 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
44 super();
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
45 }
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
46
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
47 /**
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
48 * Turns <i>toTransform</i> into another format using the template <i>
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
49 * templateFileName</i> with the <i>encoding</i>. The parameters added in
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
50 * {@link #addParameter(java.lang.String, java.lang.String)} are available
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
51 * in the transformer.
699
af22fa5567a6 Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 690
diff changeset
52 *
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
53 * @param toTransform The node to be transformed.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
54 * @param encoding The encoding to be used.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
55 * @param templateFileName The template used for transformation.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
56 * @return the transformed document as string.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
57 */
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
58 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
59 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
60 String resultValue = null;
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
61 try {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
62 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
63 TransformerFactory xformFactory = TransformerFactory.newInstance();
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
64 Transformer transformer = xformFactory
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
65 .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
66
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
67 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
68 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
69 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
70 }
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
71 }
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
72
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
73 ByteArrayOutputStream baos = new ByteArrayOutputStream();
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
74 StreamResult scrResult = new StreamResult(baos);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
75 // log.debug(xmlUtils.writeNode2String(toTransform));
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
76 DOMSource source = new DOMSource(toTransform);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
77 transformer.transform(source, scrResult);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
78 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
79 } catch (TransformerConfigurationException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
80 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
81 } catch (UnsupportedEncodingException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
82 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
83 } catch (TransformerFactoryConfigurationError e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
84 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
85 } catch (TransformerException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
86 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
87 }
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
88
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
89 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
90 }
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
91
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
92
690
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
93 /**
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
94 * Add a parameter that is required in the XSL sheet.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
95 *
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
96 * @param name The name of the parameter.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
97 * @param value The value of the parameter.
254f062e334b Added JavaDoc.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 684
diff changeset
98 */
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
99 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
100 if (params == null) {
998
27029f0ec7e1 Reverted the changes of rev1194.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 993
diff changeset
101 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
102 }
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
103
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
104 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
105 }
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
106 }
700
89ade245ca7a Using unix line endings only.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 699
diff changeset
107 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org