1 57 58 package com.sun.org.apache.xerces.internal.impl; 59 60 import java.util.Hashtable ; 61 import java.util.Locale ; 62 63 import com.sun.org.apache.xerces.internal.util.DefaultErrorHandler; 64 import com.sun.org.apache.xerces.internal.util.ErrorHandlerProxy; 65 import com.sun.org.apache.xerces.internal.util.MessageFormatter; 66 import com.sun.org.apache.xerces.internal.xni.XMLLocator; 67 import com.sun.org.apache.xerces.internal.xni.XNIException; 68 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent; 69 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager; 70 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException; 71 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 72 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException; 73 import org.xml.sax.ErrorHandler ; 74 75 111 public class XMLErrorReporter 112 implements XMLComponent { 113 114 118 120 125 public static final short SEVERITY_WARNING = 0; 126 127 132 public static final short SEVERITY_ERROR = 1; 133 134 143 public static final short SEVERITY_FATAL_ERROR = 2; 144 145 147 148 protected static final String CONTINUE_AFTER_FATAL_ERROR = 149 Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE; 150 151 153 154 protected static final String ERROR_HANDLER = 155 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 156 157 159 160 private static final String [] RECOGNIZED_FEATURES = { 161 CONTINUE_AFTER_FATAL_ERROR, 162 }; 163 164 165 private static final Boolean [] FEATURE_DEFAULTS = { 166 null, 167 }; 168 169 170 private static final String [] RECOGNIZED_PROPERTIES = { 171 ERROR_HANDLER, 172 }; 173 174 175 private static final Object [] PROPERTY_DEFAULTS = { 176 null, 177 }; 178 179 183 184 protected Locale fLocale; 185 186 187 protected Hashtable fMessageFormatters; 188 189 190 protected XMLErrorHandler fErrorHandler; 191 192 193 protected XMLLocator fLocator; 194 195 196 protected boolean fContinueAfterFatalError; 197 198 204 protected XMLErrorHandler fDefaultErrorHandler; 205 206 210 211 public XMLErrorReporter() { 212 213 228 fMessageFormatters = new Hashtable (); 229 230 } 232 236 241 public void setLocale(Locale locale) { 242 fLocale = locale; 243 } 245 250 public Locale getLocale() { 251 return fLocale ; 252 } 254 259 public void setDocumentLocator(XMLLocator locator) { 260 fLocator = locator; 261 } 263 274 public void putMessageFormatter(String domain, 275 MessageFormatter messageFormatter) { 276 fMessageFormatters.put(domain, messageFormatter); 277 } 279 285 public MessageFormatter getMessageFormatter(String domain) { 286 return (MessageFormatter)fMessageFormatters.get(domain); 287 } 289 295 public MessageFormatter removeMessageFormatter(String domain) { 296 return (MessageFormatter) fMessageFormatters.remove(domain); 297 } 299 314 public void reportError(String domain, String key, Object [] arguments, 315 short severity) throws XNIException { 316 reportError(fLocator, domain, key, arguments, severity); 317 } 319 333 public void reportError(XMLLocator location, 334 String domain, String key, Object [] arguments, 335 short severity) throws XNIException { 336 337 340 MessageFormatter messageFormatter = getMessageFormatter(domain); 342 String message; 343 if (messageFormatter != null) { 344 message = messageFormatter.formatMessage(fLocale, key, arguments); 345 } 346 else { 347 StringBuffer str = new StringBuffer (); 348 str.append(domain); 349 str.append('#'); 350 str.append(key); 351 int argCount = arguments != null ? arguments.length : 0; 352 if (argCount > 0) { 353 str.append('?'); 354 for (int i = 0; i < argCount; i++) { 355 str.append(arguments[i]); 356 if (i < argCount -1) { 357 str.append('&'); 358 } 359 } 360 } 361 message = str.toString(); 362 } 363 XMLParseException parseException = 364 new XMLParseException(location, message); 365 366 XMLErrorHandler errorHandler = fErrorHandler; 368 if (errorHandler == null) { 369 if (fDefaultErrorHandler == null) { 370 fDefaultErrorHandler = new DefaultErrorHandler(); 371 } 372 errorHandler = fDefaultErrorHandler; 373 } 374 375 switch (severity) { 377 case SEVERITY_WARNING: { 378 errorHandler.warning(domain, key, parseException); 379 break; 380 } 381 case SEVERITY_ERROR: { 382 errorHandler.error(domain, key, parseException); 383 break; 384 } 385 case SEVERITY_FATAL_ERROR: { 386 errorHandler.fatalError(domain, key, parseException); 387 if (!fContinueAfterFatalError) { 388 throw parseException; 389 } 390 break; 391 } 392 } 393 394 } 396 400 414 public void reset(XMLComponentManager componentManager) 415 throws XNIException { 416 417 try { 419 fContinueAfterFatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR); 420 } 421 catch (XNIException e) { 422 fContinueAfterFatalError = false; 423 } 424 425 fErrorHandler = (XMLErrorHandler)componentManager.getProperty(ERROR_HANDLER); 427 428 } 430 435 public String [] getRecognizedFeatures() { 436 return (String [])(RECOGNIZED_FEATURES.clone()); 437 } 439 454 public void setFeature(String featureId, boolean state) 455 throws XMLConfigurationException { 456 457 461 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 462 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 463 464 if (suffixLength == Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE.length() && 470 featureId.endsWith(Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE)) { 471 fContinueAfterFatalError = state; 472 } 473 } 474 475 } 477 public boolean getFeature(String featureId) 479 throws XMLConfigurationException { 480 481 485 if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { 486 final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); 487 488 if (suffixLength == Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE.length() && 494 featureId.endsWith(Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE)) { 495 return fContinueAfterFatalError ; 496 } 497 } 498 return false; 499 500 } 502 507 public String [] getRecognizedProperties() { 508 return (String [])(RECOGNIZED_PROPERTIES.clone()); 509 } 511 526 public void setProperty(String propertyId, Object value) 527 throws XMLConfigurationException { 528 529 533 if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { 534 final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); 535 536 if (suffixLength == Constants.ERROR_HANDLER_PROPERTY.length() && 537 propertyId.endsWith(Constants.ERROR_HANDLER_PROPERTY)) { 538 fErrorHandler = (XMLErrorHandler)value; 539 } 540 } 541 542 } 544 553 public Boolean getFeatureDefault(String featureId) { 554 for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { 555 if (RECOGNIZED_FEATURES[i].equals(featureId)) { 556 return FEATURE_DEFAULTS[i]; 557 } 558 } 559 return null; 560 } 562 571 public Object getPropertyDefault(String propertyId) { 572 for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { 573 if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { 574 return PROPERTY_DEFAULTS[i]; 575 } 576 } 577 return null; 578 } 580 583 public XMLErrorHandler getErrorHandler() { 584 return fErrorHandler; 585 } 586 587 588 private ErrorHandler fSaxProxy = null; 589 590 594 public ErrorHandler getSAXErrorHandler() { 595 if( fSaxProxy==null ) 596 fSaxProxy = new ErrorHandlerProxy() { 597 protected XMLErrorHandler getErrorHandler() { 598 return fErrorHandler; 599 } 600 }; 601 return fSaxProxy; 602 } 603 } | Popular Tags |