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 javax.xml.bind.ValidationEvent; 28 import javax.xml.bind.ValidationEventHandler; 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * {@link javax.xml.bind.ValidationEventHandler ValidationEventHandler} 34 * implementation that collects all events. 35 * 36 * <p> 37 * To use this class, create a new instance and pass it to the setEventHandler 38 * method of the Validator, Unmarshaller, Marshaller class. After the call to 39 * validate or unmarshal completes, call the getEvents method to retrieve all 40 * the reported errors and warnings. 41 * 42 * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul> 43 * @version $Revision: 1.3 $ 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>(); 53 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 } 65 66 /** 67 * Clear all collected errors and warnings. 68 */ 69 public void reset() { 70 events.clear(); 71 } 72 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 } 83 84 public boolean handleEvent( ValidationEvent event ) { 85 events.add(event); 86 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 } 104 105 return retVal; 106 } 107 108 private static void _assert( boolean b, String msg ) { 109 if( !b ) { 110 throw new InternalError( msg ); 111 } 112 } 113 }