001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: import org.apache.xerces.xni.XMLLocator;
021: import org.apache.xerces.xni.XNIException;
022: import org.apache.xerces.xni.parser.XMLErrorHandler;
023: import org.apache.xerces.xni.parser.XMLParseException;
024: import org.xml.sax.ErrorHandler;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.SAXParseException;
027:
028: /**
029: * This class wraps a SAX error handler in an XNI error handler.
030: *
031: * @see ErrorHandler
032: *
033: * @author Andy Clark, IBM
034: *
035: * @version $Id: ErrorHandlerWrapper.java 447241 2006-09-18 05:12:57Z mrglavas $
036: */
037: public class ErrorHandlerWrapper implements XMLErrorHandler {
038:
039: //
040: // Data
041: //
042:
043: /** The SAX error handler. */
044: protected ErrorHandler fErrorHandler;
045:
046: //
047: // Constructors
048: //
049:
050: /** Default constructor. */
051: public ErrorHandlerWrapper() {
052: }
053:
054: /** Wraps the specified SAX error handler. */
055: public ErrorHandlerWrapper(ErrorHandler errorHandler) {
056: setErrorHandler(errorHandler);
057: } // <init>(ErrorHandler)
058:
059: //
060: // Public methods
061: //
062:
063: /** Sets the SAX error handler. */
064: public void setErrorHandler(ErrorHandler errorHandler) {
065: fErrorHandler = errorHandler;
066: } // setErrorHandler(ErrorHandler)
067:
068: /** Returns the SAX error handler. */
069: public ErrorHandler getErrorHandler() {
070: return fErrorHandler;
071: } // getErrorHandler():ErrorHandler
072:
073: //
074: // XMLErrorHandler methods
075: //
076:
077: /**
078: * Reports a warning. Warnings are non-fatal and can be safely ignored
079: * by most applications.
080: *
081: * @param domain The domain of the warning. The domain can be any
082: * string but is suggested to be a valid URI. The
083: * domain can be used to conveniently specify a web
084: * site location of the relevent specification or
085: * document pertaining to this warning.
086: * @param key The warning key. This key can be any string and
087: * is implementation dependent.
088: * @param exception Exception.
089: *
090: * @throws XNIException Thrown to signal that the parser should stop
091: * parsing the document.
092: */
093: public void warning(String domain, String key,
094: XMLParseException exception) throws XNIException {
095:
096: if (fErrorHandler != null) {
097: SAXParseException saxException = createSAXParseException(exception);
098:
099: try {
100: fErrorHandler.warning(saxException);
101: } catch (SAXParseException e) {
102: throw createXMLParseException(e);
103: } catch (SAXException e) {
104: throw createXNIException(e);
105: }
106: }
107:
108: } // warning(String,String,XMLParseException)
109:
110: /**
111: * Reports an error. Errors are non-fatal and usually signify that the
112: * document is invalid with respect to its grammar(s).
113: *
114: * @param domain The domain of the error. The domain can be any
115: * string but is suggested to be a valid URI. The
116: * domain can be used to conveniently specify a web
117: * site location of the relevent specification or
118: * document pertaining to this error.
119: * @param key The error key. This key can be any string and
120: * is implementation dependent.
121: * @param exception Exception.
122: *
123: * @throws XNIException Thrown to signal that the parser should stop
124: * parsing the document.
125: */
126: public void error(String domain, String key,
127: XMLParseException exception) throws XNIException {
128:
129: if (fErrorHandler != null) {
130: SAXParseException saxException = createSAXParseException(exception);
131:
132: try {
133: fErrorHandler.error(saxException);
134: } catch (SAXParseException e) {
135: throw createXMLParseException(e);
136: } catch (SAXException e) {
137: throw createXNIException(e);
138: }
139: }
140:
141: } // error(String,String,XMLParseException)
142:
143: /**
144: * Report a fatal error. Fatal errors usually occur when the document
145: * is not well-formed and signifies that the parser cannot continue
146: * normal operation.
147: * <p>
148: * <strong>Note:</strong> The error handler should <em>always</em>
149: * throw an <code>XNIException</code> from this method. This exception
150: * can either be the same exception that is passed as a parameter to
151: * the method or a new XNI exception object. If the registered error
152: * handler fails to throw an exception, the continuing operation of
153: * the parser is undetermined.
154: *
155: * @param domain The domain of the fatal error. The domain can be
156: * any string but is suggested to be a valid URI. The
157: * domain can be used to conveniently specify a web
158: * site location of the relevent specification or
159: * document pertaining to this fatal error.
160: * @param key The fatal error key. This key can be any string
161: * and is implementation dependent.
162: * @param exception Exception.
163: *
164: * @throws XNIException Thrown to signal that the parser should stop
165: * parsing the document.
166: */
167: public void fatalError(String domain, String key,
168: XMLParseException exception) throws XNIException {
169:
170: if (fErrorHandler != null) {
171: SAXParseException saxException = createSAXParseException(exception);
172:
173: try {
174: fErrorHandler.fatalError(saxException);
175: } catch (SAXParseException e) {
176: throw createXMLParseException(e);
177: } catch (SAXException e) {
178: throw createXNIException(e);
179: }
180: }
181:
182: } // fatalError(String,String,XMLParseException)
183:
184: //
185: // Protected methods
186: //
187:
188: /** Creates a SAXParseException from an XMLParseException. */
189: protected static SAXParseException createSAXParseException(
190: XMLParseException exception) {
191: return new SAXParseException(exception.getMessage(), exception
192: .getPublicId(), exception.getExpandedSystemId(),
193: exception.getLineNumber(), exception.getColumnNumber(),
194: exception.getException());
195: } // createSAXParseException(XMLParseException):SAXParseException
196:
197: /** Creates an XMLParseException from a SAXParseException. */
198: protected static XMLParseException createXMLParseException(
199: SAXParseException exception) {
200: final String fPublicId = exception.getPublicId();
201: final String fExpandedSystemId = exception.getSystemId();
202: final int fLineNumber = exception.getLineNumber();
203: final int fColumnNumber = exception.getColumnNumber();
204: XMLLocator location = new XMLLocator() {
205: public String getPublicId() {
206: return fPublicId;
207: }
208:
209: public String getExpandedSystemId() {
210: return fExpandedSystemId;
211: }
212:
213: public String getBaseSystemId() {
214: return null;
215: }
216:
217: public String getLiteralSystemId() {
218: return null;
219: }
220:
221: public int getColumnNumber() {
222: return fColumnNumber;
223: }
224:
225: public int getLineNumber() {
226: return fLineNumber;
227: }
228:
229: public int getCharacterOffset() {
230: return -1;
231: }
232:
233: public String getEncoding() {
234: return null;
235: }
236:
237: public String getXMLVersion() {
238: return null;
239: }
240: };
241: return new XMLParseException(location, exception.getMessage(),
242: exception);
243: } // createXMLParseException(SAXParseException):XMLParseException
244:
245: /** Creates an XNIException from a SAXException.
246: NOTE: care should be taken *not* to call this with a SAXParseException; this will
247: lose information!!! */
248: protected static XNIException createXNIException(
249: SAXException exception) {
250: return new XNIException(exception.getMessage(), exception);
251: } // createXNIException(SAXException):XMLParseException
252: } // class ErrorHandlerWrapper
|