001    /*
002     * $Id: ValidationEventCollector.java,v 1.9 2002/11/11 18:52:21 ryans Exp $
003     *
004     * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
005     * 
006     * This software is the proprietary information of Sun Microsystems, Inc.  
007     * Use is subject to license terms.
008     * 
009     */
010    package javax.xml.bind.util;
011    
012    import java.util.Vector;
013    import javax.xml.bind.ValidationEventHandler;
014    import javax.xml.bind.ValidationEvent;
015    
016    /**
017     * {@link javax.xml.bind.ValidationEventHandler ValidationEventHandler} 
018     * implementation that collects all events.
019     * 
020     * <p>
021     * To use this class, create a new instance and pass it to the setEventHandler
022     * method of the Validator, Unmarshaller, Marshaller class.  After the call to 
023     * validate or unmarshal completes, call the getEvents method to retrieve all 
024     * the reported errors and warnings.
025     *
026     * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul> 
027     * @version $Revision: 1.9 $
028     * @see javax.xml.bind.Validator
029     * @see javax.xml.bind.ValidationEventHandler
030     * @see javax.xml.bind.ValidationEvent
031     * @see javax.xml.bind.ValidationEventLocator
032     * @since JAXB1.0
033     */
034    public class ValidationEventCollector implements ValidationEventHandler
035    {
036        private final Vector events = new Vector();
037         
038        /**
039         * Return an array of ValidationEvent objects containing a copy of each of 
040         * the collected errors and warnings.
041         * 
042         * @return
043         *      a copy of all the collected errors and warnings or an empty array
044         *      if there weren't any
045         */
046        public ValidationEvent[] getEvents() {
047            return (ValidationEvent[])events.toArray(new ValidationEvent[events.size()]);
048        }
049        
050        /**
051         * Clear all collected errors and warnings.
052         */
053        public void reset() {
054            events.removeAllElements();
055        }
056        
057        /**
058         * Returns true if this event collector contains at least one 
059         * ValidationEvent.
060         *
061         * @return true if this event collector contains at least one 
062         *         ValidationEvent, false otherwise
063         */
064        public boolean hasEvents() {
065            return events.size() != 0 ? true : false;
066        }
067        
068        public boolean handleEvent( ValidationEvent event ) {        
069            events.add(event);
070    
071            boolean retVal = true;
072            switch( event.getSeverity() ) {
073                case ValidationEvent.WARNING:
074                    retVal = true; // continue validation
075                    break;
076                case ValidationEvent.ERROR:
077                    retVal = true; // continue validation
078                    break;
079                case ValidationEvent.FATAL_ERROR:
080                    retVal = false; // halt validation
081                    break;
082                default:
083                    _assert( false, 
084                             Messages.format( Messages.UNRECOGNIZED_SEVERITY,
085                                              new Integer( event.getSeverity() ) ) );
086                    break;
087            }
088            
089            return retVal;
090        }
091    
092        private static void _assert( boolean b, String msg ) {
093            if( !b ) {
094                throw new InternalError( msg );
095            }
096        }
097    }