You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by an...@locus.apache.org on 2000/12/20 07:36:31 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/util DefaultErrorHandler.java

andyc       00/12/19 22:36:30

  Modified:    java/src/org/apache/xerces/impl Tag: xerces_j_2
                        XMLErrorReporter.java
  Added:       java/src/org/apache/xerces/util Tag: xerces_j_2
                        DefaultErrorHandler.java
  Log:
  Changed the parser to use a default error handler to print error
  messages to the console if no error handler is registered. This
  is a change in behavior from previous versions. This change now
  requires a no-op error handler to be installed in order to "quiet"
  the new default behavior.
  
  This should solve once and for all the numerous reports of "bugs"
  that the validator is not working because there was no output.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.6   +222 -85   xml-xerces/java/src/org/apache/xerces/impl/Attic/XMLErrorReporter.java
  
  Index: XMLErrorReporter.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/impl/Attic/XMLErrorReporter.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- XMLErrorReporter.java	2000/10/08 01:36:40	1.1.2.5
  +++ XMLErrorReporter.java	2000/12/20 06:36:29	1.1.2.6
  @@ -59,9 +59,12 @@
   
   import java.util.Hashtable;
   import java.util.Locale;
  +
  +import org.apache.xerces.util.DefaultErrorHandler;
   import org.apache.xerces.util.MessageFormatter;
   import org.apache.xerces.xni.XMLComponent;
   import org.apache.xerces.xni.XMLComponentManager;
  +
   import org.xml.sax.ErrorHandler;
   import org.xml.sax.Locator;
   import org.xml.sax.SAXException;
  @@ -70,9 +73,41 @@
   import org.xml.sax.SAXNotSupportedException;
   
   /**
  + * 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:
  + * <pre>
  + *   http://apache.org/xml/properties/internal/error-reporter
  + * </pre>
  + * <p>
  + * Errors are separated into domains that categorize a class of errors.
  + * In a parser configuration, the parser would register a
  + * <code>MessageFormatter</code> 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.
  + * <p>
  + * This component requires the following features and properties from the
  + * component manager that uses it:
  + * <ul>
  + *  <li>http://apache.org/xml/properties/internal/error-handler</li>
  + * </ul>
  + * <p>
  + * This component can use the following features and properties but they
  + * are not required:
  + * <ul>
  + *  <li>http://apache.org/xml/features/continue-after-fatal-error</li>
  + * </ul>
  + *
  + * @see MessageFormatter
  + *
    * @author Stubs generated by DesignDoc on Mon Sep 11 11:10:57 PDT 2000
    * @author Eric Ye, IBM
  - * @version $Id: XMLErrorReporter.java,v 1.1.2.5 2000/10/08 01:36:40 andyc Exp $
  + * @author Andy Clark, IBM
  + *
  + * @version $Id: XMLErrorReporter.java,v 1.1.2.6 2000/12/20 06:36:29 andyc Exp $
    */
   public class XMLErrorReporter
       implements XMLComponent {
  @@ -81,162 +116,250 @@
       // Constants
       //
   
  -    /** 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_WARNING = 0;
   
  -    /** 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_ERROR = 1;
   
  -    /** 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.
  +     * <p>
  +     * <strong>Note:</strong> The parser does have a "continue after fatal
  +     * error" feature but it should be used with extreme caution and care.
  +     */
       public static final short SEVERITY_FATAL_ERROR = 2;
       
       //
       // Data
       //
   
  -    /** fLocale */
  +    /** The locale to be used to format error messages. */
       protected Locale fLocale;
   
  -    /** fMessageFormatters */
  +    /** Mapping of Message formatters for domains. */
       protected Hashtable fMessageFormatters;
   
  -    /** fErrorHandler */
  +    /** Error handler. */
       protected ErrorHandler fErrorHandler;
   
  -    /** fLocator */
  +    /** Document locator. */
       protected Locator fLocator;
   
  -    /** fContinueAfterFatalError */
  +    /** Continue after fatal error feature. */
       protected boolean fContinueAfterFatalError;
   
  +    /** 
  +     * 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.
  +     */
  +    protected ErrorHandler fDefaultErrorHandler;
  +
       //
       // Constructors
       //
   
  -    /**
  -     * 
  -     */
  +    /** Constructs an error reporter with a locator. */
       public XMLErrorReporter(Locator locator) {
  +
  +        // 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
  +
           fMessageFormatters = new Hashtable();
           fLocator = locator;
  -    }
  +
  +    } // <init>(Locator)
   
       //
       // Methods
       //
   
       /**
  -     * setLocale
  +     * Sets the current locale.
        * 
  -     * @param locale 
  +     * @param locale The new locale.
        */
       public void setLocale(Locale locale) {
           fLocale = locale;
  -    } // setLocale
  +    } // setLocale(Locale)
   
       /**
  -     * putMessageFormatter
  +     * Registers a message formatter for the specified domain.
  +     * <p>
  +     * <strong>Note:</strong> 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 
        */
  -    public void putMessageFormatter(String domain, MessageFormatter messageFormatter) {
  +    public void putMessageFormatter(String domain, 
  +                                    MessageFormatter messageFormatter) {
           fMessageFormatters.put(domain, messageFormatter);
  -    } // putMessageFormatter
  +    } // putMessageFormatter(String,MessageFormatter)
   
       /**
  -     * getMessageFormatter
  -     * 
  -     * @param domain 
  +     * Returns the message formatter associated with the specified domain,
  +     * or null if no message formatter is registered for that domain.
        * 
  -     * @return 
  +     * @param domain The domain of the message formatter.
        */
       public MessageFormatter getMessageFormatter(String domain) {
  -        return (MessageFormatter) fMessageFormatters.get(domain);
  -    } // getMessageFormatter
  +        return (MessageFormatter)fMessageFormatters.get(domain);
  +    } // getMessageFormatter(String):MessageFormatter
   
       /**
  -     * removeMessageFormatter
  +     * Removes the message formatter for the specified domain and
  +     * returns the removed message formatter.
        * 
  -     * @param domain 
  -     * 
  -     * @return 
  +     * @param domain The domain of the message formatter.
        */
       public MessageFormatter removeMessageFormatter(String domain) {
           return (MessageFormatter) fMessageFormatters.remove(domain);
  -    } // removeMessageFormatter
  +    } // removeMessageFormatter(String):MessageFormatter
   
       /**
  -     * reportError
  -     * 
  -     * @param domain 
  -     * @param key 
  -     * @param arguments 
  -     * @param 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
        */
  -    public void reportError(String domain, String key, Object[] arguments, short severity)
  -        throws SAXException {
  +    public void reportError(String domain, String key, Object[] arguments, 
  +                            short severity) throws SAXException {
   
  -        SAXParseException spe;
  -
  -        MessageFormatter msgFormatter = (MessageFormatter) fMessageFormatters.get(domain);
  -
  -        spe = new SAXParseException(msgFormatter.formatMessage(fLocale, key, arguments), fLocator);
  -
  -        // default error handling   
  -        if (fErrorHandler == null) {
  -            if ( severity == SEVERITY_FATAL_ERROR 
  -                 && !fContinueAfterFatalError) {
  -                throw spe;
  +        // REVISIT: [Q] Should we do anything about invalid severity
  +        //              parameter? -Ac
  +        
  +        // format error message and create parse exception
  +        MessageFormatter messageFormatter = getMessageFormatter(domain);
  +        String message = messageFormatter.formatMessage(fLocale, key, arguments);
  +        SAXParseException spe = new SAXParseException(message, fLocator);
  +
  +        // get error handler
  +        ErrorHandler errorHandler = fErrorHandler;
  +        if (errorHandler == null) {
  +            if (fDefaultErrorHandler == null) {
  +                fDefaultErrorHandler = new DefaultErrorHandler();
               }
  -            return;
  +            errorHandler = fDefaultErrorHandler;
           }
   
  -        // call ErrorHandler callbacks
  -        if (severity == SEVERITY_WARNING ) {
  -            fErrorHandler.warning(spe);
  -        }
  -        else if (severity == SEVERITY_FATAL_ERROR) {
  -            fErrorHandler.fatalError(spe);
  -            if (!fContinueAfterFatalError) {
  -                //
  -                // !! in Xerces 1, spe was wrapped again.
  -                //
  -                throw spe;
  +        // call error handler
  +        switch (severity) {
  +            case SEVERITY_WARNING: {
  +                errorHandler.warning(spe);
  +                break;
               }
  -        }
  -        else {
  -            fErrorHandler.error(spe);
  +            case SEVERITY_ERROR: {
  +                errorHandler.error(spe);
  +                break;
  +            }
  +            case SEVERITY_FATAL_ERROR: {
  +                errorHandler.fatalError(spe);
  +                if (!fContinueAfterFatalError) {
  +                    // NOTE: In Xerces 1.x, SAX parse exception was wrapped
  +                    //       again.
  +                    throw spe;
  +                }
  +                break;
  +            }
           }
   
  -    } // reportError
  +    } // reportError(String,String,Object,short)
   
       //
       // XMLComponent methods
       //
   
       /**
  -     * reset
  -     * 
  -     * @param configurationManager 
  +     * 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.
        */
       public void reset(XMLComponentManager componentManager)
           throws SAXException {
  -        final String ERROR_HANDLER = Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
  +
  +        // features
  +        String CONTINUE_AFTER_FATAL_ERROR = Constants.XERCES_FEATURE_PREFIX + 
  +                                            Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
  +        try {
  +            fContinueAfterFatalError = componentManager.getFeature(CONTINUE_AFTER_FATAL_ERROR);
  +        }
  +        catch (SAXException e) {
  +            fContinueAfterFatalError = false;
  +        }
  +
  +        // properties
  +        String ERROR_HANDLER = Constants.XERCES_PROPERTY_PREFIX + 
  +                               Constants.ERROR_HANDLER_PROPERTY;
           fErrorHandler = (ErrorHandler)componentManager.getProperty(ERROR_HANDLER);
  -    } // reset
  +
  +    } // reset(XMLComponentManager)
   
       /**
  -     * setFeature
  -     * 
  -     * @param featureId 
  -     * @param state 
  +     * Sets the state of a feature. This method is called by the component
  +     * manager any time after reset when a feature changes state. 
  +     * <p>
  +     * <strong>Note:</strong> 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.
        */
       public void setFeature(String featureId, boolean state)
           throws SAXNotRecognizedException, SAXNotSupportedException {
  +
  +        //
  +        // Xerces features
  +        //
  +
           if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) {
  -            String feature =
  -                featureId.substring(Constants.XERCES_FEATURE_PREFIX.length());
  +            String feature = featureId.substring(Constants.XERCES_FEATURE_PREFIX.length());
               //
               // http://apache.org/xml/features/continue-after-fatal-error
               //   Allows the parser to continue after a fatal error.
  @@ -246,25 +369,39 @@
                   fContinueAfterFatalError = state;
               }
           }
  -    } // setFeature
  +
  +    } // setFeature(String,boolean)
   
       /**
  -     * setProperty
  -     * 
  -     * @param propertyId 
  -     * @param value 
  +     * Sets the value of a property. This method is called by the component
  +     * manager any time after reset when a property changes value. 
  +     * <p>
  +     * <strong>Note:</strong> 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.
        */
       public void setProperty(String propertyId, Object value)
           throws SAXNotRecognizedException, SAXNotSupportedException {
   
  +        //
  +        // Xerces properties
  +        //
  +
           if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
  -            String property =
  -                propertyId.substring(Constants.XERCES_PROPERTY_PREFIX.length());
  +            String property = propertyId.substring(Constants.XERCES_PROPERTY_PREFIX.length());
   
               if (property.equals(Constants.ERROR_HANDLER_PROPERTY)) {
  -                fErrorHandler = (ErrorHandler) value;
  +                fErrorHandler = (ErrorHandler)value;
               }
           }
  -    } // setProperty
   
  +    } // setProperty(String,Object)
  +    
   } // class XMLErrorReporter
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.1   +151 -0    xml-xerces/java/src/org/apache/xerces/util/Attic/DefaultErrorHandler.java