KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > ValidatorImpl


1 package com.sun.tools.xjc.runtime;
2
3 import javax.xml.bind.DatatypeConverter;
4 import javax.xml.bind.PropertyException;
5 import javax.xml.bind.ValidationEvent;
6 import javax.xml.bind.ValidationEventHandler;
7 import javax.xml.bind.ValidationException;
8 import javax.xml.bind.Validator;
9 import javax.xml.bind.helpers.DefaultValidationEventHandler;
10
11 import org.xml.sax.SAXException JavaDoc;
12
13 import com.sun.xml.bind.DatatypeConverterImpl;
14 import com.sun.xml.bind.validator.Messages;
15
16 /*
17     TODO:
18     reorganize classes into appropriate packages.
19     to reflect the fact that some of the classes in
20     the marshaller package are used for both marshalling
21     and validation.
22
23     In particular, the MarshallingContext interface should be
24     renamed. It is not only for marshalling.
25     (something like "Serializer", maybe).
26 */

27
28 /**
29  * Validator implementation of JAXB RI.
30  */

31 public class ValidatorImpl implements Validator
32 {
33     /** Validation errors will be reported to this object. */
34     private ValidationEventHandler eventHandler =
35         new DefaultValidationEventHandler();
36     
37     final DefaultJAXBContextImpl jaxbContext;
38     
39     public ValidatorImpl( DefaultJAXBContextImpl c ) {
40         // initialize datatype converter with ours
41
DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
42         
43         jaxbContext = c;
44     }
45     /**
46      * We need to know whether an validation error was detected or not.
47      * For this purpose, we set up the validation so that this interceptor
48      * will "intercept" errors before the application receives it.
49      */

50     private static class EventInterceptor implements ValidationEventHandler {
51         EventInterceptor( ValidationEventHandler _next ) {
52             this.next = _next;
53         }
54         
55         private boolean hadError = false;
56         public boolean hadError() { return hadError; }
57         
58         /** event will be passed to this component. */
59         private final ValidationEventHandler next;
60         
61         public boolean handleEvent( ValidationEvent e ) {
62             hadError = true;
63             boolean result;
64             if( next!=null ) {
65                 // pass it to the application
66
try {
67                     result = next.handleEvent(e);
68                 } catch( RuntimeException JavaDoc re ) {
69                     // if the client event handler causes a RuntimeException,
70
// then we have to return false
71
result = false;
72                 }
73             } else {
74                 // if no error handler was specified, there is no point
75
// in continuing the validation.
76
result = false;
77             }
78             return result;
79         }
80     };
81
82     public boolean validateRoot( Object JavaDoc o ) throws ValidationException {
83         if( o == null ) {
84             throw new IllegalArgumentException JavaDoc(
85                 Messages.format( Messages.MUST_NOT_BE_NULL, "rootObj" ) );
86         }
87         
88         return validate(o,true);
89     }
90     
91     public boolean validate( Object JavaDoc o ) throws ValidationException {
92         if( o == null ) {
93             throw new IllegalArgumentException JavaDoc(
94                 Messages.format( Messages.MUST_NOT_BE_NULL, "subrootObj" ) );
95         }
96         
97         return validate(o,false);
98     }
99     
100     private boolean validate( Object JavaDoc o, boolean validateId )
101         throws ValidationException {
102             
103         try {
104         
105             //ValidatableObject vo = Util.toValidatableObject(o);
106
ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o);
107             
108             if(vo==null)
109                 throw new ValidationException(
110                     Messages.format( Messages.NOT_VALIDATABLE ) );
111         
112             EventInterceptor ei = new EventInterceptor(eventHandler);
113             ValidationContext context = new ValidationContext(jaxbContext,ei,validateId);
114             context.validate(vo);
115             context.reconcileIDs();
116             
117             return !ei.hadError();
118         } catch( SAXException JavaDoc e ) {
119             // we need a consistent mechanism to convert SAXException into JAXBException
120
Exception JavaDoc nested = e.getException();
121             if( nested != null ) {
122                 throw new ValidationException( nested );
123             } else {
124                 throw new ValidationException( e );
125             }
126             //return false;
127
}
128     }
129     
130     public ValidationEventHandler getEventHandler() {
131         return eventHandler;
132     }
133     
134     public void setEventHandler( ValidationEventHandler handler ) {
135         if( handler == null ) {
136             eventHandler = new DefaultValidationEventHandler();
137         } else {
138             eventHandler = handler;
139         }
140     }
141     
142     /**
143      * There are no required properties, so simply throw an exception. Other
144      * providers may have support for properties on Validator, but the RI doesn't
145      */

146     public void setProperty( String JavaDoc name, Object JavaDoc value )
147         throws PropertyException {
148         
149         if( name == null ) {
150             throw new IllegalArgumentException JavaDoc(
151                 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
152         }
153
154         throw new PropertyException(name, value);
155     }
156     
157     /**
158      * There are no required properties, so simply throw an exception. Other
159      * providers may have support for properties on Validator, but the RI doesn't
160      */

161     public Object JavaDoc getProperty( String JavaDoc name )
162         throws PropertyException {
163             
164         if( name == null ) {
165             throw new IllegalArgumentException JavaDoc(
166                 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
167         }
168
169         throw new PropertyException(name);
170     }
171
172 }
173
Popular Tags