1
2
3
4
5
6
7
8
9
10
11
12 package org.astrogrid.applications.http.script;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import java.io.BufferedReader;
18 import java.io.FileNotFoundException;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.PrintWriter;
24 import java.io.Writer;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.FactoryConfigurationError;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerConfigurationException;
33 import javax.xml.transform.TransformerException;
34 import javax.xml.transform.TransformerFactory;
35 import javax.xml.transform.TransformerFactoryConfigurationError;
36 import javax.xml.transform.dom.DOMResult;
37 import javax.xml.transform.dom.DOMSource;
38 import javax.xml.transform.stream.StreamSource;
39
40 import org.apache.xml.serialize.OutputFormat;
41 import org.apache.xml.serialize.XMLSerializer;
42 import org.astrogrid.applications.beans.v1.WebHttpCall;
43 import org.astrogrid.registry.beans.cea.CeaHttpApplicationType;
44 import org.astrogrid.workflow.beans.v1.Tool;
45 import org.exolab.castor.xml.MarshalException;
46 import org.exolab.castor.xml.Marshaller;
47 import org.exolab.castor.xml.ValidationException;
48 import org.w3c.dom.DOMException;
49 import org.w3c.dom.Document;
50 import org.w3c.dom.Node;
51
52 /***
53 * A Preprocessor that processes using xslt. If no xslt code is supplied, then a
54 * default xslt script is used.
55 * @TODO this needs refactoring so that pre and post-processing can be handled by
56 * the same classes
57 *
58 * @author jdt
59 */
60 public final class XSLTPreprocessor implements Preprocessor {
61 /***
62 * Logger for this class
63 */
64 private static final Log log = LogFactory.getLog(XSLTPreprocessor.class);
65
66 public static final String xmlName = "xslt";
67
68 private Source xslSource;
69
70 private Transformer stylesheet;
71
72 /***
73 * @param code
74 */
75 public XSLTPreprocessor(String code) {
76 if (log.isTraceEnabled()) {
77 log.trace("XSLTPreprocessor(String code = " + code + ") - start");
78 }
79
80
81
82 if (log.isTraceEnabled()) {
83 log.trace("XSLTPreprocessor(String) - end");
84 }
85 }
86
87 /***
88 * Ctor based on the default xslt in the DefaultInputParameters.xsl file
89 */
90 public XSLTPreprocessor() {
91 if (log.isTraceEnabled()) {
92 log.trace("XSLTPreprocessor() - start");
93 }
94
95 try {
96 final InputStream xslFileStream = this.getClass().getResourceAsStream("DefaultInputParameters.xsl");
97 final Source xslSource = new StreamSource(new BufferedReader(new InputStreamReader(xslFileStream)));
98 final TransformerFactory xformFactory = TransformerFactory.newInstance();
99 stylesheet = xformFactory.newTransformer(xslSource);
100
101 xslFileStream.close();
102
103 } catch (TransformerConfigurationException e) {
104
105 log.error("XSLTPreprocessor(): failed to set up Transformer", e);
106 } catch (TransformerFactoryConfigurationError e) {
107
108 log.error("XSLTPreprocessor(): failed to set up Transformer", e);
109 } catch (IOException e) {
110
111 log.info("XSLTPreprocessor(): failed to close xslt input stream - it probably doesn't matter", e);
112 }
113
114 if (log.isTraceEnabled()) {
115 log.trace("XSLTPreprocessor() - end");
116 }
117 }
118
119
120
121
122
123
124
125 public WebHttpCall process(Tool tool, CeaHttpApplicationType app) {
126 if (log.isTraceEnabled()) {
127 log.trace("process(Tool tool = " + tool + ", CeaHttpApplicationType app = " + app + ") - start");
128 }
129
130
131 try {
132
133 Writer writer3 = new PrintWriter(new FileOutputStream("tool-eg.xml"));
134 Writer writer4 = new PrintWriter(new FileOutputStream("webapp-eg.xml"));
135 tool.marshal(writer3);
136 app.marshal(writer4);
137
138
139
140 Writer writer = new PrintWriter(new FileOutputStream("jdt-tool.xml"));
141 Writer writer2 = new PrintWriter(new FileOutputStream("jdt-tool-trans.xml"));
142 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
143 DocumentBuilder builder = factory.newDocumentBuilder();
144
145 Document callingDocument = builder.getDOMImplementation().createDocument(null, "http-app", null);
146 Node root = callingDocument.getFirstChild();
147 Marshaller marshaller = new Marshaller(root);
148 marshaller.marshal(app);
149 marshaller.marshal(tool);
150
151 OutputFormat format = new OutputFormat(callingDocument);
152 XMLSerializer output = new XMLSerializer(writer, format);
153 output.serialize(callingDocument);
154
155 TransformerFactory xformFactory = TransformerFactory.newInstance();
156 InputStream xslFileStream = this.getClass().getResourceAsStream("DefaultInputParameters.xsl");
157 Source xsl = new StreamSource(new BufferedReader(new InputStreamReader(xslFileStream)));
158 Transformer stylesheet = xformFactory.newTransformer(xsl);
159
160
161
162
163
164 Source request = new DOMSource(callingDocument);
165 DOMResult response = new DOMResult();
166 stylesheet.transform(request, response);
167
168 Node node = response.getNode();
169 Document callingDocument2 = (Document) node;
170
171 OutputFormat format2 = new OutputFormat(callingDocument2);
172 XMLSerializer output2 = new XMLSerializer(writer2, format2);
173 output2.serialize(callingDocument2);
174
175 } catch (MarshalException e) {
176 log.error("process(Tool, CeaHttpApplicationType)", e);
177
178
179 e.printStackTrace();
180 } catch (FileNotFoundException e) {
181 log.error("process(Tool, CeaHttpApplicationType)", e);
182
183
184 e.printStackTrace();
185 } catch (DOMException e) {
186 log.error("process(Tool, CeaHttpApplicationType)", e);
187
188
189 e.printStackTrace();
190 } catch (ValidationException e) {
191 log.error("process(Tool, CeaHttpApplicationType)", e);
192
193
194 e.printStackTrace();
195 } catch (FactoryConfigurationError e) {
196 log.error("process(Tool, CeaHttpApplicationType)", e);
197
198
199 e.printStackTrace();
200 } catch (ParserConfigurationException e) {
201 log.error("process(Tool, CeaHttpApplicationType)", e);
202
203
204 e.printStackTrace();
205 } catch (IOException e) {
206 log.error("process(Tool, CeaHttpApplicationType)", e);
207
208
209 e.printStackTrace();
210 } catch (TransformerException e) {
211 log.error("process(Tool, CeaHttpApplicationType)", e);
212
213
214 e.printStackTrace();
215 }
216
217 if (log.isTraceEnabled()) {
218 log.trace("process(Tool, CeaHttpApplicationType) - end - return value = " + null);
219 }
220 return null;
221 }
222
223 }