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