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