1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.model.impl.runtime;
9
10 import java.util.StringTokenizer;
11
12 import javax.xml.bind.Element;
13 import javax.xml.bind.ParseConversionEvent;
14 import javax.xml.bind.ValidationEvent;
15 import javax.xml.bind.helpers.ParseConversionEventImpl;
16 import javax.xml.bind.helpers.ValidationEventImpl;
17 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
18
19 import org.xml.sax.Attributes;
20 import org.xml.sax.SAXException;
21
22 import com.sun.xml.bind.JAXBAssertionError;
23 import com.sun.xml.bind.unmarshaller.Messages;
24
25 /***
26 * Convenient default implementation of {@link UnmarshallingEventHandler} to
27 * minimize code generation.
28 *
29 * <p>
30 * For historical reasons, sometimes this type is used where
31 * {@link UnmarshallingEventHandler} should be used.
32 *
33 * Once an exception is in the form of UnmarshalException, we consider it to be
34 * already reported to the client app.
35 */
36 public abstract class AbstractUnmarshallingEventHandlerImpl implements
37 UnmarshallingEventHandler {
38 public AbstractUnmarshallingEventHandlerImpl(UnmarshallingContext _ctxt,
39 String _stateTextTypes) {
40
41 this.context = _ctxt;
42 this.stateTextTypes = _stateTextTypes;
43 }
44
45 public final UnmarshallingContext context;
46
47 /***
48 * Text type of states encoded into a string. 'L' means a list state.
49 */
50 private final String stateTextTypes;
51
52
53
54
55
56
57
58 public void enterElement(String uri, String local, String qname,
59 Attributes atts) throws SAXException {
60 unexpectedEnterElement(uri, local, qname, atts);
61 }
62
63 public void leaveElement(String uri, String local, String qname)
64 throws SAXException {
65 unexpectedLeaveElement(uri, local, qname);
66 }
67
68 public final void text(String text) throws SAXException {
69 if (isListState()) {
70
71
72
73
74 StringTokenizer tokens = new StringTokenizer(text);
75 if (tokens.countTokens() == 1) {
76 handleText(text);
77 } else {
78 while (tokens.hasMoreTokens())
79
80
81 context.getCurrentHandler().text(tokens.nextToken());
82 }
83 } else {
84
85 handleText(text);
86 }
87 }
88
89 protected void handleText(String s) throws SAXException {
90 unexpectedText(s);
91 }
92
93 public void enterAttribute(String uri, String local, String qname)
94 throws SAXException {
95 unexpectedEnterAttribute(uri, local, qname);
96 }
97
98 public void leaveAttribute(String uri, String local, String qname)
99 throws SAXException {
100 unexpectedLeaveAttribute(uri, local, qname);
101 }
102
103 public void leaveChild(int nextState) throws SAXException {
104 this.state = nextState;
105 }
106
107 /***
108 * Checks if the current state is marked as a list state.
109 */
110 protected final boolean isListState() {
111 return stateTextTypes.charAt(state) == 'L';
112 }
113
114 /*** Current state of this automaton. */
115 public int state;
116
117
118
119
120
121
122 /*** Called when a RuntimeException is thrown during unmarshalling a text. */
123 protected void handleUnexpectedTextException(String text, RuntimeException e)
124 throws SAXException {
125
126 reportError(Messages.format(Messages.UNEXPECTED_TEXT, text), e, true);
127 }
128
129 /***
130 * Last resort when something goes terribly wrong within the unmarshaller.
131 */
132 protected void handleGenericException(Exception e) throws SAXException {
133 reportError(e.getMessage(), e, false);
134 }
135
136 protected final void dump() {
137 System.err.println("state is :" + state);
138 }
139
140 private void reportError(String msg, boolean canRecover)
141 throws SAXException {
142 reportError(msg, null, canRecover);
143 }
144
145 private void reportError(String msg, Exception nested, boolean canRecover)
146 throws SAXException {
147 context.handleEvent(new ValidationEventImpl(
148 canRecover ? ValidationEvent.ERROR
149 : ValidationEvent.FATAL_ERROR, msg,
150 new ValidationEventLocatorImpl(context.getLocator()), nested),
151 canRecover);
152 }
153
154 protected final void unexpectedEnterElement(String uri, String local,
155 String qname, Attributes atts) throws SAXException {
156
157 reportError(Messages.format(Messages.UNEXPECTED_ENTER_ELEMENT, uri,
158 local), true);
159
160 context.pushContentHandler(new Discarder(context), state);
161 context.getCurrentHandler().enterElement(uri, local, qname, atts);
162 }
163
164 protected final void unexpectedLeaveElement(String uri, String local,
165 String qname) throws SAXException {
166 reportError(Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri,
167 local), false);
168 }
169
170 protected final void unexpectedEnterAttribute(String uri, String local,
171 String qname) throws SAXException {
172 reportError(Messages.format(Messages.UNEXPECTED_ENTER_ATTRIBUTE, uri,
173 local), false);
174 }
175
176 protected final void unexpectedLeaveAttribute(String uri, String local,
177 String qname) throws SAXException {
178 reportError(Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri,
179 local), false);
180 }
181
182 protected final void unexpectedText(String str) throws SAXException {
183
184 str = str.replace('\r', ' ').replace('\n', ' ').replace('\t', ' ')
185 .trim();
186
187 reportError(Messages.format(Messages.UNEXPECTED_TEXT, str), true);
188 }
189
190 protected final void unexpectedLeaveChild() throws SAXException {
191
192
193
194 dump();
195 throw new JAXBAssertionError(Messages
196 .format(Messages.UNEXPECTED_LEAVE_CHILD));
197 }
198
199 /***
200 * This method is called by the generated derived class when a datatype
201 * parse method throws an exception.
202 */
203 protected void handleParseConversionException(Exception e)
204 throws SAXException {
205 if (e instanceof RuntimeException)
206 throw (RuntimeException) e;
207
208
209
210 ParseConversionEvent pce = new ParseConversionEventImpl(
211 ValidationEvent.ERROR, e.getMessage(),
212 new ValidationEventLocatorImpl(context.getLocator()), e);
213 context.handleEvent(pce, true);
214 }
215
216
217
218
219
220
221 private UnmarshallingEventHandler spawnChild(Class clazz, int memento) {
222
223 UnmarshallableObject child;
224 try {
225 child = (UnmarshallableObject) clazz.newInstance();
226 } catch (InstantiationException e) {
227 throw new InstantiationError(e.getMessage());
228 } catch (IllegalAccessException e) {
229 throw new IllegalAccessError(e.getMessage());
230 }
231
232 UnmarshallingEventHandler handler = child.createUnmarshaller(context);
233 context.pushContentHandler(handler, memento);
234 return handler;
235 }
236
237 protected final Object spawnChildFromEnterElement(Class clazz, int memento,
238 String uri, String local, String qname, Attributes atts)
239 throws SAXException {
240 UnmarshallingEventHandler ueh = spawnChild(clazz, memento);
241 ueh.enterElement(uri, local, qname, atts);
242 return ueh.owner();
243 }
244
245 protected final Object spawnChildFromEnterAttribute(Class clazz,
246 int memento, String uri, String local, String qname)
247 throws SAXException {
248 UnmarshallingEventHandler ueh = spawnChild(clazz, memento);
249 ueh.enterAttribute(uri, local, qname);
250 return ueh.owner();
251 }
252
253 protected final Object spawnChildFromText(Class clazz, int memento,
254 String value) throws SAXException {
255 UnmarshallingEventHandler ueh = spawnChild(clazz, memento);
256 ueh.text(value);
257 return ueh.owner();
258 }
259
260
261 protected final Object spawnChildFromLeaveElement(Class clazz, int memento,
262 String uri, String local, String qname) throws SAXException {
263 UnmarshallingEventHandler ueh = spawnChild(clazz, memento);
264 ueh.leaveElement(uri, local, qname);
265 return ueh.owner();
266 }
267
268 protected final Object spawnChildFromLeaveAttribute(Class clazz,
269 int memento, String uri, String local, String qname)
270 throws SAXException {
271 UnmarshallingEventHandler ueh = spawnChild(clazz, memento);
272 ueh.leaveAttribute(uri, local, qname);
273 return ueh.owner();
274 }
275
276 protected final Element spawnWildcard(int memento, String uri,
277 String local, String qname, Attributes atts) throws SAXException {
278 UnmarshallingEventHandler ueh = context.getGrammarInfo()
279 .createUnmarshaller(uri, local, context);
280
281 if (ueh != null) {
282 context.pushContentHandler(ueh, memento);
283 ueh.enterElement(uri, local, qname, atts);
284 return (Element) ueh.owner();
285 } else {
286
287
288 context.pushContentHandler(new Discarder(context), memento);
289 context.getCurrentHandler().enterElement(uri, local, qname, atts);
290 return null;
291 }
292 }
293
294
295
296
297
298
299
300 protected final void spawnHandlerFromEnterElement(
301 UnmarshallingEventHandler unm, int memento, String uri,
302 String local, String qname, Attributes atts) throws SAXException {
303
304 context.pushContentHandler(unm, memento);
305 unm.enterElement(uri, local, qname, atts);
306 }
307
308 protected final void spawnHandlerFromEnterAttribute(
309 UnmarshallingEventHandler unm, int memento, String uri,
310 String local, String qname) throws SAXException {
311
312 context.pushContentHandler(unm, memento);
313 unm.enterAttribute(uri, local, qname);
314 }
315
316 protected final void spawnHandlerFromFromText(
317 UnmarshallingEventHandler unm, int memento, String value)
318 throws SAXException {
319
320 context.pushContentHandler(unm, memento);
321 unm.text(value);
322 }
323
324 protected final void spawnHandlerFromLeaveElement(
325 UnmarshallingEventHandler unm, int memento, String uri,
326 String local, String qname) throws SAXException {
327
328 context.pushContentHandler(unm, memento);
329 unm.leaveElement(uri, local, qname);
330 }
331
332 protected final void spawnHandlerFromLeaveAttribute(
333 UnmarshallingEventHandler unm, int memento, String uri,
334 String local, String qname) throws SAXException {
335
336 context.pushContentHandler(unm, memento);
337 unm.leaveAttribute(uri, local, qname);
338 }
339
340 protected final void spawnHandlerFromText(UnmarshallingEventHandler unm,
341 int memento, String text) throws SAXException {
342
343 context.pushContentHandler(unm, memento);
344 unm.text(text);
345 }
346
347
348
349
350
351
352 protected final void revertToParentFromEnterElement(String uri,
353 String local, String qname, Attributes atts) throws SAXException {
354 context.popContentHandler();
355 context.getCurrentHandler().enterElement(uri, local, qname, atts);
356 }
357
358 protected final void revertToParentFromLeaveElement(String uri,
359 String local, String qname) throws SAXException {
360 context.popContentHandler();
361 context.getCurrentHandler().leaveElement(uri, local, qname);
362 }
363
364 protected final void revertToParentFromEnterAttribute(String uri,
365 String local, String qname) throws SAXException {
366 context.popContentHandler();
367 context.getCurrentHandler().enterAttribute(uri, local, qname);
368 }
369
370 protected final void revertToParentFromLeaveAttribute(String uri,
371 String local, String qname) throws SAXException {
372 context.popContentHandler();
373 context.getCurrentHandler().leaveAttribute(uri, local, qname);
374 }
375
376 protected final void revertToParentFromText(String value)
377 throws SAXException {
378 context.popContentHandler();
379 context.getCurrentHandler().text(value);
380 }
381 }