annotate gnv/src/main/java/de/intevation/gnv/util/XSLTransformer.java @ 396:8330c1dbab34

Cleanup imports gnv/trunk@548 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Jan 2010 20:22:38 +0000
parents 14eecfde4607
children 15ac78a91d1b
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 /**
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 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
4 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
5
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
6 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
7 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
8 import java.io.UnsupportedEncodingException;
396
8330c1dbab34 Cleanup imports
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 394
diff changeset
9
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
10 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
11 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
12
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.Source;
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.Transformer;
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.TransformerConfigurationException;
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.TransformerException;
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.TransformerFactory;
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.TransformerFactoryConfigurationError;
396
8330c1dbab34 Cleanup imports
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 394
diff changeset
19
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 javax.xml.transform.dom.DOMSource;
396
8330c1dbab34 Cleanup imports
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 394
diff changeset
21
11
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.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
23 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
24
396
8330c1dbab34 Cleanup imports
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 394
diff changeset
25 import org.apache.log4j.Logger;
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
26
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 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
28
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 * @author Tim Englich <tim.englich@intevation.de>
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
31 *
11
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 public class XSLTransformer {
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
34 /**
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
35 * 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
36 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
37 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
38
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
39
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
40 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
41
11
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 * Constructor
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
44 */
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
45 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
46 super();
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
47 }
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
48
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
49 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
50 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
51 String resultValue = null;
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
52 try {
396
8330c1dbab34 Cleanup imports
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 394
diff changeset
53 //XMLUtils xmlUtils = new XMLUtils();
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
54 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
55 TransformerFactory xformFactory = TransformerFactory.newInstance();
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
56 Transformer transformer = xformFactory
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
57 .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
58
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
59 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
60 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
61 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
62 }
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
63 }
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
64
36
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
65 ByteArrayOutputStream baos = new ByteArrayOutputStream();
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
66 StreamResult scrResult = new StreamResult(baos);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
67 // log.debug(xmlUtils.writeNode2String(toTransform));
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
68 DOMSource source = new DOMSource(toTransform);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
69 transformer.transform(source, scrResult);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
70 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
71 } catch (TransformerConfigurationException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
72 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
73 } catch (UnsupportedEncodingException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
74 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
75 } catch (TransformerFactoryConfigurationError e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
76 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
77 } catch (TransformerException e) {
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
78 log.error(e, e);
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
79 }
ad381cc47217 Format Code to max 80 Chars per Row
Tim Englich <tim.englich@intevation.de>
parents: 35
diff changeset
80
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
81 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
82 }
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
83
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
84
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
85 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
86 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
87 params = new HashMap<String, String>(3);
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
88 }
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
89
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
90 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
91 }
11
3cb753564552 Integration of the required XSL-Transformation of the Describe-Response of the Artifactdatabase
Tim Englich <tim.englich@intevation.de>
parents:
diff changeset
92 }

http://dive4elements.wald.intevation.org