1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.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
25
26
27
28
29
30
31
32
33
34
35 /***
36 * Validator implementation of JAXB RI.
37 */
38 public class ValidatorImpl implements Validator {
39 /*** Validation errors will be reported to this object. */
40 private ValidationEventHandler eventHandler = new DefaultValidationEventHandler();
41
42 final DefaultJAXBContextImpl jaxbContext;
43
44 public ValidatorImpl(DefaultJAXBContextImpl c) {
45
46 DatatypeConverter
47 .setDatatypeConverter(DatatypeConverterImpl.theInstance);
48
49 jaxbContext = c;
50 }
51
52 /***
53 * We need to know whether an validation error was detected or not. For this
54 * purpose, we set up the validation so that this interceptor will
55 * "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
64 public boolean hadError() {
65 return hadError;
66 }
67
68 /*** event will be passed to this component. */
69 private final ValidationEventHandler next;
70
71 public boolean handleEvent(ValidationEvent e) {
72 hadError = true;
73 boolean result;
74 if (next != null) {
75
76 try {
77 result = next.handleEvent(e);
78 } catch (RuntimeException re) {
79
80
81 result = false;
82 }
83 } else {
84
85
86 result = false;
87 }
88 return result;
89 }
90 };
91
92 public boolean validateRoot(Object o) throws ValidationException {
93 if (o == null) {
94 throw new IllegalArgumentException(Messages.format(
95 Messages.MUST_NOT_BE_NULL, "rootObj"));
96 }
97
98 return validate(o, true);
99 }
100
101 public boolean validate(Object o) throws ValidationException {
102 if (o == null) {
103 throw new IllegalArgumentException(Messages.format(
104 Messages.MUST_NOT_BE_NULL, "subrootObj"));
105 }
106
107 return validate(o, false);
108 }
109
110 private boolean validate(Object o, boolean validateId)
111 throws ValidationException {
112
113 try {
114
115
116 ValidatableObject vo = jaxbContext.getGrammarInfo()
117 .castToValidatableObject(o);
118
119 if (vo == null)
120 throw new ValidationException(Messages
121 .format(Messages.NOT_VALIDATABLE));
122
123 EventInterceptor ei = new EventInterceptor(eventHandler);
124 ValidationContext context = new ValidationContext(jaxbContext, ei,
125 validateId);
126 context.validate(vo);
127 context.reconcileIDs();
128
129 return !ei.hadError();
130 } catch (SAXException e) {
131
132
133 Exception nested = e.getException();
134 if (nested != null) {
135 throw new ValidationException(nested);
136 } else {
137 throw new ValidationException(e);
138 }
139
140 }
141 }
142
143 public ValidationEventHandler getEventHandler() {
144 return eventHandler;
145 }
146
147 public void setEventHandler(ValidationEventHandler handler) {
148 if (handler == null) {
149 eventHandler = new DefaultValidationEventHandler();
150 } else {
151 eventHandler = handler;
152 }
153 }
154
155 /***
156 * There are no required properties, so simply throw an exception. Other
157 * providers may have support for properties on Validator, but the RI
158 * doesn't
159 */
160 public void setProperty(String name, Object value) throws PropertyException {
161
162 if (name == null) {
163 throw new IllegalArgumentException(Messages.format(
164 Messages.MUST_NOT_BE_NULL, "name"));
165 }
166
167 throw new PropertyException(name, value);
168 }
169
170 /***
171 * There are no required properties, so simply throw an exception. Other
172 * providers may have support for properties on Validator, but the RI
173 * doesn't
174 */
175 public Object getProperty(String name) throws PropertyException {
176
177 if (name == null) {
178 throw new IllegalArgumentException(Messages.format(
179 Messages.MUST_NOT_BE_NULL, "name"));
180 }
181
182 throw new PropertyException(name);
183 }
184
185 }