1 | /*
|
---|
2 | * XMLTransformer.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import org.greenstone.util.GlobalProperties;
|
---|
22 |
|
---|
23 | // XML classes
|
---|
24 | import javax.xml.transform.Transformer;
|
---|
25 | import javax.xml.transform.TransformerFactory;
|
---|
26 | import javax.xml.transform.TransformerConfigurationException;
|
---|
27 | import javax.xml.transform.TransformerException;
|
---|
28 | import javax.xml.transform.ErrorListener;
|
---|
29 |
|
---|
30 | import javax.xml.transform.stream.StreamSource;
|
---|
31 | import javax.xml.transform.dom.DOMSource;
|
---|
32 | import javax.xml.transform.Source;
|
---|
33 | import javax.xml.transform.stream.StreamResult;
|
---|
34 | import javax.xml.transform.dom.DOMResult;
|
---|
35 |
|
---|
36 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
37 | import javax.xml.parsers.DocumentBuilder;
|
---|
38 | import org.w3c.dom.Element;
|
---|
39 | import org.w3c.dom.Document;
|
---|
40 |
|
---|
41 | import org.w3c.dom.Node;
|
---|
42 | import org.w3c.dom.NodeList;
|
---|
43 |
|
---|
44 | // other java classes
|
---|
45 | import java.io.StringReader;
|
---|
46 | import java.io.StringWriter;
|
---|
47 | import java.io.BufferedReader;
|
---|
48 | import java.io.FileReader;
|
---|
49 | import java.io.FileWriter;
|
---|
50 | import java.io.File;
|
---|
51 | import java.util.HashMap;
|
---|
52 | import java.util.Set;
|
---|
53 | import java.util.Map;
|
---|
54 | import java.util.Iterator;
|
---|
55 |
|
---|
56 | import org.apache.xml.utils.DefaultErrorHandler;
|
---|
57 |
|
---|
58 | import org.apache.log4j.*;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * XMLTransformer - utility class for greenstone
|
---|
62 | *
|
---|
63 | * transforms xml using xslt
|
---|
64 | *
|
---|
65 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
66 | * @version $Revision: 25617 $
|
---|
67 | */
|
---|
68 | public class XMLTransformer
|
---|
69 | {
|
---|
70 | private static int debugFileCount = 0; // for unique filenames when debugging XML transformations with physical files
|
---|
71 |
|
---|
72 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLTransformer.class.getName());
|
---|
73 |
|
---|
74 | /** The transformer factory we're using */
|
---|
75 | TransformerFactory t_factory = null;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * The no-arguments constructor.
|
---|
79 | *
|
---|
80 | * Any exceptions thrown are caught internally
|
---|
81 | *
|
---|
82 | * @see javax.xml.transform.TransformerFactory
|
---|
83 | */
|
---|
84 | public XMLTransformer()
|
---|
85 | {
|
---|
86 | // http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/index.html?javax/xml/transform/TransformerFactory.html states that
|
---|
87 | // TransformerFactory.newInstance() looks in jar files for a Factory specified in META-INF/services/javax.xml.transform.TransformerFactory,
|
---|
88 | // else it will use the "platform default"
|
---|
89 | // In this case: xalan.jar's META-INF/services/javax.xml.transform.TransformerFactory contains org.apache.xalan.processor.TransformerFactoryImpl
|
---|
90 | // as required.
|
---|
91 |
|
---|
92 | // This means we no longer have to do a System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");
|
---|
93 | // followed by a this.t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
|
---|
94 | // The System.setProperty step to force the TransformerFactory implementation that gets used, conflicts with
|
---|
95 | // Fedora (visiting the Greenstone server pages breaks the Greenstone-tomcat hosted Fedora pages) as Fedora
|
---|
96 | // does not include the xalan.jar and therefore can't then find the xalan TransformerFactory explicitly set.
|
---|
97 |
|
---|
98 | // Gone back to forcing use of xalan transformer, since other jars like crimson.jar, which may be on some
|
---|
99 | // classpaths, could be be chosen as the TransformerFactory implementation over xalan. This is what used to
|
---|
100 | // give problems before. Instead, have placed copies of the jars that Fedora needs (xalan.jar and serializer.jar
|
---|
101 | // and the related xsltc.jar which it may need) into packages/tomcat/lib so that it's on the server's classpath
|
---|
102 | // and will be found by Fedora.
|
---|
103 |
|
---|
104 | // make sure we are using the xalan transformer
|
---|
105 | System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");
|
---|
106 | try
|
---|
107 | {
|
---|
108 | this.t_factory = org.apache.xalan.processor.TransformerFactoryImpl.newInstance();
|
---|
109 | //this.t_factory = TransformerFactory.newInstance();
|
---|
110 | this.t_factory.setErrorListener(new TransformErrorListener()); // handle errors in the xml Source used to instantiate transformers
|
---|
111 | }
|
---|
112 | catch (Exception e)
|
---|
113 | {
|
---|
114 | logger.error("exception creating t_factory " + e.getMessage());
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Transform an XML document using a XSLT stylesheet
|
---|
120 | *
|
---|
121 | * @param stylesheet
|
---|
122 | * a filename for an XSLT stylesheet
|
---|
123 | * @param xml_in
|
---|
124 | * the XML to be transformed
|
---|
125 | * @return the transformed XML
|
---|
126 | */
|
---|
127 | public String transform(String stylesheet, String xml_in)
|
---|
128 | {
|
---|
129 |
|
---|
130 | try
|
---|
131 | {
|
---|
132 | TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener();
|
---|
133 | transformerErrorListener.setStylesheet(stylesheet);
|
---|
134 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
135 | Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
|
---|
136 |
|
---|
137 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
138 | StringWriter output = new StringWriter();
|
---|
139 | StreamSource streamSource = new StreamSource(new StringReader(xml_in));
|
---|
140 | transformer.setErrorListener(new TransformErrorListener(stylesheet, streamSource));
|
---|
141 | transformer.transform(streamSource, new StreamResult(output));
|
---|
142 | return output.toString();
|
---|
143 | }
|
---|
144 | catch (TransformerConfigurationException e)
|
---|
145 | {
|
---|
146 | logger.error("couldn't create transformer object: " + e.getMessageAndLocation());
|
---|
147 | logger.error(e.getLocationAsString());
|
---|
148 | return "";
|
---|
149 | }
|
---|
150 | catch (TransformerException e)
|
---|
151 | {
|
---|
152 | logger.error("couldn't transform the source: " + e.getMessageAndLocation());
|
---|
153 | return "";
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public String transformToString(Document stylesheet, Document source)
|
---|
158 | {
|
---|
159 | return transformToString(stylesheet, source, null);
|
---|
160 | }
|
---|
161 |
|
---|
162 | public String transformToString(Document stylesheet, Document source, HashMap parameters)
|
---|
163 | {
|
---|
164 |
|
---|
165 | try
|
---|
166 | {
|
---|
167 | TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener();
|
---|
168 | transformerErrorListener.setStylesheet(stylesheet);
|
---|
169 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
170 | Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
|
---|
171 | if (parameters != null)
|
---|
172 | {
|
---|
173 | Set params = parameters.entrySet();
|
---|
174 | Iterator i = params.iterator();
|
---|
175 | while (i.hasNext())
|
---|
176 | {
|
---|
177 | Map.Entry m = (Map.Entry) i.next();
|
---|
178 | transformer.setParameter((String) m.getKey(), m.getValue());
|
---|
179 | }
|
---|
180 | }
|
---|
181 | //transformer.setParameter("page_lang", source.getDocumentElement().getAttribute(GSXML.LANG_ATT));
|
---|
182 |
|
---|
183 | // Use the Transformer to transform an XML Source and send the output to a Result object.
|
---|
184 | StringWriter output = new StringWriter();
|
---|
185 | DOMSource domSource = new DOMSource(source);
|
---|
186 |
|
---|
187 | transformer.setErrorListener(new TransformErrorListener(stylesheet, domSource));
|
---|
188 | transformer.transform(domSource, new StreamResult(output));
|
---|
189 | return output.toString();
|
---|
190 | }
|
---|
191 | catch (TransformerConfigurationException e)
|
---|
192 | {
|
---|
193 | logger.error("couldn't create transformer object: " + e.getMessageAndLocation());
|
---|
194 | logger.error(e.getLocationAsString());
|
---|
195 | return "";
|
---|
196 | }
|
---|
197 | catch (TransformerException e)
|
---|
198 | {
|
---|
199 | logger.error("couldn't transform the source: " + e.getMessageAndLocation());
|
---|
200 | return "";
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Transform an XML document using a XSLT stylesheet, but using a DOMResult
|
---|
206 | * whose node should be set to the Document donated by resultNode
|
---|
207 | */
|
---|
208 | public Node transform_withResultNode(Document stylesheet, Document source, Document resultNode)
|
---|
209 | {
|
---|
210 | return transform(stylesheet, source, null, null, resultNode);
|
---|
211 | }
|
---|
212 |
|
---|
213 | public Node transform(Document stylesheet, Document source)
|
---|
214 | {
|
---|
215 | return transform(stylesheet, source, null, null, null);
|
---|
216 | }
|
---|
217 |
|
---|
218 | public Node transform(Document stylesheet, Document source, HashMap parameters)
|
---|
219 | {
|
---|
220 | return transform(stylesheet, source, parameters, null, null);
|
---|
221 | }
|
---|
222 |
|
---|
223 | public Node transform(Document stylesheet, Document source, HashMap parameters, Document docDocType)
|
---|
224 | {
|
---|
225 | return transform(stylesheet, source, parameters, docDocType, null);
|
---|
226 | }
|
---|
227 |
|
---|
228 | protected Node transform(Document stylesheet, Document source, HashMap parameters, Document docDocType, Document resultNode)
|
---|
229 | {
|
---|
230 | try
|
---|
231 | {
|
---|
232 | // Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
|
---|
233 | TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener();
|
---|
234 | transformerErrorListener.setStylesheet(stylesheet);
|
---|
235 | Transformer transformer = this.t_factory.newTransformer(new DOMSource(stylesheet));
|
---|
236 | //logger.info("XMLTransformer transformer is " + transformer); //done in ErrorListener
|
---|
237 |
|
---|
238 | if (parameters != null)
|
---|
239 | {
|
---|
240 | Set params = parameters.entrySet();
|
---|
241 | Iterator i = params.iterator();
|
---|
242 | while (i.hasNext())
|
---|
243 | {
|
---|
244 | Map.Entry m = (Map.Entry) i.next();
|
---|
245 | transformer.setParameter((String) m.getKey(), m.getValue());
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | // When we transform the DOMResult, we need to make sure the result of
|
---|
250 | // the transformation has a DocType. For that to happen, we need to create
|
---|
251 | // the DOMResult using a Document with a predefined docType.
|
---|
252 | // If we don't have a DocType then do the transformation with a DOMResult
|
---|
253 | // that does not contain any doctype (like we use to do before).
|
---|
254 | DOMResult result = docDocType == null ? new DOMResult() : new DOMResult(docDocType);
|
---|
255 | if (resultNode != null)
|
---|
256 | {
|
---|
257 | result.setNode(resultNode);
|
---|
258 | }
|
---|
259 | DOMSource domSource = new DOMSource(source);
|
---|
260 | transformer.setErrorListener(new TransformErrorListener(stylesheet, domSource));
|
---|
261 | transformer.transform(domSource, result);
|
---|
262 | return result.getNode(); // pass the entire document
|
---|
263 | }
|
---|
264 | catch (TransformerConfigurationException e)
|
---|
265 | {
|
---|
266 | return transformError("XMLTransformer.transform(Doc, Doc, HashMap, Doc)" + "\ncouldn't create transformer object", e);
|
---|
267 | }
|
---|
268 | catch (TransformerException e)
|
---|
269 | {
|
---|
270 | return transformError("XMLTransformer.transform(Doc, Doc, HashMap, Doc)" + "\ncouldn't transform the source", e);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | public Node transform(File stylesheet, File source)
|
---|
275 | {
|
---|
276 | return transform(stylesheet, source, null);
|
---|
277 | }
|
---|
278 |
|
---|
279 | // debugAsFile is only to be set to true when either the stylesheet or source parameters
|
---|
280 | // are not objects of type File. The debugAsFile variable is passed into the
|
---|
281 | // TransformErrorListener. When set to true, the TransformErrorListener will itself create
|
---|
282 | // two files containing the stylesheet and source XML, and try to transform the new source
|
---|
283 | // file with the stylesheet file for debugging purposes.
|
---|
284 | protected Node transform(File stylesheet, File source, Document docDocType)
|
---|
285 | {
|
---|
286 | try
|
---|
287 | {
|
---|
288 | TransformErrorListener transformerErrorListener = (TransformErrorListener) this.t_factory.getErrorListener();
|
---|
289 | transformerErrorListener.setStylesheet(stylesheet);
|
---|
290 | Transformer transformer = this.t_factory.newTransformer(new StreamSource(stylesheet));
|
---|
291 | DOMResult result = (docDocType == null) ? new DOMResult() : new DOMResult(docDocType);
|
---|
292 | StreamSource streamSource = new StreamSource(source);
|
---|
293 |
|
---|
294 | transformer.setErrorListener(new TransformErrorListener(stylesheet, streamSource));
|
---|
295 |
|
---|
296 | transformer.transform(streamSource, result);
|
---|
297 | return result.getNode().getFirstChild();
|
---|
298 | }
|
---|
299 | catch (TransformerConfigurationException e)
|
---|
300 | {
|
---|
301 | return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't create transformer object for files\n" + stylesheet + "\n" + source, e);
|
---|
302 | }
|
---|
303 | catch (TransformerException e)
|
---|
304 | {
|
---|
305 | return transformError("XMLTransformer.transform(File, File)" + "\ncouldn't transform the source for files\n" + stylesheet + "\n" + source, e);
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | // Given a heading string on the sort of transformation error that occurred and the exception object itself,
|
---|
310 | // this method prints the exception to the tomcat window (system.err) and the greenstone log and then returns
|
---|
311 | // an xhtml error page that is constructed from it.
|
---|
312 | protected Node transformError(String heading, TransformerException e)
|
---|
313 | {
|
---|
314 | String message = heading + "\n" + e.getMessage();
|
---|
315 | logger.error(heading + ": " + e.getMessage());
|
---|
316 |
|
---|
317 | String location = e.getLocationAsString();
|
---|
318 | if (location != null)
|
---|
319 | {
|
---|
320 | logger.error(location);
|
---|
321 | message = message + "\n" + location;
|
---|
322 | }
|
---|
323 | System.err.println("****\n" + message + "\n****");
|
---|
324 | return constructErrorXHTMLPage(message);
|
---|
325 | }
|
---|
326 |
|
---|
327 | // Given an error message, splits it into separate lines based on any newlines present and generates an xhtml page
|
---|
328 | // (xml Element) with paragraphs for each line. This is then returned so that it can be displayed in the browser.
|
---|
329 | public static Element constructErrorXHTMLPage(String message)
|
---|
330 | {
|
---|
331 | try
|
---|
332 | {
|
---|
333 | String[] lines = message.split("\n");
|
---|
334 |
|
---|
335 | Document xhtmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
---|
336 | // <html></html>
|
---|
337 | Node htmlNode = xhtmlDoc.createElement("html");
|
---|
338 | xhtmlDoc.appendChild(htmlNode);
|
---|
339 | // <head></head>
|
---|
340 | Node headNode = xhtmlDoc.createElement("head");
|
---|
341 | htmlNode.appendChild(headNode);
|
---|
342 | // <title></title>
|
---|
343 | Node titleNode = xhtmlDoc.createElement("title");
|
---|
344 | headNode.appendChild(titleNode);
|
---|
345 | Node titleString = xhtmlDoc.createTextNode("Error occurred");
|
---|
346 | titleNode.appendChild(titleString);
|
---|
347 |
|
---|
348 | // <body></body>
|
---|
349 | Node bodyNode = xhtmlDoc.createElement("body");
|
---|
350 | htmlNode.appendChild(bodyNode);
|
---|
351 |
|
---|
352 | // finally put the message in the body
|
---|
353 | Node h1Node = xhtmlDoc.createElement("h1");
|
---|
354 | bodyNode.appendChild(h1Node);
|
---|
355 | Node headingString = xhtmlDoc.createTextNode("The following error occurred:");
|
---|
356 | h1Node.appendChild(headingString);
|
---|
357 |
|
---|
358 | //Node textNode = xhtmlDoc.createTextNode(message);
|
---|
359 | //bodyNode.appendChild(textNode);
|
---|
360 |
|
---|
361 | for (int i = 0; i < lines.length; i++)
|
---|
362 | {
|
---|
363 | Node pNode = xhtmlDoc.createElement("p");
|
---|
364 | Node textNode = xhtmlDoc.createTextNode(lines[i]);
|
---|
365 | pNode.appendChild(textNode);
|
---|
366 | bodyNode.appendChild(pNode);
|
---|
367 | }
|
---|
368 |
|
---|
369 | return xhtmlDoc.getDocumentElement();
|
---|
370 |
|
---|
371 | }
|
---|
372 | catch (Exception e)
|
---|
373 | {
|
---|
374 | String errmsg = "Exception trying to construct error xhtml page from message: " + message + "\n" + e.getMessage();
|
---|
375 | System.err.println(errmsg);
|
---|
376 | logger.error(errmsg);
|
---|
377 | return null;
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | // ErrorListener class for both Transformer objects and TransformerFactory objects.
|
---|
382 | // This class can be used to register a handler for any fatal errors, errors and warnings that
|
---|
383 | // may occur when either transforming an xml file with an xslt stylesheet using the XMLTransformer,
|
---|
384 | // or when instantiating a Transformer object using the XMLTransformer's TransformerFactory member var.
|
---|
385 | // The latter case occurs when the xml Source used to instantiate a Transformer from a TransformerFactory
|
---|
386 | // is invalid in some manner, which results in a null Transformer object. However, as no
|
---|
387 | // TransformerConfigurationException or TransformerException are thrown in this case, the errors
|
---|
388 | // would have not been noticed until things go wrong later when trying to use the (null) Transformer.
|
---|
389 | //
|
---|
390 | // The errors caught by this ErrorListener class are printed both to the greenstone.log and to the
|
---|
391 | // tomcat console (System.err), and the error message is stored in the errorMessage variable so that
|
---|
392 | // it can be retrieved and be used to generate an xhtml error page.
|
---|
393 | public class TransformErrorListener implements ErrorListener
|
---|
394 | {
|
---|
395 | protected String errorMessage = null;
|
---|
396 | protected String stylesheet = null;
|
---|
397 | protected Source source = null; // can be DOMSource or StreamSource
|
---|
398 | protected boolean debugAsFile = true; // true if xslt or source are not real physical files
|
---|
399 |
|
---|
400 | // *********** METHODS TO BE CALLED WHEN SETTING AN ERROR LISTENER ON TRANSFORMERFACTORY OBJECTS
|
---|
401 | // The default constructor is only for when setting an ErrorListener on TransformerFactory objects
|
---|
402 | public TransformErrorListener()
|
---|
403 | {
|
---|
404 | this.stylesheet = null;
|
---|
405 | this.source = null;
|
---|
406 | XMLTransformer.debugFileCount++;
|
---|
407 | }
|
---|
408 |
|
---|
409 | public void setStylesheet(Document xslt)
|
---|
410 | {
|
---|
411 | this.debugAsFile = true;
|
---|
412 | this.stylesheet = GSXML.elementToString(xslt.getDocumentElement(), true);
|
---|
413 | this.source = null;
|
---|
414 | }
|
---|
415 |
|
---|
416 | public void setStylesheet(String xslt)
|
---|
417 | {
|
---|
418 | this.debugAsFile = true;
|
---|
419 | this.stylesheet = xslt;
|
---|
420 | this.source = null;
|
---|
421 | }
|
---|
422 |
|
---|
423 | public void setStylesheet(File xslt)
|
---|
424 | {
|
---|
425 | this.debugAsFile = false; // if this constructor is called, we're dealing with physical files for both xslt and source
|
---|
426 | this.stylesheet = xslt.getAbsolutePath();
|
---|
427 | this.source = null;
|
---|
428 | }
|
---|
429 |
|
---|
430 | // *********** METHODS TO BE CALLED WHEN SETTING AN ERROR LISTENER ON TRANSFORMERFACTORY OBJECTS
|
---|
431 | // When setting an ErrorListener on Transformer object, the ErrorListener takes a Stylesheet xslt and a Source
|
---|
432 | public TransformErrorListener(String xslt, Source source)
|
---|
433 | {
|
---|
434 | this.stylesheet = xslt;
|
---|
435 | this.source = source;
|
---|
436 | XMLTransformer.debugFileCount++;
|
---|
437 | }
|
---|
438 |
|
---|
439 | public TransformErrorListener(Document xslt, Source source)
|
---|
440 | {
|
---|
441 | this.stylesheet = GSXML.elementToString(xslt.getDocumentElement(), true);
|
---|
442 | this.source = source;
|
---|
443 | XMLTransformer.debugFileCount++;
|
---|
444 | }
|
---|
445 |
|
---|
446 | public TransformErrorListener(File xslt, Source source)
|
---|
447 | {
|
---|
448 | this.debugAsFile = false; // if this constructor is called, we're dealing with physical files for both xslt and source
|
---|
449 | this.source = source;
|
---|
450 | this.stylesheet = xslt.getAbsolutePath(); // not necessary to get the string from the file
|
---|
451 | // all we were going to do with it *on error* was write it out to a file anyway
|
---|
452 | }
|
---|
453 |
|
---|
454 | // *********** METHODS CALLED AUTOMATICALLY ON ERROR
|
---|
455 |
|
---|
456 | // Receive notification of a recoverable error.
|
---|
457 | public void error(TransformerException exception)
|
---|
458 | {
|
---|
459 | handleError("Error:\n", exception);
|
---|
460 | }
|
---|
461 |
|
---|
462 | // Receive notification of a non-recoverable error.
|
---|
463 | public void fatalError(TransformerException exception)
|
---|
464 | {
|
---|
465 | handleError("Fatal Error:\n", exception);
|
---|
466 | }
|
---|
467 |
|
---|
468 | // Receive notification of a warning.
|
---|
469 | public void warning(TransformerException exception)
|
---|
470 | {
|
---|
471 | handleError("Warning:\n", exception);
|
---|
472 | }
|
---|
473 |
|
---|
474 | public String toString(TransformerException e)
|
---|
475 | {
|
---|
476 | String msg = "Exception encountered was:\n\t";
|
---|
477 | String location = e.getLocationAsString();
|
---|
478 | if (location != null)
|
---|
479 | {
|
---|
480 | msg = msg + "Location: " + location + "\n\t";
|
---|
481 | }
|
---|
482 |
|
---|
483 | return msg + "Message: " + e.getMessage();
|
---|
484 | }
|
---|
485 |
|
---|
486 | // clears the errorPage variable after the first call to this method
|
---|
487 | public String getErrorMessage()
|
---|
488 | {
|
---|
489 | String errMsg = this.errorMessage;
|
---|
490 | if (this.errorMessage != null)
|
---|
491 | {
|
---|
492 | this.errorMessage = null;
|
---|
493 | }
|
---|
494 | return errMsg;
|
---|
495 | }
|
---|
496 |
|
---|
497 | // sets the errorMessage member variable to the data stored in the exception
|
---|
498 | // and writes the errorMessage to the logger and tomcat's System.err
|
---|
499 | protected void handleError(String errorType, TransformerException exception)
|
---|
500 | {
|
---|
501 |
|
---|
502 | this.errorMessage = errorType + toString(exception);
|
---|
503 |
|
---|
504 | // If either the stylesheet or the source to be transformed with it were not files,
|
---|
505 | // so that the transformation was performed in-memory, then the "location" information
|
---|
506 | // during the error handling (if any) wouldn't have been helpful.
|
---|
507 | // To allow proper debugging, we write both stylesheet and source out as physical files
|
---|
508 | // and perform the same transformation again, so that when a transformation error does
|
---|
509 | // occur, the files are not in-memory but can be viewed, and any location information
|
---|
510 | // for the error given by the ErrorListener will be sensible (instead of the unhelpful
|
---|
511 | // "line#0 column#0 in file://somewhere/dummy.xsl").
|
---|
512 | // Note that if the stylesheet and the source it is to transform were both physical
|
---|
513 | // files to start off with, we will not need to perform the same transformation again
|
---|
514 | // since the error reporting would have provided accurate locations for those.
|
---|
515 | if (debugAsFile)
|
---|
516 | {
|
---|
517 |
|
---|
518 | performTransformWithPhysicalFiles(); // will give accurate line numbers
|
---|
519 |
|
---|
520 | // No need to print out the current error message (seen in the Else statement below),
|
---|
521 | // as the recursive call to XMLTransformer.transform(File, File, false) in method
|
---|
522 | // performTransformWithPhysicalFiles() will do this for us.
|
---|
523 | }
|
---|
524 | else
|
---|
525 | {
|
---|
526 | // printing out the error message
|
---|
527 | // since !debugAsFile, we are dealing with physical files,
|
---|
528 | // variable stylesheet would have stored the filename instead of contents
|
---|
529 | this.errorMessage = this.errorMessage + "\nstylesheet filename: " + stylesheet;
|
---|
530 |
|
---|
531 | this.errorMessage += "\nException CAUSE:\n" + exception.getCause();
|
---|
532 | System.err.println("\n****Error transforming xml:\n" + this.errorMessage + "\n****\n");
|
---|
533 | //System.err.println("Stylesheet was:\n + this.stylesheet + "************END STYLESHEET***********\n\n");
|
---|
534 |
|
---|
535 | logger.error(this.errorMessage);
|
---|
536 |
|
---|
537 | // now print out the source to a file, and run the stylesheet on it using a transform()
|
---|
538 | // then any error will be referring to one of these two input files.
|
---|
539 | }
|
---|
540 | }
|
---|
541 |
|
---|
542 | // This method will redo the transformation that went wrong with *real* files:
|
---|
543 | // it writes out the stylesheet and source XML to files first, then performs the transformation
|
---|
544 | // to get the actual line location of where things went wrong (instead of "line#0 column#0 in dummy.xsl")
|
---|
545 | protected void performTransformWithPhysicalFiles()
|
---|
546 | {
|
---|
547 | File webLogsTmpFolder = new File(GlobalProperties.getGSDL3Home() + File.separator + "logs" + File.separator + "tmp");
|
---|
548 | if (!webLogsTmpFolder.exists())
|
---|
549 | {
|
---|
550 | webLogsTmpFolder.mkdirs(); // create any necessary folders
|
---|
551 | }
|
---|
552 | File styleFile = new File(webLogsTmpFolder + File.separator + "stylesheet" + XMLTransformer.debugFileCount + ".xml");
|
---|
553 | File sourceFile = new File(webLogsTmpFolder + File.separator + "source" + XMLTransformer.debugFileCount + ".xml");
|
---|
554 |
|
---|
555 | try
|
---|
556 | {
|
---|
557 | // write stylesheet to a file called stylesheet_systemID in tmp
|
---|
558 | FileWriter styleSheetWriter = new FileWriter(styleFile);
|
---|
559 | styleSheetWriter.write(stylesheet, 0, stylesheet.length());
|
---|
560 | styleSheetWriter.flush();
|
---|
561 | styleSheetWriter.close();
|
---|
562 | }
|
---|
563 | catch (Exception e)
|
---|
564 | {
|
---|
565 | System.err.println("*** Exception when trying to write out stylesheet to " + styleFile.getAbsolutePath());
|
---|
566 | }
|
---|
567 |
|
---|
568 | if (this.source != null)
|
---|
569 | { // ErrorListener was set on a Transformer object
|
---|
570 | try
|
---|
571 | {
|
---|
572 | FileWriter srcWriter = new FileWriter(sourceFile);
|
---|
573 | String contents = "";
|
---|
574 | if (source instanceof DOMSource)
|
---|
575 | {
|
---|
576 | DOMSource domSource = (DOMSource) source;
|
---|
577 | Document doc = (Document) domSource.getNode();
|
---|
578 | contents = GSXML.elementToString(doc.getDocumentElement(), true);
|
---|
579 | //contents = GSXML.xmlNodeToXMLString(domSource.getNode());
|
---|
580 | }
|
---|
581 | else if (source instanceof StreamSource)
|
---|
582 | {
|
---|
583 | StreamSource streamSource = (StreamSource) source;
|
---|
584 | BufferedReader reader = new BufferedReader(streamSource.getReader());
|
---|
585 | String line = "";
|
---|
586 | while ((line = reader.readLine()) != null)
|
---|
587 | {
|
---|
588 | contents = contents + line + "\n";
|
---|
589 | }
|
---|
590 | }
|
---|
591 | srcWriter.write(contents, 0, contents.length());
|
---|
592 | srcWriter.flush();
|
---|
593 | srcWriter.close();
|
---|
594 | }
|
---|
595 | catch (Exception e)
|
---|
596 | {
|
---|
597 | System.err.println("*** Exception when trying to write out stylesheet to " + sourceFile.getAbsolutePath());
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | System.err.println("*****************************************");
|
---|
602 | System.err.println("Look for stylesheet in: " + styleFile.getAbsolutePath());
|
---|
603 | if (this.source != null)
|
---|
604 | { // ErrorListener was set on a Transformer object
|
---|
605 | System.err.println("Look for source XML in: " + sourceFile.getAbsolutePath());
|
---|
606 | }
|
---|
607 |
|
---|
608 | // now perform the transform again, which will assign another TransformErrorListener
|
---|
609 | // but since debuggingAsFile is turned off, we won't recurse into this section of
|
---|
610 | // handling the error again
|
---|
611 | if (this.source != null)
|
---|
612 | { // ErrorListener was set on a Transformer object
|
---|
613 | XMLTransformer.this.transform(styleFile, sourceFile); // calls the File, File version, so debugAsFile will be false
|
---|
614 | }
|
---|
615 |
|
---|
616 | else
|
---|
617 | { // ErrorListener was set on a TransformerFactory object
|
---|
618 |
|
---|
619 | // The recursive step in this case is to perform the instantiation
|
---|
620 | // of the Transformer object again.
|
---|
621 | // Only one TransformerFactory object per XMLTransformer,
|
---|
622 | // and only one TransformerHandler object set on any TransformerFactory
|
---|
623 | // But the stylesheet used to create a Transformer from that TransformerFactory
|
---|
624 | // object changes each time, by calls to setStylesheet(),
|
---|
625 | // Therefore, the debugAsFile state for the single TransformerFactory's
|
---|
626 | // TransformerHandler changes each time also.
|
---|
627 |
|
---|
628 | try
|
---|
629 | {
|
---|
630 | debugAsFile = false;
|
---|
631 | this.stylesheet = styleFile.getAbsolutePath();
|
---|
632 | //TransformErrorListener transformerErrorListener = (TransformErrorListener)XMLTransformer.this.t_factory.getErrorListener();
|
---|
633 | //transformerErrorListener.setStylesheet(styleFile);
|
---|
634 | Transformer transformer = XMLTransformer.this.t_factory.newTransformer(new StreamSource(styleFile));
|
---|
635 | if (transformer == null)
|
---|
636 | {
|
---|
637 | String msg = "XMLTransformer transformer is " + transformer;
|
---|
638 | logger.info(msg);
|
---|
639 | System.out.println(msg + "\n****\n");
|
---|
640 | }
|
---|
641 | }
|
---|
642 | catch (TransformerConfigurationException e)
|
---|
643 | {
|
---|
644 | String message = "Couldn't create transformer object: " + e.getMessageAndLocation();
|
---|
645 | logger.error(message);
|
---|
646 | logger.error(e.getLocationAsString());
|
---|
647 | System.out.println(message);
|
---|
648 | }
|
---|
649 | }
|
---|
650 | }
|
---|
651 | }
|
---|
652 | }
|
---|