1 16 17 package org.apache.xerces.impl; 18 19 import java.util.Hashtable ; 20 import java.util.Locale ; 21 22 import org.apache.xerces.util.DefaultErrorHandler; 23 import org.apache.xerces.util.ErrorHandlerProxy; 24 import org.apache.xerces.util.MessageFormatter; 25 import org.apache.xerces.xni.XMLLocator; 26 import org.apache.xerces.xni.XNIException; 27 import org.apache.xerces.xni.parser.XMLComponent; 28 import org.apache.xerces.xni.parser.XMLComponentManager; 29 import org.apache.xerces.xni.parser.XMLConfigurationException; 30 import org.apache.xerces.xni.parser.XMLErrorHandler; 31 import org.apache.xerces.xni.parser.XMLParseException; 32 import org.xml.sax.ErrorHandler ; 33 34 72 public class XMLErrorReporter 73 implements XMLComponent { 74 75 79 81 86 public static final short SEVERITY_WARNING = 0; 87 88 93 public static final short SEVERITY_ERROR = 1; 94 95 104 public static final short SEVERITY_FATAL_ERROR = 2; 105 106 108 109 protected static final String CONTINUE_AFTER_FATAL_ERROR = 110 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 111 112 114 115 protected static final String ERROR_HANDLER = 116 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 117 118 120 121 private static final String [] RECOGNIZED_FEATURES = { 122 CONTINUE_AFTER_FATAL_ERROR, 123 }; 124 125 126 private static final Boolean [] FEATURE_DEFAULTS = { 127 null, 128 }; 129 130 131 private static final String [] RECOGNIZED_PROPERTIES = { 132 ERROR_HANDLER, 133 }; 134 135 136 private static final Object [] PROPERTY_DEFAULTS = { 137 null, 138 }; 139 140 144 145 protected Locale fLocale; 146 147 148 protected Hashtable fMessageFormatters; 149 150 151 protected XMLErrorHandler fErrorHandler; 152 153 154 protected XMLLocator fLocator; 155 156 157 protected boolean fContinueAfterFatalError; 158 159 165 protected XMLErrorHandler fDefaultErrorHandler; 166 167 168 private ErrorHandler fSaxProxy = null; 169 170 174 175 public XMLErrorReporter() { 176 177 192 fMessageFormatters = new Hashtable (); 193 194 } 196 200 205 public void setLocale(Locale locale) { 206 fLocale = locale; 207 } 209 214 public Locale getLocale() { 215 return fLocale ; 216 } 218 223 public void setDocumentLocator(XMLLocator locator) { 224 fLocator = locator; 225 } 227 238 public void putMessageFormatter(String domain, 239 MessageFormatter messageFormatter) { 240 fMessageFormatters.put(domain, messageFormatter); 241 } 243 249 public MessageFormatter getMessageFormatter(String domain) { 250 return (MessageFormatter)fMessageFormatters.get(domain); 251 } 253 259 public MessageFormatter removeMessageFormatter(String domain) { 260 return (MessageFormatter) fMessageFormatters.remove(domain); 261 } 263 278 public void reportError(String domain, String key, Object [] arguments, 279 short severity) throws XNIException { 280 reportError(fLocator, domain, key, arguments, severity); 281 } 283 297 public void reportError(XMLLocator location, 298 String domain, String key, Object [] arguments, 299 short severity) throws XNIException { 300 301 304 MessageFormatter messageFormatter = getMessageFormatter(domain); 306 String message; 307 if (messageFormatter != null) { 308 message = messageFormatter.formatMessage(fLocale, key, arguments); 309 } 310 else { 311 StringBuffer str = new StringBuffer (); 312 str.append(domain); 313 str.append('#'); 314 str.append(key); 315 int argCount = arguments != null ? arguments.length : 0; 316 if (argCount > 0) { 317 str.append('?'); 318 for (int i = 0; i < argCount; i++) { 319 str.append(arguments[i]); 320 if (i < argCount -1) { 321 str.append('&'); 322 } 323 } 324 } 325 message = str.toString(); 326 } 327 XMLParseException parseException = 328 new XMLParseException(location, message); 329 330 XMLErrorHandler errorHandler = fErrorHandler; 332 if (errorHandler == null) { 333 if (fDefaultErrorHandler == null) { 334 fDefaultErrorHandler = new DefaultErrorHandler(); 335 } 336 errorHandler = fDefaultErrorHandler; 337 } 338 339 switch (severity) { 341 case SEVERITY_WARNING: { 342 errorHandler.warning(domain, key, parseException); 343 break; 344 } 345 case SEVERITY_ERROR: { 346 errorHandler.error(domain, key, parseException); 347 break; 348 } 349 case SEVERITY_FATAL_ERROR: { 350 errorHandler.fatalError(domain, key, parseException); 351 if (!fContinueAfterFatalError) { 352 throw parseException; 353 } 354 break; 355 } 356 } 357 358 } 360 364 378 public void reset(XMLComponentManager componentManager) 379 throws XNIException { 380 381 try { 383 fContinueAfterFatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR); 384 } 385 catch (XNIException e) { 386 fContinueAfterFatalError = false; 387 } 388 389 fErrorHandler = (XMLErrorHandler)componentManager.getProperty(ERROR_HANDLER); 391 392 } 394 399 public String [] getRecognizedFeatures() { 400 return (String [])(RECOGNIZED_FEATURES.clone()); 401 } 403 418 public void setFeature(String featureId, boolean state) 419 throws XMLConfigurationException { 420 421 425 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 426 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 427 428 if (suffixLength == Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE.length() && 434 featureId.endsWith(Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE)) { 435 fContinueAfterFatalError = state; 436 } 437 } 438 439 } 441 public boolean getFeature(String featureId) 443 throws XMLConfigurationException { 444 445 449 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 450 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 451 452 if (suffixLength == Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE.length() && 458 featureId.endsWith(Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE)) { 459 return fContinueAfterFatalError ; 460 } 461 } 462 return false; 463 464 } 466 471 public String [] getRecognizedProperties() { 472 return (String [])(RECOGNIZED_PROPERTIES.clone()); 473 } 475 490 public void setProperty(String propertyId, Object value) 491 throws XMLConfigurationException { 492 493 497 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 498 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 499 500 if (suffixLength == Constants.ERROR_HANDLER_PROPERTY.length() && 501 propertyId.endsWith(Constants.ERROR_HANDLER_PROPERTY)) { 502 fErrorHandler = (XMLErrorHandler)value; 503 } 504 } 505 506 } 508 517 public Boolean getFeatureDefault(String featureId) { 518 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 519 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 520 return FEATURE_DEFAULTS[i]; 521 } 522 } 523 return null; 524 } 526 535 public Object getPropertyDefault(String propertyId) { 536 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 537 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 538 return PROPERTY_DEFAULTS[i]; 539 } 540 } 541 return null; 542 } 544 547 public XMLErrorHandler getErrorHandler() { 548 return fErrorHandler; 549 } 550 551 555 public ErrorHandler getSAXErrorHandler() { 556 if (fSaxProxy == null) { 557 fSaxProxy = new ErrorHandlerProxy() { 558 protected XMLErrorHandler getErrorHandler() { 559 return fErrorHandler; 560 } 561 }; 562 } 563 return fSaxProxy; 564 } 565 566 } | Popular Tags |