1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: DefaultErrorHandler.java,v 1.15 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.io.PrintStream;
  21. import java.io.PrintWriter;
  22. import javax.xml.transform.ErrorListener;
  23. import javax.xml.transform.SourceLocator;
  24. import javax.xml.transform.TransformerException;
  25. import com.sun.org.apache.xml.internal.res.XMLErrorResources;
  26. import com.sun.org.apache.xml.internal.res.XMLMessages;
  27. import org.xml.sax.ErrorHandler;
  28. import org.xml.sax.SAXException;
  29. import org.xml.sax.SAXParseException;
  30. /**
  31. * Implement SAX error handler for default reporting.
  32. * @xsl.usage general
  33. */
  34. public class DefaultErrorHandler implements ErrorHandler, ErrorListener
  35. {
  36. PrintWriter m_pw;
  37. /**
  38. * Constructor DefaultErrorHandler
  39. */
  40. public DefaultErrorHandler(PrintWriter pw)
  41. {
  42. m_pw = pw;
  43. }
  44. /**
  45. * Constructor DefaultErrorHandler
  46. */
  47. public DefaultErrorHandler(PrintStream pw)
  48. {
  49. m_pw = new PrintWriter(pw, true);
  50. }
  51. /**
  52. * Constructor DefaultErrorHandler
  53. */
  54. public DefaultErrorHandler()
  55. {
  56. m_pw = new PrintWriter(System.err, true);
  57. }
  58. /**
  59. * Receive notification of a warning.
  60. *
  61. * <p>SAX parsers will use this method to report conditions that
  62. * are not errors or fatal errors as defined by the XML 1.0
  63. * recommendation. The default behaviour is to take no action.</p>
  64. *
  65. * <p>The SAX parser must continue to provide normal parsing events
  66. * after invoking this method: it should still be possible for the
  67. * application to process the document through to the end.</p>
  68. *
  69. * @param exception The warning information encapsulated in a
  70. * SAX parse exception.
  71. * @throws SAXException Any SAX exception, possibly
  72. * wrapping another exception.
  73. */
  74. public void warning(SAXParseException exception) throws SAXException
  75. {
  76. printLocation(m_pw, exception);
  77. m_pw.println("Parser warning: " + exception.getMessage());
  78. }
  79. /**
  80. * Receive notification of a recoverable error.
  81. *
  82. * <p>This corresponds to the definition of "error" in section 1.2
  83. * of the W3C XML 1.0 Recommendation. For example, a validating
  84. * parser would use this callback to report the violation of a
  85. * validity constraint. The default behaviour is to take no
  86. * action.</p>
  87. *
  88. * <p>The SAX parser must continue to provide normal parsing events
  89. * after invoking this method: it should still be possible for the
  90. * application to process the document through to the end. If the
  91. * application cannot do so, then the parser should report a fatal
  92. * error even if the XML 1.0 recommendation does not require it to
  93. * do so.</p>
  94. *
  95. * @param exception The error information encapsulated in a
  96. * SAX parse exception.
  97. * @throws SAXException Any SAX exception, possibly
  98. * wrapping another exception.
  99. */
  100. public void error(SAXParseException exception) throws SAXException
  101. {
  102. //printLocation(exception);
  103. // m_pw.println(exception.getMessage());
  104. throw exception;
  105. }
  106. /**
  107. * Receive notification of a non-recoverable error.
  108. *
  109. * <p>This corresponds to the definition of "fatal error" in
  110. * section 1.2 of the W3C XML 1.0 Recommendation. For example, a
  111. * parser would use this callback to report the violation of a
  112. * well-formedness constraint.</p>
  113. *
  114. * <p>The application must assume that the document is unusable
  115. * after the parser has invoked this method, and should continue
  116. * (if at all) only for the sake of collecting addition error
  117. * messages: in fact, SAX parsers are free to stop reporting any
  118. * other events once this method has been invoked.</p>
  119. *
  120. * @param exception The error information encapsulated in a
  121. * SAX parse exception.
  122. * @throws SAXException Any SAX exception, possibly
  123. * wrapping another exception.
  124. */
  125. public void fatalError(SAXParseException exception) throws SAXException
  126. {
  127. // printLocation(exception);
  128. // m_pw.println(exception.getMessage());
  129. throw exception;
  130. }
  131. /**
  132. * Receive notification of a warning.
  133. *
  134. * <p>SAX parsers will use this method to report conditions that
  135. * are not errors or fatal errors as defined by the XML 1.0
  136. * recommendation. The default behaviour is to take no action.</p>
  137. *
  138. * <p>The SAX parser must continue to provide normal parsing events
  139. * after invoking this method: it should still be possible for the
  140. * application to process the document through to the end.</p>
  141. *
  142. * @param exception The warning information encapsulated in a
  143. * SAX parse exception.
  144. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
  145. * wrapping another exception.
  146. * @see javax.xml.transform.TransformerException
  147. */
  148. public void warning(TransformerException exception) throws TransformerException
  149. {
  150. printLocation(m_pw, exception);
  151. m_pw.println(exception.getMessage());
  152. }
  153. /**
  154. * Receive notification of a recoverable error.
  155. *
  156. * <p>This corresponds to the definition of "error" in section 1.2
  157. * of the W3C XML 1.0 Recommendation. For example, a validating
  158. * parser would use this callback to report the violation of a
  159. * validity constraint. The default behaviour is to take no
  160. * action.</p>
  161. *
  162. * <p>The SAX parser must continue to provide normal parsing events
  163. * after invoking this method: it should still be possible for the
  164. * application to process the document through to the end. If the
  165. * application cannot do so, then the parser should report a fatal
  166. * error even if the XML 1.0 recommendation does not require it to
  167. * do so.</p>
  168. *
  169. * @param exception The error information encapsulated in a
  170. * SAX parse exception.
  171. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
  172. * wrapping another exception.
  173. * @see javax.xml.transform.TransformerException
  174. */
  175. public void error(TransformerException exception) throws TransformerException
  176. {
  177. // printLocation(exception);
  178. // ensureLocationSet(exception);
  179. printLocation(m_pw, exception);
  180. m_pw.println(exception.getMessage());
  181. //throw exception;
  182. }
  183. /**
  184. * Receive notification of a non-recoverable error.
  185. *
  186. * <p>This corresponds to the definition of "fatal error" in
  187. * section 1.2 of the W3C XML 1.0 Recommendation. For example, a
  188. * parser would use this callback to report the violation of a
  189. * well-formedness constraint.</p>
  190. *
  191. * <p>The application must assume that the document is unusable
  192. * after the parser has invoked this method, and should continue
  193. * (if at all) only for the sake of collecting addition error
  194. * messages: in fact, SAX parsers are free to stop reporting any
  195. * other events once this method has been invoked.</p>
  196. *
  197. * @param exception The error information encapsulated in a
  198. * SAX parse exception.
  199. * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
  200. * wrapping another exception.
  201. * @see javax.xml.transform.TransformerException
  202. */
  203. public void fatalError(TransformerException exception) throws TransformerException
  204. {
  205. // printLocation(exception);
  206. // ensureLocationSet(exception);
  207. throw exception;
  208. }
  209. public static void ensureLocationSet(TransformerException exception)
  210. {
  211. // SourceLocator locator = exception.getLocator();
  212. SourceLocator locator = null;
  213. Throwable cause = exception;
  214. // Try to find the locator closest to the cause.
  215. do
  216. {
  217. if(cause instanceof SAXParseException)
  218. {
  219. locator = new SAXSourceLocator((SAXParseException)cause);
  220. }
  221. else if (cause instanceof TransformerException)
  222. {
  223. SourceLocator causeLocator = ((TransformerException)cause).getLocator();
  224. if(null != causeLocator)
  225. locator = causeLocator;
  226. }
  227. if(cause instanceof TransformerException)
  228. cause = ((TransformerException)cause).getCause();
  229. else if(cause instanceof SAXException)
  230. cause = ((SAXException)cause).getException();
  231. else
  232. cause = null;
  233. }
  234. while(null != cause);
  235. exception.setLocator(locator);
  236. }
  237. public static void printLocation(PrintStream pw, TransformerException exception)
  238. {
  239. printLocation(new PrintWriter(pw), exception);
  240. }
  241. public static void printLocation(java.io.PrintStream pw, org.xml.sax.SAXParseException exception)
  242. {
  243. printLocation(new PrintWriter(pw), exception);
  244. }
  245. public static void printLocation(PrintWriter pw, Throwable exception)
  246. {
  247. SourceLocator locator = null;
  248. Throwable cause = exception;
  249. // Try to find the locator closest to the cause.
  250. do
  251. {
  252. if(cause instanceof SAXParseException)
  253. {
  254. locator = new SAXSourceLocator((SAXParseException)cause);
  255. }
  256. else if (cause instanceof TransformerException)
  257. {
  258. SourceLocator causeLocator = ((TransformerException)cause).getLocator();
  259. if(null != causeLocator)
  260. locator = causeLocator;
  261. }
  262. if(cause instanceof TransformerException)
  263. cause = ((TransformerException)cause).getCause();
  264. else if(cause instanceof WrappedRuntimeException)
  265. cause = ((WrappedRuntimeException)cause).getException();
  266. else if(cause instanceof SAXException)
  267. cause = ((SAXException)cause).getException();
  268. else
  269. cause = null;
  270. }
  271. while(null != cause);
  272. if(null != locator)
  273. {
  274. // m_pw.println("Parser fatal error: "+exception.getMessage());
  275. String id = (null != locator.getPublicId() )
  276. ? locator.getPublicId()
  277. : (null != locator.getSystemId())
  278. ? locator.getSystemId() : XMLMessages.createXMLMessage(XMLErrorResources.ER_SYSTEMID_UNKNOWN, null); //"SystemId Unknown";
  279. pw.print(id + "; " +XMLMessages.createXMLMessage("line", null) + locator.getLineNumber()
  280. + "; " +XMLMessages.createXMLMessage("column", null) + locator.getColumnNumber()+"; ");
  281. }
  282. else
  283. pw.print("("+XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null)+")");
  284. }
  285. }