1 5 package javax.xml.bind.helpers; 6 7 import org.w3c.dom.Node ; 8 9 import javax.xml.bind.ValidationEvent; 10 import javax.xml.bind.ValidationEventHandler; 11 import javax.xml.bind.ValidationEventLocator; 12 import java.net.URL ; 13 14 37 public class DefaultValidationEventHandler implements ValidationEventHandler { 38 39 public boolean handleEvent( ValidationEvent event ) { 40 41 if( event == null ) { 42 throw new IllegalArgumentException (); 43 } 44 45 String severity = null; 47 boolean retVal = false; 48 switch ( event.getSeverity() ) { 49 case ValidationEvent.WARNING: 50 severity = Messages.format( Messages.WARNING ); 51 retVal = true; break; 53 case ValidationEvent.ERROR: 54 severity = Messages.format( Messages.ERROR ); 55 retVal = false; break; 57 case ValidationEvent.FATAL_ERROR: 58 severity = Messages.format( Messages.FATAL_ERROR ); 59 retVal = false; break; 61 default: 62 assert false : 63 Messages.format( Messages.UNRECOGNIZED_SEVERITY, 64 event.getSeverity() ); 65 } 66 67 String location = getLocation( event ); 69 70 System.out.println( 71 Messages.format( Messages.SEVERITY_MESSAGE, 72 severity, 73 event.getMessage(), 74 location ) ); 75 76 return retVal; 78 } 79 80 84 private String getLocation(ValidationEvent event) { 85 StringBuffer msg = new StringBuffer (); 86 87 ValidationEventLocator locator = event.getLocator(); 88 89 if( locator != null ) { 90 91 URL url = locator.getURL(); 92 Object obj = locator.getObject(); 93 Node node = locator.getNode(); 94 int line = locator.getLineNumber(); 95 96 if( url!=null || line!=-1 ) { 97 msg.append( "line " + line ); 98 if( url!=null ) 99 msg.append( " of " + url ); 100 } else if( obj != null ) { 101 msg.append( " obj: " + obj.toString() ); 102 } else if( node != null ) { 103 msg.append( " node: " + node.toString() ); 104 } 105 } else { 106 msg.append( Messages.format( Messages.LOCATION_UNAVAILABLE ) ); 107 } 108 109 return msg.toString(); 110 } 111 } 112 113 | Popular Tags |