FileDocCategorySizeDatePackage
XMLErrorReporter.javaAPI DocJava SE 6 API21621Tue Jun 10 00:22:40 BST 2008com.sun.org.apache.xerces.internal.impl

XMLErrorReporter

public class XMLErrorReporter extends Object implements XMLComponent
This class is a common element of all parser configurations and is used to report errors that occur. This component can be queried by parser components from the component manager using the following property ID:
http://apache.org/xml/properties/internal/error-reporter

Errors are separated into domains that categorize a class of errors. In a parser configuration, the parser would register a MessageFormatter for each domain that is capable of localizing error messages and formatting them based on information about the error. Any parser component can invent new error domains and register additional message formatters to localize messages in those domains.

This component requires the following features and properties from the component manager that uses it:

  • http://apache.org/xml/properties/internal/error-handler

This component can use the following features and properties but they are not required:

  • http://apache.org/xml/features/continue-after-fatal-error
xerces.internal
see
MessageFormatter
author
Eric Ye, IBM
author
Andy Clark, IBM
version
$Id: XMLErrorReporter.java,v 1.1.2.1 2005/08/01 03:34:09 jeffsuttor Exp $

Fields Summary
public static final short
SEVERITY_WARNING
Severity: warning. Warnings represent informational messages only that should not be considered serious enough to stop parsing or indicate an error in the document's validity.
public static final short
SEVERITY_ERROR
Severity: error. Common causes of errors are document structure and/or content that that does not conform to the grammar rules specified for the document. These are typically validation errors.
public static final short
SEVERITY_FATAL_ERROR
Severity: fatal error. Fatal errors are errors in the syntax of the XML document or invalid byte sequences for a given encoding. The XML 1.0 Specification mandates that errors of this type are not recoverable.

Note: The parser does have a "continue after fatal error" feature but it should be used with extreme caution and care.

protected static final String
CONTINUE_AFTER_FATAL_ERROR
Feature identifier: continue after fatal error.
protected static final String
ERROR_HANDLER
Property identifier: error handler.
private static final String[]
RECOGNIZED_FEATURES
Recognized features.
private static final Boolean[]
FEATURE_DEFAULTS
Feature defaults.
private static final String[]
RECOGNIZED_PROPERTIES
Recognized properties.
private static final Object[]
PROPERTY_DEFAULTS
Property defaults.
protected Locale
fLocale
The locale to be used to format error messages.
protected Hashtable
fMessageFormatters
Mapping of Message formatters for domains.
protected XMLErrorHandler
fErrorHandler
Error handler.
protected XMLLocator
fLocator
Document locator.
protected boolean
fContinueAfterFatalError
Continue after fatal error feature.
protected XMLErrorHandler
fDefaultErrorHandler
Default error handler. This error handler is only used in the absence of a registered error handler so that errors are not "swallowed" silently. This is one of the most common "problems" reported by users of the parser.
private ErrorHandler
fSaxProxy
Constructors Summary
public XMLErrorReporter()
Constructs an error reporter with a locator.


    //
    // Constructors
    //

            
      

        // REVISIT: [Q] Should the locator be passed to the reportError
        //              method? Otherwise, there is no way for a parser
        //              component to store information about where an
        //              error occurred so as to report it later. 
        //
        //              An example would be to record the location of
        //              IDREFs so that, at the end of the document, if
        //              there is no associated ID declared, the error
        //              could report the location information of the
        //              reference. -Ac
        //
        // NOTE: I added another reportError method that allows the
        //       caller to specify the location of the error being
        //       reported. -Ac

        fMessageFormatters = new Hashtable();

    
Methods Summary
public com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandlergetErrorHandler()
Get the internal XMLErrrorHandler.

        return fErrorHandler;
    
public booleangetFeature(java.lang.String featureId)


        //
        // Xerces features
        //

        if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
        	final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
        	
            //
            // http://apache.org/xml/features/continue-after-fatal-error
            //   Allows the parser to continue after a fatal error.
            //   Normally, a fatal error would stop the parse.
            //
            if (suffixLength == Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE.length() && 
                featureId.endsWith(Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE)) {
                return fContinueAfterFatalError ;
            }
        }
        return false;

    
public java.lang.BooleangetFeatureDefault(java.lang.String featureId)
Returns the default state for a feature, or null if this component does not want to report a default value for this feature.

param
featureId The feature identifier.
since
Xerces 2.2.0

        for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) {
            if (RECOGNIZED_FEATURES[i].equals(featureId)) {
                return FEATURE_DEFAULTS[i];
            }
        }
        return null;
    
public java.util.LocalegetLocale()
Gets the current locale.

return
the current Locale

        return fLocale ;
    
public com.sun.org.apache.xerces.internal.util.MessageFormattergetMessageFormatter(java.lang.String domain)
Returns the message formatter associated with the specified domain, or null if no message formatter is registered for that domain.

param
domain The domain of the message formatter.

        return (MessageFormatter)fMessageFormatters.get(domain);
    
public java.lang.ObjectgetPropertyDefault(java.lang.String propertyId)
Returns the default state for a property, or null if this component does not want to report a default value for this property.

param
propertyId The property identifier.
since
Xerces 2.2.0

        for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) {
            if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) {
                return PROPERTY_DEFAULTS[i];
            }
        }
        return null;
    
public java.lang.String[]getRecognizedFeatures()
Returns a list of feature identifiers that are recognized by this component. This method may return null if no features are recognized by this component.

        return (String[])(RECOGNIZED_FEATURES.clone());
    
public java.lang.String[]getRecognizedProperties()
Returns a list of property identifiers that are recognized by this component. This method may return null if no properties are recognized by this component.

        return (String[])(RECOGNIZED_PROPERTIES.clone());
    
public org.xml.sax.ErrorHandlergetSAXErrorHandler()
Gets the internal XMLErrorHandler as SAX ErrorHandler.

    
                
       
        if( fSaxProxy==null )
            fSaxProxy = new ErrorHandlerProxy() {
                protected XMLErrorHandler getErrorHandler() {
                    return fErrorHandler;
                }
            };
        return fSaxProxy;
    
public voidputMessageFormatter(java.lang.String domain, com.sun.org.apache.xerces.internal.util.MessageFormatter messageFormatter)
Registers a message formatter for the specified domain.

Note: Registering a message formatter for a domain when there is already a formatter registered will cause the previous formatter to be lost. This method replaces any previously registered message formatter for the specified domain.

param
domain
param
messageFormatter

        fMessageFormatters.put(domain, messageFormatter);
    
public com.sun.org.apache.xerces.internal.util.MessageFormatterremoveMessageFormatter(java.lang.String domain)
Removes the message formatter for the specified domain and returns the removed message formatter.

param
domain The domain of the message formatter.

        return (MessageFormatter) fMessageFormatters.remove(domain);
    
public voidreportError(java.lang.String domain, java.lang.String key, java.lang.Object[] arguments, short severity)
Reports an error. The error message passed to the error handler is formatted for the locale by the message formatter installed for the specified error domain.

param
domain The error domain.
param
key The key of the error message.
param
arguments The replacement arguments for the error message, if needed.
param
severity The severity of the error.
see
#SEVERITY_WARNING
see
#SEVERITY_ERROR
see
#SEVERITY_FATAL_ERROR

        reportError(fLocator, domain, key, arguments, severity);
    
public voidreportError(com.sun.org.apache.xerces.internal.xni.XMLLocator location, java.lang.String domain, java.lang.String key, java.lang.Object[] arguments, short severity)
Reports an error at a specific location.

param
location The error location.
param
domain The error domain.
param
key The key of the error message.
param
arguments The replacement arguments for the error message, if needed.
param
severity The severity of the error.
see
#SEVERITY_WARNING
see
#SEVERITY_ERROR
see
#SEVERITY_FATAL_ERROR


        // REVISIT: [Q] Should we do anything about invalid severity
        //              parameter? -Ac
        
        // format error message and create parse exception
        MessageFormatter messageFormatter = getMessageFormatter(domain);
        String message;
        if (messageFormatter != null) {
            message = messageFormatter.formatMessage(fLocale, key, arguments);
        }
        else {
            StringBuffer str = new StringBuffer();
            str.append(domain);
            str.append('#");
            str.append(key);
            int argCount = arguments != null ? arguments.length : 0;
            if (argCount > 0) {
                str.append('?");
                for (int i = 0; i < argCount; i++) {
                    str.append(arguments[i]);
                    if (i < argCount -1) {
                        str.append('&");
                    }
                }
            }
            message = str.toString();
        }
        XMLParseException parseException = 
            new XMLParseException(location, message);

        // get error handler
        XMLErrorHandler errorHandler = fErrorHandler;
        if (errorHandler == null) {
            if (fDefaultErrorHandler == null) {
                fDefaultErrorHandler = new DefaultErrorHandler();
            }
            errorHandler = fDefaultErrorHandler;
        }

        // call error handler
        switch (severity) {
            case SEVERITY_WARNING: {
                errorHandler.warning(domain, key, parseException);
                break;
            }
            case SEVERITY_ERROR: {
                errorHandler.error(domain, key, parseException);
                break;
            }
            case SEVERITY_FATAL_ERROR: {
                errorHandler.fatalError(domain, key, parseException);
                if (!fContinueAfterFatalError) {
                    throw parseException;
                }
                break;
            }
        }

    
public voidreset(com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager componentManager)
Resets the component. The component can query the component manager about any features and properties that affect the operation of the component.

param
componentManager The component manager.
throws
SAXException Thrown by component on initialization error. For example, if a feature or property is required for the operation of the component, the component manager may throw a SAXNotRecognizedException or a SAXNotSupportedException.


        // features
        try {
            fContinueAfterFatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR);
        }
        catch (XNIException e) {
            fContinueAfterFatalError = false;
        }

        // properties
        fErrorHandler = (XMLErrorHandler)componentManager.getProperty(ERROR_HANDLER);

    
public voidsetDocumentLocator(com.sun.org.apache.xerces.internal.xni.XMLLocator locator)
Sets the document locator.

param
locator The locator.

        fLocator = locator;
    
public voidsetFeature(java.lang.String featureId, boolean state)
Sets the state of a feature. This method is called by the component manager any time after reset when a feature changes state.

Note: Components should silently ignore features that do not affect the operation of the component.

param
featureId The feature identifier.
param
state The state of the feature.
throws
SAXNotRecognizedException The component should not throw this exception.
throws
SAXNotSupportedException The component should not throw this exception.


        //
        // Xerces features
        //

        if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
            final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length();
            
            //
            // http://apache.org/xml/features/continue-after-fatal-error
            //   Allows the parser to continue after a fatal error.
            //   Normally, a fatal error would stop the parse.
            //
            if (suffixLength == Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE.length() && 
                featureId.endsWith(Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE)) {
                fContinueAfterFatalError = state;
            }
        }

    
public voidsetLocale(java.util.Locale locale)
Sets the current locale.

param
locale The new locale.

        fLocale = locale;
    
public voidsetProperty(java.lang.String propertyId, java.lang.Object value)
Sets the value of a property. This method is called by the component manager any time after reset when a property changes value.

Note: Components should silently ignore properties that do not affect the operation of the component.

param
propertyId The property identifier.
param
value The value of the property.
throws
SAXNotRecognizedException The component should not throw this exception.
throws
SAXNotSupportedException The component should not throw this exception.


        //
        // Xerces properties
        //

        if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
            final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length();

            if (suffixLength == Constants.ERROR_HANDLER_PROPERTY.length() && 
                propertyId.endsWith(Constants.ERROR_HANDLER_PROPERTY)) {
                fErrorHandler = (XMLErrorHandler)value;
            }
        }