1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001-2003 The Apache Software Foundation.
  6. * All rights 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 "Xerces" 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, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.util;
  58. import com.sun.org.apache.xerces.internal.xni.XMLLocator;
  59. import com.sun.org.apache.xerces.internal.xni.XNIException;
  60. import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
  61. import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
  62. import org.xml.sax.ErrorHandler;
  63. import org.xml.sax.SAXException;
  64. import org.xml.sax.SAXParseException;
  65. /**
  66. * This class wraps a SAX error handler in an XNI error handler.
  67. *
  68. * @see ErrorHandler
  69. *
  70. * @author Andy Clark, IBM
  71. *
  72. * @version $Id: ErrorHandlerWrapper.java,v 1.10 2003/12/17 08:28:29 neeraj Exp $
  73. */
  74. public class ErrorHandlerWrapper
  75. implements XMLErrorHandler {
  76. //
  77. // Data
  78. //
  79. /** The SAX error handler. */
  80. protected ErrorHandler fErrorHandler;
  81. //
  82. // Constructors
  83. //
  84. /** Default constructor. */
  85. public ErrorHandlerWrapper() {}
  86. /** Wraps the specified SAX error handler. */
  87. public ErrorHandlerWrapper(ErrorHandler errorHandler) {
  88. setErrorHandler(errorHandler);
  89. } // <init>(ErrorHandler)
  90. //
  91. // Public methods
  92. //
  93. /** Sets the SAX error handler. */
  94. public void setErrorHandler(ErrorHandler errorHandler) {
  95. fErrorHandler = errorHandler;
  96. } // setErrorHandler(ErrorHandler)
  97. /** Returns the SAX error handler. */
  98. public ErrorHandler getErrorHandler() {
  99. return fErrorHandler;
  100. } // getErrorHandler():ErrorHandler
  101. //
  102. // XMLErrorHandler methods
  103. //
  104. /**
  105. * Reports a warning. Warnings are non-fatal and can be safely ignored
  106. * by most applications.
  107. *
  108. * @param domain The domain of the warning. The domain can be any
  109. * string but is suggested to be a valid URI. The
  110. * domain can be used to conveniently specify a web
  111. * site location of the relevent specification or
  112. * document pertaining to this warning.
  113. * @param key The warning key. This key can be any string and
  114. * is implementation dependent.
  115. * @param exception Exception.
  116. *
  117. * @throws XNIException Thrown to signal that the parser should stop
  118. * parsing the document.
  119. */
  120. public void warning(String domain, String key,
  121. XMLParseException exception) throws XNIException {
  122. if (fErrorHandler != null) {
  123. SAXParseException saxException = createSAXParseException(exception);
  124. try {
  125. fErrorHandler.warning(saxException);
  126. }
  127. catch (SAXParseException e) {
  128. throw createXMLParseException(e);
  129. }
  130. catch (SAXException e) {
  131. throw createXNIException(e);
  132. }
  133. }
  134. } // warning(String,String,XMLParseException)
  135. /**
  136. * Reports an error. Errors are non-fatal and usually signify that the
  137. * document is invalid with respect to its grammar(s).
  138. *
  139. * @param domain The domain of the error. The domain can be any
  140. * string but is suggested to be a valid URI. The
  141. * domain can be used to conveniently specify a web
  142. * site location of the relevent specification or
  143. * document pertaining to this error.
  144. * @param key The error key. This key can be any string and
  145. * is implementation dependent.
  146. * @param exception Exception.
  147. *
  148. * @throws XNIException Thrown to signal that the parser should stop
  149. * parsing the document.
  150. */
  151. public void error(String domain, String key,
  152. XMLParseException exception) throws XNIException {
  153. if (fErrorHandler != null) {
  154. SAXParseException saxException = createSAXParseException(exception);
  155. try {
  156. fErrorHandler.error(saxException);
  157. }
  158. catch (SAXParseException e) {
  159. throw createXMLParseException(e);
  160. }
  161. catch (SAXException e) {
  162. throw createXNIException(e);
  163. }
  164. }
  165. } // error(String,String,XMLParseException)
  166. /**
  167. * Report a fatal error. Fatal errors usually occur when the document
  168. * is not well-formed and signifies that the parser cannot continue
  169. * normal operation.
  170. * <p>
  171. * <strong>Note:</strong> The error handler should <em>always</em>
  172. * throw an <code>XNIException</code> from this method. This exception
  173. * can either be the same exception that is passed as a parameter to
  174. * the method or a new XNI exception object. If the registered error
  175. * handler fails to throw an exception, the continuing operation of
  176. * the parser is undetermined.
  177. *
  178. * @param domain The domain of the fatal error. The domain can be
  179. * any string but is suggested to be a valid URI. The
  180. * domain can be used to conveniently specify a web
  181. * site location of the relevent specification or
  182. * document pertaining to this fatal error.
  183. * @param key The fatal error key. This key can be any string
  184. * and is implementation dependent.
  185. * @param exception Exception.
  186. *
  187. * @throws XNIException Thrown to signal that the parser should stop
  188. * parsing the document.
  189. */
  190. public void fatalError(String domain, String key,
  191. XMLParseException exception) throws XNIException {
  192. if (fErrorHandler != null) {
  193. SAXParseException saxException = createSAXParseException(exception);
  194. try {
  195. fErrorHandler.fatalError(saxException);
  196. }
  197. catch (SAXParseException e) {
  198. throw createXMLParseException(e);
  199. }
  200. catch (SAXException e) {
  201. throw createXNIException(e);
  202. }
  203. }
  204. } // fatalError(String,String,XMLParseException)
  205. //
  206. // Protected methods
  207. //
  208. /** Creates a SAXParseException from an XMLParseException. */
  209. protected static SAXParseException createSAXParseException(XMLParseException exception) {
  210. return new SAXParseException(exception.getMessage(),
  211. exception.getPublicId(),
  212. exception.getExpandedSystemId(),
  213. exception.getLineNumber(),
  214. exception.getColumnNumber(),
  215. exception.getException());
  216. } // createSAXParseException(XMLParseException):SAXParseException
  217. /** Creates an XMLParseException from a SAXParseException. */
  218. protected static XMLParseException createXMLParseException(SAXParseException exception) {
  219. final String fPublicId = exception.getPublicId();
  220. final String fExpandedSystemId = exception.getSystemId();
  221. final int fLineNumber = exception.getLineNumber();
  222. final int fColumnNumber = exception.getColumnNumber();
  223. XMLLocator location = new XMLLocator() {
  224. public void setPublicId(String id) {}
  225. public String getPublicId() { return fPublicId; }
  226. public void setExpandedSystemId( String id) {}
  227. public String getExpandedSystemId() { return fExpandedSystemId; }
  228. public void setBaseSystemId(String id) {}
  229. public String getBaseSystemId() { return null; }
  230. public void setLiteralSystemId(String id) {}
  231. public String getLiteralSystemId() { return null; }
  232. public int getColumnNumber() { return fColumnNumber; }
  233. public void setColumnNumber(int col) {}
  234. public int getLineNumber() { return fLineNumber; }
  235. public void setLineNumber(int line) {}
  236. public String getEncoding() { return null; }
  237. };
  238. return new XMLParseException(location, exception.getMessage(),exception);
  239. } // createXMLParseException(SAXParseException):XMLParseException
  240. /** Creates an XNIException from a SAXException.
  241. NOTE: care should be taken *not* to call this with a SAXParseException; this will
  242. lose information!!! */
  243. protected static XNIException createXNIException(SAXException exception) {
  244. return new XNIException(exception.getMessage(),exception);
  245. } // createXNIException(SAXException):XMLParseException
  246. } // class ErrorHandlerWrapper