1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import org.xml.sax.*;
  59. import javax.xml.transform.ErrorListener;
  60. import javax.xml.transform.TransformerException;
  61. import javax.xml.transform.SourceLocator;
  62. import org.apache.xalan.res.XSLMessages;
  63. import org.apache.xalan.res.XSLTErrorResources;
  64. import java.io.PrintWriter;
  65. import java.io.PrintStream;
  66. /**
  67. * <meta name="usage" content="general"/>
  68. * Implement SAX error handler for default reporting.
  69. */
  70. public class DefaultErrorHandler implements ErrorHandler, ErrorListener
  71. {
  72. PrintWriter m_pw;
  73. /**
  74. * Constructor DefaultErrorHandler
  75. */
  76. public DefaultErrorHandler(PrintWriter pw)
  77. {
  78. m_pw = pw;
  79. }
  80. /**
  81. * Constructor DefaultErrorHandler
  82. */
  83. public DefaultErrorHandler(PrintStream pw)
  84. {
  85. m_pw = new PrintWriter(pw, true);
  86. }
  87. /**
  88. * Constructor DefaultErrorHandler
  89. */
  90. public DefaultErrorHandler()
  91. {
  92. m_pw = new PrintWriter(System.err, true);
  93. }
  94. /**
  95. * Receive notification of a warning.
  96. *
  97. * <p>SAX parsers will use this method to report conditions that
  98. * are not errors or fatal errors as defined by the XML 1.0
  99. * recommendation. The default behaviour is to take no action.</p>
  100. *
  101. * <p>The SAX parser must continue to provide normal parsing events
  102. * after invoking this method: it should still be possible for the
  103. * application to process the document through to the end.</p>
  104. *
  105. * @param exception The warning information encapsulated in a
  106. * SAX parse exception.
  107. * @throws SAXException Any SAX exception, possibly
  108. * wrapping another exception.
  109. */
  110. public void warning(SAXParseException exception) throws SAXException
  111. {
  112. printLocation(m_pw, exception);
  113. m_pw.println("Parser warning: " + exception.getMessage());
  114. }
  115. /**
  116. * Receive notification of a recoverable error.
  117. *
  118. * <p>This corresponds to the definition of "error" in section 1.2
  119. * of the W3C XML 1.0 Recommendation. For example, a validating
  120. * parser would use this callback to report the violation of a
  121. * validity constraint. The default behaviour is to take no
  122. * action.</p>
  123. *
  124. * <p>The SAX parser must continue to provide normal parsing events
  125. * after invoking this method: it should still be possible for the
  126. * application to process the document through to the end. If the
  127. * application cannot do so, then the parser should report a fatal
  128. * error even if the XML 1.0 recommendation does not require it to
  129. * do so.</p>
  130. *
  131. * @param exception The error information encapsulated in a
  132. * SAX parse exception.
  133. * @throws SAXException Any SAX exception, possibly
  134. * wrapping another exception.
  135. */
  136. public void error(SAXParseException exception) throws SAXException
  137. {
  138. //printLocation(exception);
  139. // m_pw.println(exception.getMessage());
  140. throw exception;
  141. }
  142. /**
  143. * Receive notification of a non-recoverable error.
  144. *
  145. * <p>This corresponds to the definition of "fatal error" in
  146. * section 1.2 of the W3C XML 1.0 Recommendation. For example, a
  147. * parser would use this callback to report the violation of a
  148. * well-formedness constraint.</p>
  149. *
  150. * <p>The application must assume that the document is unusable
  151. * after the parser has invoked this method, and should continue
  152. * (if at all) only for the sake of collecting addition error
  153. * messages: in fact, SAX parsers are free to stop reporting any
  154. * other events once this method has been invoked.</p>
  155. *
  156. * @param exception The error information encapsulated in a
  157. * SAX parse exception.
  158. * @throws SAXException Any SAX exception, possibly
  159. * wrapping another exception.
  160. */
  161. public void fatalError(SAXParseException exception) throws SAXException
  162. {
  163. // printLocation(exception);
  164. // m_pw.println(exception.getMessage());
  165. throw exception;
  166. }
  167. /**
  168. * Receive notification of a warning.
  169. *
  170. * <p>SAX parsers will use this method to report conditions that
  171. * are not errors or fatal errors as defined by the XML 1.0
  172. * recommendation. The default behaviour is to take no action.</p>
  173. *
  174. * <p>The SAX parser must continue to provide normal parsing events
  175. * after invoking this method: it should still be possible for the
  176. * application to process the document through to the end.</p>
  177. *
  178. * @param exception The warning information encapsulated in a
  179. * SAX parse exception.
  180. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
  181. * wrapping another exception.
  182. * @see javax.xml.transform.TransformerException
  183. */
  184. public void warning(TransformerException exception) throws TransformerException
  185. {
  186. printLocation(m_pw, exception);
  187. m_pw.println(exception.getMessage());
  188. }
  189. /**
  190. * Receive notification of a recoverable error.
  191. *
  192. * <p>This corresponds to the definition of "error" in section 1.2
  193. * of the W3C XML 1.0 Recommendation. For example, a validating
  194. * parser would use this callback to report the violation of a
  195. * validity constraint. The default behaviour is to take no
  196. * action.</p>
  197. *
  198. * <p>The SAX parser must continue to provide normal parsing events
  199. * after invoking this method: it should still be possible for the
  200. * application to process the document through to the end. If the
  201. * application cannot do so, then the parser should report a fatal
  202. * error even if the XML 1.0 recommendation does not require it to
  203. * do so.</p>
  204. *
  205. * @param exception The error information encapsulated in a
  206. * SAX parse exception.
  207. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
  208. * wrapping another exception.
  209. * @see javax.xml.transform.TransformerException
  210. */
  211. public void error(TransformerException exception) throws TransformerException
  212. {
  213. // printLocation(exception);
  214. // ensureLocationSet(exception);
  215. throw exception;
  216. }
  217. /**
  218. * Receive notification of a non-recoverable error.
  219. *
  220. * <p>This corresponds to the definition of "fatal error" in
  221. * section 1.2 of the W3C XML 1.0 Recommendation. For example, a
  222. * parser would use this callback to report the violation of a
  223. * well-formedness constraint.</p>
  224. *
  225. * <p>The application must assume that the document is unusable
  226. * after the parser has invoked this method, and should continue
  227. * (if at all) only for the sake of collecting addition error
  228. * messages: in fact, SAX parsers are free to stop reporting any
  229. * other events once this method has been invoked.</p>
  230. *
  231. * @param exception The error information encapsulated in a
  232. * SAX parse exception.
  233. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
  234. * wrapping another exception.
  235. * @see javax.xml.transform.TransformerException
  236. */
  237. public void fatalError(TransformerException exception) throws TransformerException
  238. {
  239. // printLocation(exception);
  240. // ensureLocationSet(exception);
  241. throw exception;
  242. }
  243. public static void ensureLocationSet(TransformerException exception)
  244. {
  245. // SourceLocator locator = exception.getLocator();
  246. SourceLocator locator = null;
  247. Throwable cause = exception;
  248. // Try to find the locator closest to the cause.
  249. do
  250. {
  251. if(cause instanceof SAXParseException)
  252. {
  253. locator = new SAXSourceLocator((SAXParseException)cause);
  254. }
  255. else if (cause instanceof TransformerException)
  256. {
  257. SourceLocator causeLocator = ((TransformerException)cause).getLocator();
  258. if(null != causeLocator)
  259. locator = causeLocator;
  260. }
  261. if(cause instanceof TransformerException)
  262. cause = ((TransformerException)cause).getCause();
  263. else if(cause instanceof SAXException)
  264. cause = ((SAXException)cause).getException();
  265. else
  266. cause = null;
  267. }
  268. while(null != cause);
  269. exception.setLocator(locator);
  270. }
  271. public static void printLocation(PrintStream pw, TransformerException exception)
  272. {
  273. printLocation(new PrintWriter(pw), exception);
  274. }
  275. public static void printLocation(java.io.PrintStream pw, org.xml.sax.SAXParseException exception)
  276. {
  277. printLocation(new PrintWriter(pw), exception);
  278. }
  279. public static void printLocation(PrintWriter pw, Throwable exception)
  280. {
  281. SourceLocator locator = null;
  282. Throwable cause = exception;
  283. // Try to find the locator closest to the cause.
  284. do
  285. {
  286. if(cause instanceof SAXParseException)
  287. {
  288. locator = new SAXSourceLocator((SAXParseException)cause);
  289. }
  290. else if (cause instanceof TransformerException)
  291. {
  292. SourceLocator causeLocator = ((TransformerException)cause).getLocator();
  293. if(null != causeLocator)
  294. locator = causeLocator;
  295. }
  296. if(cause instanceof TransformerException)
  297. cause = ((TransformerException)cause).getCause();
  298. else if(cause instanceof WrappedRuntimeException)
  299. cause = ((WrappedRuntimeException)cause).getException();
  300. else if(cause instanceof SAXException)
  301. cause = ((SAXException)cause).getException();
  302. else
  303. cause = null;
  304. }
  305. while(null != cause);
  306. if(null != locator)
  307. {
  308. // m_pw.println("Parser fatal error: "+exception.getMessage());
  309. String id = (null != locator.getPublicId() )
  310. ? locator.getPublicId()
  311. : (null != locator.getSystemId())
  312. ? locator.getSystemId() : XSLMessages.createMessage(XSLTErrorResources.ER_SYSTEMID_UNKNOWN, null); //"SystemId Unknown";
  313. pw.print(id + "; " +XSLMessages.createMessage("line", null) + locator.getLineNumber()
  314. + "; " +XSLMessages.createMessage("column", null) + locator.getColumnNumber()+"; ");
  315. }
  316. else
  317. pw.print("("+XSLMessages.createMessage(XSLTErrorResources.ER_LOCATION_UNKNOWN, null)+")");
  318. }
  319. }