1 /*
2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package javax.xml.bind.util;
26
27 import java.util.Vector;
28 import javax.xml.bind.ValidationEventHandler;
29 import javax.xml.bind.ValidationEvent;
30
31 /**
32 * {@link javax.xml.bind.ValidationEventHandler ValidationEventHandler}
33 * implementation that collects all events.
34 *
35 * <p>
36 * To use this class, create a new instance and pass it to the setEventHandler
37 * method of the Validator, Unmarshaller, Marshaller class. After the call to
38 * validate or unmarshal completes, call the getEvents method to retrieve all
39 * the reported errors and warnings.
40 *
41 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
42 * @see javax.xml.bind.Validator
43 * @see javax.xml.bind.ValidationEventHandler
44 * @see javax.xml.bind.ValidationEvent
45 * @see javax.xml.bind.ValidationEventLocator
46 * @since JAXB1.0
47 */
48 public class ValidationEventCollector implements ValidationEventHandler
49 {
50 private final Vector<ValidationEvent> events = new Vector<ValidationEvent>();
51
52 /**
53 * Return an array of ValidationEvent objects containing a copy of each of
54 * the collected errors and warnings.
55 *
56 * @return
57 * a copy of all the collected errors and warnings or an empty array
58 * if there weren't any
59 */
60 public ValidationEvent[] getEvents() {
61 return events.toArray(new ValidationEvent[events.size()]);
62 }
63
64 /**
65 * Clear all collected errors and warnings.
66 */
67 public void reset() {
68 events.removeAllElements();
69 }
70
71 /**
72 * Returns true if this event collector contains at least one
73 * ValidationEvent.
74 *
75 * @return true if this event collector contains at least one
76 * ValidationEvent, false otherwise
77 */
78 public boolean hasEvents() {
79 return !events.isEmpty();
80 }
81
82 public boolean handleEvent( ValidationEvent event ) {
83 events.add(event);
84
85 boolean retVal = true;
86 switch( event.getSeverity() ) {
87 case ValidationEvent.WARNING:
88 retVal = true; // continue validation
89 break;
90 case ValidationEvent.ERROR:
91 retVal = true; // continue validation
92 break;
93 case ValidationEvent.FATAL_ERROR:
94 retVal = false; // halt validation
95 break;
96 default:
97 _assert( false,
98 Messages.format( Messages.UNRECOGNIZED_SEVERITY,
99 event.getSeverity() ) );
100 break;
101 }
102
103 return retVal;
104 }
105
106 private static void _assert( boolean b, String msg ) {
107 if( !b ) {
108 throw new InternalError( msg );
109 }
110 }
111 }