Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package javax.xml.bind.util;
28 import javax.xml.bind.ValidationEvent;
29 import javax.xml.bind.ValidationEventHandler;
30 import java.util.ArrayList;
31 import java.util.List;
33 /**
34 * {@link javax.xml.bind.ValidationEventHandler ValidationEventHandler}
35 * implementation that collects all events.
36 *
37 * <p>
38 * To use this class, create a new instance and pass it to the setEventHandler
39 * method of the Validator, Unmarshaller, Marshaller class. After the call to
40 * validate or unmarshal completes, call the getEvents method to retrieve all
41 * the reported errors and warnings.
42 *
43 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
44 * @see javax.xml.bind.Validator
45 * @see javax.xml.bind.ValidationEventHandler
46 * @see javax.xml.bind.ValidationEvent
47 * @see javax.xml.bind.ValidationEventLocator
48 * @since JAXB1.0
49 */
50 public class ValidationEventCollector implements ValidationEventHandler
51 {
52 private final List<ValidationEvent> events = new ArrayList<ValidationEvent>();
54 /**
55 * Return an array of ValidationEvent objects containing a copy of each of
56 * the collected errors and warnings.
57 *
58 * @return
59 * a copy of all the collected errors and warnings or an empty array
60 * if there weren't any
61 */
62 public ValidationEvent[] getEvents() {
63 return events.toArray(new ValidationEvent[events.size()]);
64 }
66 /**
67 * Clear all collected errors and warnings.
68 */
69 public void reset() {
70 events.clear();
71 }
73 /**
74 * Returns true if this event collector contains at least one
75 * ValidationEvent.
76 *
77 * @return true if this event collector contains at least one
78 * ValidationEvent, false otherwise
79 */
80 public boolean hasEvents() {
81 return !events.isEmpty();
82 }
84 public boolean handleEvent( ValidationEvent event ) {
85 events.add(event);
87 boolean retVal = true;
88 switch( event.getSeverity() ) {
89 case ValidationEvent.WARNING:
90 retVal = true; // continue validation
91 break;
92 case ValidationEvent.ERROR:
93 retVal = true; // continue validation
94 break;
95 case ValidationEvent.FATAL_ERROR:
96 retVal = false; // halt validation
97 break;
98 default:
99 _assert( false,
100 Messages.format( Messages.UNRECOGNIZED_SEVERITY,
101 event.getSeverity() ) );
102 break;
103 }
105 return retVal;
106 }
108 private static void _assert( boolean b, String msg ) {
109 if( !b ) {
110 throw new InternalError( msg );
111 }
112 }
113 }