1
2
3
4
5
6
7
8 package net.sf.jour.rt.view.config.impl.runtime;
9
10 import javax.xml.bind.PropertyException;
11 import javax.xml.bind.ValidationEvent;
12 import javax.xml.bind.ValidationEventHandler;
13 import javax.xml.bind.ValidationException;
14 import javax.xml.bind.Validator;
15 import javax.xml.bind.helpers.DefaultValidationEventHandler;
16
17 import org.xml.sax.SAXException;
18
19 import com.sun.xml.bind.validator.Messages;
20
21
22
23
24
25
26
27
28
29
30
31
32
33 /***
34 * Validator implementation of JAXB RI.
35 */
36 public class ValidatorImpl implements Validator
37 {
38 /*** Validation errors will be reported to this object. */
39 private ValidationEventHandler eventHandler =
40 new DefaultValidationEventHandler();
41
42 final DefaultJAXBContextImpl jaxbContext;
43
44 public ValidatorImpl( DefaultJAXBContextImpl c ) {
45 jaxbContext = c;
46 }
47 /***
48 * We need to know whether an validation error was detected or not.
49 * For this purpose, we set up the validation so that this interceptor
50 * will "intercept" errors before the application receives it.
51 */
52 private static class EventInterceptor implements ValidationEventHandler {
53 EventInterceptor( ValidationEventHandler _next ) {
54 this.next = _next;
55 }
56
57 private boolean hadError = false;
58 public boolean hadError() { return hadError; }
59
60 /*** event will be passed to this component. */
61 private final ValidationEventHandler next;
62
63 public boolean handleEvent( ValidationEvent e ) {
64 hadError = true;
65 boolean result;
66 if( next!=null ) {
67
68 try {
69 result = next.handleEvent(e);
70 } catch( RuntimeException re ) {
71
72
73 result = false;
74 }
75 } else {
76
77
78 result = false;
79 }
80 return result;
81 }
82 };
83
84 public boolean validateRoot( Object o ) throws ValidationException {
85 if( o == null ) {
86 throw new IllegalArgumentException(
87 Messages.format( Messages.MUST_NOT_BE_NULL, "rootObj" ) );
88 }
89
90 return validate(o,true);
91 }
92
93 public boolean validate( Object o ) throws ValidationException {
94 if( o == null ) {
95 throw new IllegalArgumentException(
96 Messages.format( Messages.MUST_NOT_BE_NULL, "subrootObj" ) );
97 }
98
99 return validate(o,false);
100 }
101
102 private boolean validate( Object o, boolean validateId )
103 throws ValidationException {
104
105 try {
106
107
108 ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o);
109
110 if(vo==null)
111 throw new ValidationException(
112 Messages.format( Messages.NOT_VALIDATABLE ) );
113
114 EventInterceptor ei = new EventInterceptor(eventHandler);
115 ValidationContext context = new ValidationContext(jaxbContext,ei,validateId);
116 context.validate(vo);
117 context.reconcileIDs();
118
119 return !ei.hadError();
120 } catch( SAXException e ) {
121
122 Exception nested = e.getException();
123 if( nested != null ) {
124 throw new ValidationException( nested );
125 } else {
126 throw new ValidationException( e );
127 }
128
129 }
130 }
131
132 public ValidationEventHandler getEventHandler() {
133 return eventHandler;
134 }
135
136 public void setEventHandler( ValidationEventHandler handler ) {
137 if( handler == null ) {
138 eventHandler = new DefaultValidationEventHandler();
139 } else {
140 eventHandler = handler;
141 }
142 }
143
144 /***
145 * There are no required properties, so simply throw an exception. Other
146 * providers may have support for properties on Validator, but the RI doesn't
147 */
148 public void setProperty( String name, Object value )
149 throws PropertyException {
150
151 if( name == null ) {
152 throw new IllegalArgumentException(
153 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
154 }
155
156 throw new PropertyException(name, value);
157 }
158
159 /***
160 * There are no required properties, so simply throw an exception. Other
161 * providers may have support for properties on Validator, but the RI doesn't
162 */
163 public Object getProperty( String name )
164 throws PropertyException {
165
166 if( name == null ) {
167 throw new IllegalArgumentException(
168 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
169 }
170
171 throw new PropertyException(name);
172 }
173
174 }