KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > util > ValidationEventCollector


1 /*
2  * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5 package javax.xml.bind.util;
6
7 import javax.xml.bind.ValidationEvent;
8 import javax.xml.bind.ValidationEventHandler;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12 /**
13  * {@link javax.xml.bind.ValidationEventHandler ValidationEventHandler}
14  * implementation that collects all events.
15  *
16  * <p>
17  * To use this class, create a new instance and pass it to the setEventHandler
18  * method of the Validator, Unmarshaller, Marshaller class. After the call to
19  * validate or unmarshal completes, call the getEvents method to retrieve all
20  * the reported errors and warnings.
21  *
22  * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
23  * @version $Revision$
24  * @see javax.xml.bind.Validator
25  * @see javax.xml.bind.ValidationEventHandler
26  * @see javax.xml.bind.ValidationEvent
27  * @see javax.xml.bind.ValidationEventLocator
28  * @since JAXB1.0
29  */

30 public class ValidationEventCollector implements ValidationEventHandler
31 {
32     private final List JavaDoc<ValidationEvent> events = new ArrayList JavaDoc<ValidationEvent>();
33      
34     /**
35      * Return an array of ValidationEvent objects containing a copy of each of
36      * the collected errors and warnings.
37      *
38      * @return
39      * a copy of all the collected errors and warnings or an empty array
40      * if there weren't any
41      */

42     public ValidationEvent[] getEvents() {
43         return events.toArray(new ValidationEvent[events.size()]);
44     }
45     
46     /**
47      * Clear all collected errors and warnings.
48      */

49     public void reset() {
50         events.clear();
51     }
52     
53     /**
54      * Returns true if this event collector contains at least one
55      * ValidationEvent.
56      *
57      * @return true if this event collector contains at least one
58      * ValidationEvent, false otherwise
59      */

60     public boolean hasEvents() {
61         return !events.isEmpty();
62     }
63     
64     public boolean handleEvent( ValidationEvent event ) {
65         events.add(event);
66
67         boolean retVal = true;
68         switch( event.getSeverity() ) {
69             case ValidationEvent.WARNING:
70                 retVal = true; // continue validation
71
break;
72             case ValidationEvent.ERROR:
73                 retVal = true; // continue validation
74
break;
75             case ValidationEvent.FATAL_ERROR:
76                 retVal = false; // halt validation
77
break;
78             default:
79                 _assert( false,
80                          Messages.format( Messages.UNRECOGNIZED_SEVERITY,
81                                  event.getSeverity() ) );
82                 break;
83         }
84         
85         return retVal;
86     }
87
88     private static void _assert( boolean b, String JavaDoc msg ) {
89         if( !b ) {
90             throw new InternalError JavaDoc( msg );
91         }
92     }
93 }
94
Popular Tags