325N/A/*
325N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage javax.xml.bind.util;
325N/A
325N/Aimport javax.xml.bind.ValidationEvent;
325N/Aimport javax.xml.bind.ValidationEventHandler;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.List;
325N/A
325N/A/**
325N/A * {@link javax.xml.bind.ValidationEventHandler ValidationEventHandler}
325N/A * implementation that collects all events.
325N/A *
325N/A * <p>
325N/A * To use this class, create a new instance and pass it to the setEventHandler
325N/A * method of the Validator, Unmarshaller, Marshaller class. After the call to
325N/A * validate or unmarshal completes, call the getEvents method to retrieve all
325N/A * the reported errors and warnings.
325N/A *
325N/A * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li><li>Ryan Shoemaker, Sun Microsystems, Inc.</li><li>Joe Fialli, Sun Microsystems, Inc.</li></ul>
325N/A * @see javax.xml.bind.Validator
325N/A * @see javax.xml.bind.ValidationEventHandler
325N/A * @see javax.xml.bind.ValidationEvent
325N/A * @see javax.xml.bind.ValidationEventLocator
325N/A * @since JAXB1.0
325N/A */
325N/Apublic class ValidationEventCollector implements ValidationEventHandler
325N/A{
325N/A private final List<ValidationEvent> events = new ArrayList<ValidationEvent>();
325N/A
325N/A /**
325N/A * Return an array of ValidationEvent objects containing a copy of each of
325N/A * the collected errors and warnings.
325N/A *
325N/A * @return
325N/A * a copy of all the collected errors and warnings or an empty array
325N/A * if there weren't any
325N/A */
325N/A public ValidationEvent[] getEvents() {
325N/A return events.toArray(new ValidationEvent[events.size()]);
325N/A }
325N/A
325N/A /**
325N/A * Clear all collected errors and warnings.
325N/A */
325N/A public void reset() {
325N/A events.clear();
325N/A }
325N/A
325N/A /**
325N/A * Returns true if this event collector contains at least one
325N/A * ValidationEvent.
325N/A *
325N/A * @return true if this event collector contains at least one
325N/A * ValidationEvent, false otherwise
325N/A */
325N/A public boolean hasEvents() {
325N/A return !events.isEmpty();
325N/A }
325N/A
325N/A public boolean handleEvent( ValidationEvent event ) {
325N/A events.add(event);
325N/A
325N/A boolean retVal = true;
325N/A switch( event.getSeverity() ) {
325N/A case ValidationEvent.WARNING:
325N/A retVal = true; // continue validation
325N/A break;
325N/A case ValidationEvent.ERROR:
325N/A retVal = true; // continue validation
325N/A break;
325N/A case ValidationEvent.FATAL_ERROR:
325N/A retVal = false; // halt validation
325N/A break;
325N/A default:
325N/A _assert( false,
325N/A Messages.format( Messages.UNRECOGNIZED_SEVERITY,
325N/A event.getSeverity() ) );
325N/A break;
325N/A }
325N/A
325N/A return retVal;
325N/A }
325N/A
325N/A private static void _assert( boolean b, String msg ) {
325N/A if( !b ) {
325N/A throw new InternalError( msg );
325N/A }
325N/A }
325N/A}