KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > helpers > DefaultValidationEventHandler


1 /*
2  * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5 package javax.xml.bind.helpers;
6
7 import org.w3c.dom.Node JavaDoc;
8
9 import javax.xml.bind.ValidationEvent;
10 import javax.xml.bind.ValidationEventHandler;
11 import javax.xml.bind.ValidationEventLocator;
12 import java.net.URL JavaDoc;
13
14 /**
15  * <p>
16  * JAXB 1.0 only default validation event handler. This is the default
17  * handler for all objects created from a JAXBContext that is managing
18  * schema-derived code generated by a JAXB 1.0 binding compiler.
19  *
20  * <p>
21  * This handler causes the unmarshal and validate operations to fail on the first
22  * error or fatal error.
23  *
24  * <p>
25  * This handler is not the default handler for JAXB mapped classes following
26  * JAXB 2.0 or later versions. Default validation event handling has changed
27  * and is specified in {@link javax.xml.bind.Unmarshaller} and
28  * {@link javax.xml.bind.Marshaller}.
29  *
30  * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
31  * @version $Revision: 1.4 $
32  * @see javax.xml.bind.Unmarshaller
33  * @see javax.xml.bind.Validator
34  * @see javax.xml.bind.ValidationEventHandler
35  * @since JAXB1.0
36  */

37 public class DefaultValidationEventHandler implements ValidationEventHandler {
38     
39     public boolean handleEvent( ValidationEvent event ) {
40         
41         if( event == null ) {
42             throw new IllegalArgumentException JavaDoc();
43         }
44
45         // calculate the severity prefix and return value
46
String JavaDoc severity = null;
47         boolean retVal = false;
48         switch ( event.getSeverity() ) {
49             case ValidationEvent.WARNING:
50                 severity = Messages.format( Messages.WARNING );
51                 retVal = true; // continue after warnings
52
break;
53             case ValidationEvent.ERROR:
54                 severity = Messages.format( Messages.ERROR );
55                 retVal = false; // terminate after errors
56
break;
57             case ValidationEvent.FATAL_ERROR:
58                 severity = Messages.format( Messages.FATAL_ERROR );
59                 retVal = false; // terminate after fatal errors
60
break;
61             default:
62                 assert false :
63                     Messages.format( Messages.UNRECOGNIZED_SEVERITY,
64                             event.getSeverity() );
65         }
66         
67         // calculate the location message
68
String JavaDoc location = getLocation( event );
69         
70         System.out.println(
71             Messages.format( Messages.SEVERITY_MESSAGE,
72                              severity,
73                              event.getMessage(),
74                              location ) );
75         
76         // fail on the first error or fatal error
77
return retVal;
78     }
79
80     /**
81      * Calculate a location message for the event
82      *
83      */

84     private String JavaDoc getLocation(ValidationEvent event) {
85         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
86         
87         ValidationEventLocator locator = event.getLocator();
88         
89         if( locator != null ) {
90             
91             URL JavaDoc url = locator.getURL();
92             Object JavaDoc obj = locator.getObject();
93             Node JavaDoc 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