View Javadoc

1   //
2   // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.6-01/24/2006 06:15 PM(kohsuke)-fcs 
3   // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
4   // Any modifications to this file will be lost upon recompilation of the source schema. 
5   // Generated on: 2012.10.03 at 04:27:47 AM CEST 
6   //
7   
8   package org.jdtaus.mojo.resource.model.impl.runtime;
9   
10  import javax.xml.bind.DatatypeConverter;
11  import javax.xml.bind.PropertyException;
12  import javax.xml.bind.ValidationEvent;
13  import javax.xml.bind.ValidationEventHandler;
14  import javax.xml.bind.ValidationException;
15  import javax.xml.bind.Validator;
16  import javax.xml.bind.helpers.DefaultValidationEventHandler;
17  
18  import org.xml.sax.SAXException;
19  
20  import com.sun.xml.bind.DatatypeConverterImpl;
21  import com.sun.xml.bind.validator.Messages;
22  
23  /*
24      TODO:
25      reorganize classes into appropriate packages.
26      to reflect the fact that some of the classes in
27      the marshaller package are used for both marshalling
28      and validation.
29  
30      In particular, the MarshallingContext interface should be
31      renamed. It is not only for marshalling. 
32      (something like "Serializer", maybe).
33  */
34  
35  /**
36   * Validator implementation of JAXB RI.
37   */
38  public class ValidatorImpl implements Validator
39  {
40      /** Validation errors will be reported to this object. */
41      private ValidationEventHandler eventHandler = 
42          new DefaultValidationEventHandler();
43      
44      final DefaultJAXBContextImpl jaxbContext;
45      
46      public ValidatorImpl( DefaultJAXBContextImpl c ) {
47          // initialize datatype converter with ours
48          DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
49          
50          jaxbContext = c;
51      }
52      /**
53       * We need to know whether an validation error was detected or not.
54       * For this purpose, we set up the validation so that this interceptor
55       * will "intercept" errors before the application receives it.
56       */
57      private static class EventInterceptor implements ValidationEventHandler {
58          EventInterceptor( ValidationEventHandler _next ) {
59              this.next = _next;
60          }
61          
62          private boolean hadError = false;
63          public boolean hadError() { return hadError; }
64          
65          /** event will be passed to this component. */
66          private final ValidationEventHandler next;
67          
68          public boolean handleEvent( ValidationEvent e ) {
69              hadError = true;
70              boolean result;
71              if( next!=null ) {
72                  // pass it to the application
73                  try {
74                      result = next.handleEvent(e);
75                  } catch( RuntimeException re ) {
76                      // if the client event handler causes a RuntimeException,
77                      // then we have to return false
78                      result = false;
79                  }
80              } else {
81                  // if no error handler was specified, there is no point
82                  // in continuing the validation.
83                  result = false;
84              }
85              return result;
86          }
87      };
88  
89      public boolean validateRoot( Object o ) throws ValidationException {
90          if( o == null ) {
91              throw new IllegalArgumentException( 
92                  Messages.format( Messages.MUST_NOT_BE_NULL, "rootObj" ) );
93          }
94          
95          return validate(o,true);
96      }
97      
98      public boolean validate( Object o ) throws ValidationException {
99          if( o == null ) {
100             throw new IllegalArgumentException( 
101                 Messages.format( Messages.MUST_NOT_BE_NULL, "subrootObj" ) );
102         }
103         
104         return validate(o,false);
105     }
106     
107     private boolean validate( Object o, boolean validateId ) 
108         throws ValidationException { 
109             
110         try {
111         
112             //ValidatableObject vo = Util.toValidatableObject(o);
113             ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o);
114             
115             if(vo==null)
116                 throw new ValidationException(
117                     Messages.format( Messages.NOT_VALIDATABLE ) );
118         
119             EventInterceptor ei = new EventInterceptor(eventHandler);
120             ValidationContext context = new ValidationContext(jaxbContext,ei,validateId);
121             context.validate(vo);
122             context.reconcileIDs();
123             
124             return !ei.hadError();
125         } catch( SAXException e ) {
126             // we need a consistent mechanism to convert SAXException into JAXBException
127             Exception nested = e.getException();
128             if( nested != null ) {
129                 throw new ValidationException( nested );
130             } else {
131                 throw new ValidationException( e );
132             }
133             //return false;
134         }
135     }
136     
137     public ValidationEventHandler getEventHandler() {
138         return eventHandler;
139     }
140     
141     public void setEventHandler( ValidationEventHandler handler ) {
142         if( handler == null ) {
143             eventHandler = new DefaultValidationEventHandler();
144         } else {
145             eventHandler = handler;
146         }
147     }
148     
149     /**
150      * There are no required properties, so simply throw an exception.  Other
151      * providers may have support for properties on Validator, but the RI doesn't
152      */
153     public void setProperty( String name, Object value )
154         throws PropertyException {
155         
156         if( name == null ) {
157             throw new IllegalArgumentException(
158                 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
159         }
160 
161         throw new PropertyException(name, value);
162     }
163     
164     /**
165      * There are no required properties, so simply throw an exception.  Other
166      * providers may have support for properties on Validator, but the RI doesn't
167      */
168     public Object getProperty( String name )
169         throws PropertyException {
170             
171         if( name == null ) {
172             throw new IllegalArgumentException(
173                 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
174         }
175 
176         throw new PropertyException(name);
177     }
178 
179 }