1 16 17 package org.apache.xerces.util; 18 19 import org.apache.xerces.xni.XMLLocator; 20 import org.apache.xerces.xni.XNIException; 21 import org.apache.xerces.xni.parser.XMLErrorHandler; 22 import org.apache.xerces.xni.parser.XMLParseException; 23 import org.xml.sax.ErrorHandler ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.SAXParseException ; 26 27 36 public class ErrorHandlerWrapper 37 implements XMLErrorHandler { 38 39 43 44 protected ErrorHandler fErrorHandler; 45 46 50 51 public ErrorHandlerWrapper() {} 52 53 54 public ErrorHandlerWrapper(ErrorHandler errorHandler) { 55 setErrorHandler(errorHandler); 56 } 58 62 63 public void setErrorHandler(ErrorHandler errorHandler) { 64 fErrorHandler = errorHandler; 65 } 67 68 public ErrorHandler getErrorHandler() { 69 return fErrorHandler; 70 } 72 76 92 public void warning(String domain, String key, 93 XMLParseException exception) throws XNIException { 94 95 if (fErrorHandler != null) { 96 SAXParseException saxException = createSAXParseException(exception); 97 98 try { 99 fErrorHandler.warning(saxException); 100 } 101 catch (SAXParseException e) { 102 throw createXMLParseException(e); 103 } 104 catch (SAXException e) { 105 throw createXNIException(e); 106 } 107 } 108 109 } 111 127 public void error(String domain, String key, 128 XMLParseException exception) throws XNIException { 129 130 if (fErrorHandler != null) { 131 SAXParseException saxException = createSAXParseException(exception); 132 133 try { 134 fErrorHandler.error(saxException); 135 } 136 catch (SAXParseException e) { 137 throw createXMLParseException(e); 138 } 139 catch (SAXException e) { 140 throw createXNIException(e); 141 } 142 } 143 144 } 146 170 public void fatalError(String domain, String key, 171 XMLParseException exception) throws XNIException { 172 173 if (fErrorHandler != null) { 174 SAXParseException saxException = createSAXParseException(exception); 175 176 try { 177 fErrorHandler.fatalError(saxException); 178 } 179 catch (SAXParseException e) { 180 throw createXMLParseException(e); 181 } 182 catch (SAXException e) { 183 throw createXNIException(e); 184 } 185 } 186 187 } 189 193 194 protected static SAXParseException createSAXParseException(XMLParseException exception) { 195 return new SAXParseException (exception.getMessage(), 196 exception.getPublicId(), 197 exception.getExpandedSystemId(), 198 exception.getLineNumber(), 199 exception.getColumnNumber(), 200 exception.getException()); 201 } 203 204 protected static XMLParseException createXMLParseException(SAXParseException exception) { 205 final String fPublicId = exception.getPublicId(); 206 final String fExpandedSystemId = exception.getSystemId(); 207 final int fLineNumber = exception.getLineNumber(); 208 final int fColumnNumber = exception.getColumnNumber(); 209 XMLLocator location = new XMLLocator() { 210 public String getPublicId() { return fPublicId; } 211 public String getExpandedSystemId() { return fExpandedSystemId; } 212 public String getBaseSystemId() { return null; } 213 public String getLiteralSystemId() { return null; } 214 public int getColumnNumber() { return fColumnNumber; } 215 public int getLineNumber() { return fLineNumber; } 216 public int getCharacterOffset() { return -1; } 217 public String getEncoding() { return null; } 218 public String getXMLVersion() { return null; } 219 }; 220 return new XMLParseException(location, exception.getMessage(),exception); 221 } 223 226 protected static XNIException createXNIException(SAXException exception) { 227 return new XNIException(exception.getMessage(),exception); 228 } } | Popular Tags |