1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.xerces.util;
19
20 import org.apache.xerces.xni.XMLLocator;
21 import org.apache.xerces.xni.XNIException;
22 import org.apache.xerces.xni.parser.XMLErrorHandler;
23 import org.apache.xerces.xni.parser.XMLParseException;
24 import org.xml.sax.ErrorHandler;
25 import org.xml.sax.SAXException;
26 import org.xml.sax.SAXParseException;
27
28 /**
29 * This class wraps a SAX error handler in an XNI error handler.
30 *
31 * @see ErrorHandler
32 *
33 * @author Andy Clark, IBM
34 *
35 * @version $Id: ErrorHandlerWrapper.java 447241 2006-09-18 05:12:57Z mrglavas $
36 */
37 public class ErrorHandlerWrapper
38 implements XMLErrorHandler {
39
40 //
41 // Data
42 //
43
44 /** The SAX error handler. */
45 protected ErrorHandler fErrorHandler;
46
47 //
48 // Constructors
49 //
50
51 /** Default constructor. */
52 public ErrorHandlerWrapper() {}
53
54 /** Wraps the specified SAX error handler. */
55 public ErrorHandlerWrapper(ErrorHandler errorHandler) {
56 setErrorHandler(errorHandler);
57 } // <init>(ErrorHandler)
58
59 //
60 // Public methods
61 //
62
63 /** Sets the SAX error handler. */
64 public void setErrorHandler(ErrorHandler errorHandler) {
65 fErrorHandler = errorHandler;
66 } // setErrorHandler(ErrorHandler)
67
68 /** Returns the SAX error handler. */
69 public ErrorHandler getErrorHandler() {
70 return fErrorHandler;
71 } // getErrorHandler():ErrorHandler
72
73 //
74 // XMLErrorHandler methods
75 //
76
77 /**
78 * Reports a warning. Warnings are non-fatal and can be safely ignored
79 * by most applications.
80 *
81 * @param domain The domain of the warning. The domain can be any
82 * string but is suggested to be a valid URI. The
83 * domain can be used to conveniently specify a web
84 * site location of the relevent specification or
85 * document pertaining to this warning.
86 * @param key The warning key. This key can be any string and
87 * is implementation dependent.
88 * @param exception Exception.
89 *
90 * @throws XNIException Thrown to signal that the parser should stop
91 * parsing the document.
92 */
93 public void warning(String domain, String key,
94 XMLParseException exception) throws XNIException {
95
96 if (fErrorHandler != null) {
97 SAXParseException saxException = createSAXParseException(exception);
98
99 try {
100 fErrorHandler.warning(saxException);
101 }
102 catch (SAXParseException e) {
103 throw createXMLParseException(e);
104 }
105 catch (SAXException e) {
106 throw createXNIException(e);
107 }
108 }
109
110 } // warning(String,String,XMLParseException)
111
112 /**
113 * Reports an error. Errors are non-fatal and usually signify that the
114 * document is invalid with respect to its grammar(s).
115 *
116 * @param domain The domain of the error. The domain can be any
117 * string but is suggested to be a valid URI. The
118 * domain can be used to conveniently specify a web
119 * site location of the relevent specification or
120 * document pertaining to this error.
121 * @param key The error key. This key can be any string and
122 * is implementation dependent.
123 * @param exception Exception.
124 *
125 * @throws XNIException Thrown to signal that the parser should stop
126 * parsing the document.
127 */
128 public void error(String domain, String key,
129 XMLParseException exception) throws XNIException {
130
131 if (fErrorHandler != null) {
132 SAXParseException saxException = createSAXParseException(exception);
133
134 try {
135 fErrorHandler.error(saxException);
136 }
137 catch (SAXParseException e) {
138 throw createXMLParseException(e);
139 }
140 catch (SAXException e) {
141 throw createXNIException(e);
142 }
143 }
144
145 } // error(String,String,XMLParseException)
146
147 /**
148 * Report a fatal error. Fatal errors usually occur when the document
149 * is not well-formed and signifies that the parser cannot continue
150 * normal operation.
151 * <p>
152 * <strong>Note:</strong> The error handler should <em>always</em>
153 * throw an <code>XNIException</code> from this method. This exception
154 * can either be the same exception that is passed as a parameter to
155 * the method or a new XNI exception object. If the registered error
156 * handler fails to throw an exception, the continuing operation of
157 * the parser is undetermined.
158 *
159 * @param domain The domain of the fatal error. The domain can be
160 * any string but is suggested to be a valid URI. The
161 * domain can be used to conveniently specify a web
162 * site location of the relevent specification or
163 * document pertaining to this fatal error.
164 * @param key The fatal error key. This key can be any string
165 * and is implementation dependent.
166 * @param exception Exception.
167 *
168 * @throws XNIException Thrown to signal that the parser should stop
169 * parsing the document.
170 */
171 public void fatalError(String domain, String key,
172 XMLParseException exception) throws XNIException {
173
174 if (fErrorHandler != null) {
175 SAXParseException saxException = createSAXParseException(exception);
176
177 try {
178 fErrorHandler.fatalError(saxException);
179 }
180 catch (SAXParseException e) {
181 throw createXMLParseException(e);
182 }
183 catch (SAXException e) {
184 throw createXNIException(e);
185 }
186 }
187
188 } // fatalError(String,String,XMLParseException)
189
190 //
191 // Protected methods
192 //
193
194 /** Creates a SAXParseException from an XMLParseException. */
195 protected static SAXParseException createSAXParseException(XMLParseException exception) {
196 return new SAXParseException(exception.getMessage(),
197 exception.getPublicId(),
198 exception.getExpandedSystemId(),
199 exception.getLineNumber(),
200 exception.getColumnNumber(),
201 exception.getException());
202 } // createSAXParseException(XMLParseException):SAXParseException
203
204 /** Creates an XMLParseException from a SAXParseException. */
205 protected static XMLParseException createXMLParseException(SAXParseException exception) {
206 final String fPublicId = exception.getPublicId();
207 final String fExpandedSystemId = exception.getSystemId();
208 final int fLineNumber = exception.getLineNumber();
209 final int fColumnNumber = exception.getColumnNumber();
210 XMLLocator location = new XMLLocator() {
211 public String getPublicId() { return fPublicId; }
212 public String getExpandedSystemId() { return fExpandedSystemId; }
213 public String getBaseSystemId() { return null; }
214 public String getLiteralSystemId() { return null; }
215 public int getColumnNumber() { return fColumnNumber; }
216 public int getLineNumber() { return fLineNumber; }
217 public int getCharacterOffset() { return -1; }
218 public String getEncoding() { return null; }
219 public String getXMLVersion() { return null; }
220 };
221 return new XMLParseException(location, exception.getMessage(),exception);
222 } // createXMLParseException(SAXParseException):XMLParseException
223
224 /** Creates an XNIException from a SAXException.
225 NOTE: care should be taken *not* to call this with a SAXParseException; this will
226 lose information!!! */
227 protected static XNIException createXNIException(SAXException exception) {
228 return new XNIException(exception.getMessage(),exception);
229 } // createXNIException(SAXException):XMLParseException
230 } // class ErrorHandlerWrapper