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