001 /*
002 * $Id: DefaultValidationEventHandler.java,v 1.8 2003/02/28 21:44:25 ryans Exp $
003 *
004 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
005 *
006 * This software is the proprietary information of Sun Microsystems, Inc.
007 * Use is subject to license terms.
008 *
009 */
010 package javax.xml.bind.helpers;
011
012 import java.net.URL;
013 import javax.xml.bind.ValidationEventHandler;
014 import javax.xml.bind.ValidationEvent;
015 import javax.xml.bind.ValidationEventLocator;
016
017 import org.w3c.dom.Node;
018
019 /**
020 * <p>
021 * This is the default validation event handler that will be used by the
022 * Unmarshaller and Validator if the client application doesn't specify
023 * their own.
024 *
025 * <p>
026 * It will cause the unmarshal and validate operations to fail on the first
027 * error or fatal error.
028 *
029 * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
030 * @version $Revision: 1.8 $
031 * @see javax.xml.bind.Unmarshaller
032 * @see javax.xml.bind.Validator
033 * @see javax.xml.bind.ValidationEventHandler
034 * @since JAXB1.0
035 */
036 public class DefaultValidationEventHandler implements ValidationEventHandler {
037
038 public boolean handleEvent( ValidationEvent event ) {
039
040 if( event == null ) {
041 throw new IllegalArgumentException();
042 }
043
044 // calculate the severity prefix and return value
045 String severity = null;
046 boolean retVal = false;
047 switch ( event.getSeverity() ) {
048 case ValidationEvent.WARNING:
049 severity = Messages.format( Messages.WARNING );
050 retVal = true; // continue after warnings
051 break;
052 case ValidationEvent.ERROR:
053 severity = Messages.format( Messages.ERROR );
054 retVal = false; // terminate after errors
055 break;
056 case ValidationEvent.FATAL_ERROR:
057 severity = Messages.format( Messages.FATAL_ERROR );
058 retVal = false; // terminate after fatal errors
059 break;
060 default:
061 _assert( false,
062 Messages.format( Messages.UNRECOGNIZED_SEVERITY,
063 new Integer( event.getSeverity() ) ) );
064 }
065
066 // calculate the location message
067 String location = getLocation( event );
068
069 System.out.println(
070 Messages.format( Messages.SEVERITY_MESSAGE,
071 severity,
072 event.getMessage(),
073 location ) );
074
075 // fail on the first error or fatal error
076 return retVal;
077 }
078
079 /**
080 * Calculate a location message for the event
081 *
082 */
083 private String getLocation(ValidationEvent event) {
084 StringBuffer msg = new StringBuffer();
085
086 ValidationEventLocator locator = event.getLocator();
087
088 if( locator != null ) {
089
090 URL url = locator.getURL();
091 Object obj = locator.getObject();
092 Node node = locator.getNode();
093
094 if( url != null ) {
095 msg.append( "line " + locator.getLineNumber() );
096 msg.append( " of " + url.toExternalForm() );
097 } else if( obj != null ) {
098 msg.append( " obj: " + obj.toString() );
099 } else if( node != null ) {
100 msg.append( " node: " + node.toString() );
101 }
102 } else {
103 msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) );
104 }
105
106 return msg.toString();
107 }
108
109
110 private static void _assert( boolean b, String msg ) {
111 if( !b ) {
112 throw new InternalError( msg );
113 }
114 }
115 }
116