1 57 58 59 package com.sun.org.apache.xerces.internal.util; 60 61 import com.sun.org.apache.xerces.internal.xni.XNIException; 62 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 63 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException; 64 65 import org.w3c.dom.Node ; 66 import org.w3c.dom.DOMError ; 67 import org.w3c.dom.DOMLocator ; 68 import org.w3c.dom.DOMErrorHandler ; 69 import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl; 70 import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl; 71 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 72 73 import java.io.PrintWriter ; 74 import java.util.Hashtable ; 75 76 84 85 92 public class DOMErrorHandlerWrapper 93 implements XMLErrorHandler, DOMErrorHandler { 94 95 96 private static Hashtable fgDOMErrorTypeTable; 97 98 protected DOMErrorHandler fDomErrorHandler; 100 101 boolean eStatus = true ; 103 104 protected PrintWriter fOut; 106 107 public Node fCurrentNode; 110 111 112 protected final XMLErrorCode fErrorCode = new XMLErrorCode(null, null); 113 114 protected final DOMErrorImpl fDOMError = new DOMErrorImpl(); 115 116 static { 117 fgDOMErrorTypeTable = new Hashtable (); 119 fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "DoctypeNotAllowed"), "doctype-not-allowed"); 120 fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "ElementUnterminated"), "wf-invalid-character-in-node-name"); 121 fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "EncodingDeclInvalid"), "unsupported-encoding"); 122 fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "EqRequiredInAttribute"), "wf-invalid-character-in-node-name"); 123 fgDOMErrorTypeTable.put(new XMLErrorCode(XMLMessageFormatter.XML_DOMAIN, "LessthanInAttValue"), "wf-invalid-character"); 124 } 125 126 130 132 public DOMErrorHandlerWrapper() { 133 fOut = new PrintWriter (System.err); 134 } 135 136 137 public DOMErrorHandlerWrapper(DOMErrorHandler domErrorHandler) { 138 fDomErrorHandler = domErrorHandler; 139 } 141 142 146 147 public void setErrorHandler(DOMErrorHandler errorHandler) { 148 fDomErrorHandler = errorHandler; 149 } 151 152 public DOMErrorHandler getErrorHandler(){ 153 return fDomErrorHandler; 154 } 156 160 176 177 public void warning(String domain, String key, 178 XMLParseException exception) throws XNIException { 179 fDOMError.fSeverity = DOMError.SEVERITY_WARNING; 180 fDOMError.fException = exception; 181 fDOMError.fType = key; 182 fDOMError.fRelatedData = fDOMError.fMessage = exception.getMessage(); 183 DOMLocatorImpl locator = fDOMError.fLocator; 184 if (locator != null) { 185 locator.fColumnNumber = exception.getColumnNumber(); 186 locator.fLineNumber = exception.getLineNumber(); 187 locator.fUri = exception.getExpandedSystemId(); 188 locator.fRelatedNode = fCurrentNode; 189 } 190 if (fDomErrorHandler != null) { 191 fDomErrorHandler.handleError(fDOMError); 192 } 193 } 195 211 public void error(String domain, String key, 212 XMLParseException exception) throws XNIException { 213 fDOMError.fSeverity = DOMError.SEVERITY_ERROR; 214 fDOMError.fException = exception; 215 fDOMError.fType = key; 216 fDOMError.fRelatedData = fDOMError.fMessage = exception.getMessage(); 217 DOMLocatorImpl locator = fDOMError.fLocator; 218 if (locator != null) { 219 locator.fColumnNumber = exception.getColumnNumber(); 220 locator.fLineNumber = exception.getLineNumber(); 221 locator.fUri = exception.getExpandedSystemId(); 222 locator.fRelatedNode= fCurrentNode; 223 } 224 if (fDomErrorHandler != null) { 225 fDomErrorHandler.handleError(fDOMError); 226 } 227 } 229 253 public void fatalError(String domain, String key, 254 XMLParseException exception) throws XNIException { 255 fDOMError.fSeverity = DOMError.SEVERITY_FATAL_ERROR; 256 fDOMError.fException = exception; 257 fErrorCode.setValues(domain, key); 258 String domErrorType = (String ) fgDOMErrorTypeTable.get(fErrorCode); 259 fDOMError.fType = (domErrorType != null) ? domErrorType : key; 260 fDOMError.fRelatedData = fDOMError.fMessage = exception.getMessage(); 261 DOMLocatorImpl locator = fDOMError.fLocator; 262 if (locator != null) { 263 locator.fColumnNumber = exception.getColumnNumber(); 264 locator.fLineNumber = exception.getLineNumber(); 265 locator.fUri = exception.getExpandedSystemId(); 266 locator.fRelatedNode = fCurrentNode; 267 } 268 if (fDomErrorHandler != null) { 269 fDomErrorHandler.handleError(fDOMError); 270 } 271 } 273 274 public boolean handleError(DOMError error) { 275 printError(error); 276 return eStatus; 277 } 278 279 280 281 private void printError(DOMError error) { 282 int severity = error.getSeverity(); 283 fOut.print("["); 284 if ( severity == DOMError.SEVERITY_WARNING) { 285 fOut.print("Warning"); 286 } else if ( severity == DOMError.SEVERITY_ERROR) { 287 fOut.print("Error"); 288 } else { 289 fOut.print("FatalError"); 290 eStatus = false ; } 292 fOut.print("] "); 293 DOMLocator locator = error.getLocation(); 294 if (locator != null) { 295 fOut.print(locator.getLineNumber()); 296 fOut.print(":"); 297 fOut.print(locator.getColumnNumber()); 298 fOut.print(":"); 299 fOut.print(locator.getByteOffset()); 300 fOut.print(","); 301 fOut.print(locator.getUtf16Offset()); 302 Node node = locator.getRelatedNode(); 303 if (node != null) { 304 fOut.print("["); 305 fOut.print(node.getNodeName()); 306 fOut.print("]"); 307 } 308 String systemId = locator.getUri(); 309 if (systemId != null) { 310 int index = systemId.lastIndexOf('/'); 311 if (index != -1) 312 systemId = systemId.substring(index + 1); 313 fOut.print(": "); 314 fOut.print(systemId); 315 } 316 317 } 318 319 fOut.print(":"); 320 fOut.print(error.getMessage()); 321 fOut.println(); 322 fOut.flush(); 323 324 } 326 } | Popular Tags |