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.helpers;
325N/A
325N/Aimport org.w3c.dom.Node;
325N/A
325N/Aimport javax.xml.bind.ValidationEvent;
325N/Aimport javax.xml.bind.ValidationEventHandler;
325N/Aimport javax.xml.bind.ValidationEventLocator;
325N/Aimport java.net.URL;
325N/A
325N/A/**
325N/A * <p>
325N/A * JAXB 1.0 only default validation event handler. This is the default
325N/A * handler for all objects created from a JAXBContext that is managing
325N/A * schema-derived code generated by a JAXB 1.0 binding compiler.
325N/A *
325N/A * <p>
325N/A * This handler causes the unmarshal and validate operations to fail on the first
325N/A * error or fatal error.
325N/A *
325N/A * <p>
325N/A * This handler is not the default handler for JAXB mapped classes following
325N/A * JAXB 2.0 or later versions. Default validation event handling has changed
325N/A * and is specified in {@link javax.xml.bind.Unmarshaller} and
325N/A * {@link javax.xml.bind.Marshaller}.
325N/A *
325N/A * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
325N/A * @see javax.xml.bind.Unmarshaller
325N/A * @see javax.xml.bind.Validator
325N/A * @see javax.xml.bind.ValidationEventHandler
325N/A * @since JAXB1.0
325N/A */
325N/Apublic class DefaultValidationEventHandler implements ValidationEventHandler {
325N/A
325N/A public boolean handleEvent( ValidationEvent event ) {
325N/A
325N/A if( event == null ) {
325N/A throw new IllegalArgumentException();
325N/A }
325N/A
325N/A // calculate the severity prefix and return value
325N/A String severity = null;
325N/A boolean retVal = false;
325N/A switch ( event.getSeverity() ) {
325N/A case ValidationEvent.WARNING:
325N/A severity = Messages.format( Messages.WARNING );
325N/A retVal = true; // continue after warnings
325N/A break;
325N/A case ValidationEvent.ERROR:
325N/A severity = Messages.format( Messages.ERROR );
325N/A retVal = false; // terminate after errors
325N/A break;
325N/A case ValidationEvent.FATAL_ERROR:
325N/A severity = Messages.format( Messages.FATAL_ERROR );
325N/A retVal = false; // terminate after fatal errors
325N/A break;
325N/A default:
325N/A assert false :
325N/A Messages.format( Messages.UNRECOGNIZED_SEVERITY,
325N/A event.getSeverity() );
325N/A }
325N/A
325N/A // calculate the location message
325N/A String location = getLocation( event );
325N/A
325N/A System.out.println(
325N/A Messages.format( Messages.SEVERITY_MESSAGE,
325N/A severity,
325N/A event.getMessage(),
325N/A location ) );
325N/A
325N/A // fail on the first error or fatal error
325N/A return retVal;
325N/A }
325N/A
325N/A /**
325N/A * Calculate a location message for the event
325N/A *
325N/A */
325N/A private String getLocation(ValidationEvent event) {
325N/A StringBuffer msg = new StringBuffer();
325N/A
325N/A ValidationEventLocator locator = event.getLocator();
325N/A
325N/A if( locator != null ) {
325N/A
325N/A URL url = locator.getURL();
325N/A Object obj = locator.getObject();
325N/A Node node = locator.getNode();
325N/A int line = locator.getLineNumber();
325N/A
325N/A if( url!=null || line!=-1 ) {
325N/A msg.append( "line " + line );
325N/A if( url!=null )
325N/A msg.append( " of " + url );
325N/A } else if( obj != null ) {
325N/A msg.append( " obj: " + obj.toString() );
325N/A } else if( node != null ) {
325N/A msg.append( " node: " + node.toString() );
325N/A }
325N/A } else {
325N/A msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) );
325N/A }
325N/A
325N/A return msg.toString();
325N/A }
325N/A}