KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > AbstractUnmarshallingEventHandlerImpl


1 package com.sun.tools.xjc.runtime;
2
3 import java.util.StringTokenizer JavaDoc;
4
5 import javax.xml.bind.Element;
6 import javax.xml.bind.ParseConversionEvent;
7 import javax.xml.bind.ValidationEvent;
8 import javax.xml.bind.helpers.ParseConversionEventImpl;
9 import javax.xml.bind.helpers.ValidationEventImpl;
10 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
11
12 import org.xml.sax.Attributes JavaDoc;
13 import org.xml.sax.SAXException JavaDoc;
14
15 import com.sun.xml.bind.JAXBAssertionError;
16 import com.sun.xml.bind.unmarshaller.Messages;
17
18 /**
19  * Convenient default implementation of
20  * {@link UnmarshallingEventHandler}
21  * to minimize code generation.
22  *
23  * <p>
24  * For historical reasons, sometimes this type is used where
25  * {@link UnmarshallingEventHandler} should be used.
26  *
27  * Once an exception is in the form of UnmarshalException, we consider
28  * it to be already reported to the client app.
29  */

30 public abstract class AbstractUnmarshallingEventHandlerImpl implements UnmarshallingEventHandler
31 {
32     public AbstractUnmarshallingEventHandlerImpl(UnmarshallingContext _ctxt,
33         String JavaDoc _stateTextTypes ) {
34         
35         this.context = _ctxt;
36         this.stateTextTypes = _stateTextTypes;
37     }
38     public final UnmarshallingContext context;
39     
40     /**
41      * Text type of states encoded into a string.
42      * 'L' means a list state.
43      */

44     private final String JavaDoc stateTextTypes;
45     
46 //
47
//
48
// methods that will be provided by the generated code.
49
//
50
//
51
// internal events
52
public void enterElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts) throws SAXException JavaDoc {
53         unexpectedEnterElement(uri,local,qname,atts);
54     }
55     public void leaveElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
56         unexpectedLeaveElement(uri,local,qname);
57     }
58     public final void text(String JavaDoc text) throws SAXException JavaDoc {
59         if(isListState()) {
60             // in list state, we don't need to care about whitespaces.
61
// if the text is all whitespace, this won't generate a text event,
62
// so it would be just fine.
63

64             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(text);
65             if( tokens.countTokens()==1 ) {
66                 handleText(text);
67             } else {
68                 while(tokens.hasMoreTokens())
69                     // the handler can be switched during the text processing,
70
// so the current handler has to be obtained inside the loop
71
context.getCurrentHandler().text(tokens.nextToken());
72             }
73         } else {
74             // otherwise process this token
75
handleText(text);
76         }
77     }
78     protected void handleText(String JavaDoc s) throws SAXException JavaDoc {
79         unexpectedText(s);
80     }
81     public void enterAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
82         unexpectedEnterAttribute(uri,local,qname);
83     }
84     public void leaveAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
85         unexpectedLeaveAttribute(uri,local,qname);
86     }
87     public void leaveChild(int nextState) throws SAXException JavaDoc {
88         this.state = nextState;
89     }
90     
91     
92     /**
93      * Checks if the current state is marked as a list state.
94      */

95     protected final boolean isListState() {
96         return stateTextTypes.charAt(state)=='L';
97     }
98     
99     
100     /** Current state of this automaton. */
101     public int state;
102     
103     
104     
105     
106 //
107
//
108
// utility methods
109
//
110
//
111
/** Called when a RuntimeException is thrown during unmarshalling a text. */
112     protected void handleUnexpectedTextException( String JavaDoc text, RuntimeException JavaDoc e ) throws SAXException JavaDoc {
113         // report this as an error
114
reportError( Messages.format(Messages.UNEXPECTED_TEXT,text), e, true );
115     }
116     
117     /**
118      * Last resort when something goes terribly wrong within the unmarshaller.
119      */

120     protected void handleGenericException( Exception JavaDoc e ) throws SAXException JavaDoc {
121         reportError( e.getMessage(), e, false );
122     }
123     
124     
125     protected final void dump() {
126         System.err.println("state is :"+state);
127     }
128     private void reportError( String JavaDoc msg, boolean canRecover ) throws SAXException JavaDoc {
129         reportError( msg, null, canRecover );
130     }
131     private void reportError( String JavaDoc msg, Exception JavaDoc nested, boolean canRecover ) throws SAXException JavaDoc {
132         context.handleEvent( new ValidationEventImpl(
133             canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR,
134             msg,
135             new ValidationEventLocatorImpl(context.getLocator()),
136             nested ), canRecover );
137     }
138     protected final void unexpectedEnterElement( String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts ) throws SAXException JavaDoc {
139         // notify the error
140
reportError( Messages.format(Messages.UNEXPECTED_ENTER_ELEMENT, uri, local ), true );
141         // then recover by ignoring the whole element.
142
context.pushContentHandler(new Discarder(context),state);
143         context.getCurrentHandler().enterElement(uri,local,qname,atts);
144     }
145     protected final void unexpectedLeaveElement( String JavaDoc uri, String JavaDoc local, String JavaDoc qname ) throws SAXException JavaDoc {
146         reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri, local ), false );
147     }
148     protected final void unexpectedEnterAttribute( String JavaDoc uri, String JavaDoc local, String JavaDoc qname ) throws SAXException JavaDoc {
149         reportError( Messages.format(Messages.UNEXPECTED_ENTER_ATTRIBUTE, uri, local ), false );
150     }
151     protected final void unexpectedLeaveAttribute( String JavaDoc uri, String JavaDoc local, String JavaDoc qname ) throws SAXException JavaDoc {
152         reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri, local ), false );
153     }
154     protected final void unexpectedText( String JavaDoc str ) throws SAXException JavaDoc {
155         // make str printable
156
str = str.replace('\r',' ').replace('\n',' ').replace('\t',' ').trim();
157         
158         reportError( Messages.format(Messages.UNEXPECTED_TEXT, str ), true );
159     }
160     protected final void unexpectedLeaveChild() throws SAXException JavaDoc {
161         // I believe this is really a bug of the compiler,
162
// since when an object spawns a child object, it must be "prepared"
163
// to receive this event.
164
dump();
165         throw new JAXBAssertionError(
166             Messages.format( Messages.UNEXPECTED_LEAVE_CHILD ) );
167     }
168     /**
169      * This method is called by the generated derived class
170      * when a datatype parse method throws an exception.
171      */

172     protected void handleParseConversionException(Exception JavaDoc e) throws SAXException JavaDoc {
173         if( e instanceof RuntimeException JavaDoc )
174             throw (RuntimeException JavaDoc)e; // don't catch the runtime exception. just let it go.
175

176         // wrap it into a ParseConversionEvent and report it
177
ParseConversionEvent pce = new ParseConversionEventImpl(
178             ValidationEvent.ERROR, e.getMessage(),
179             new ValidationEventLocatorImpl(context.getLocator()), e );
180         context.handleEvent(pce,true);
181     }
182     
183     
184 //
185
//
186
// spawn a new child object
187
//
188
//
189
private UnmarshallingEventHandler spawnChild( Class JavaDoc clazz, int memento ) {
190         
191         UnmarshallableObject child;
192         try {
193             child = (UnmarshallableObject)clazz.newInstance();
194         } catch (InstantiationException JavaDoc e) {
195             throw new InstantiationError JavaDoc(e.getMessage());
196         } catch (IllegalAccessException JavaDoc e) {
197             throw new IllegalAccessError JavaDoc(e.getMessage());
198         }
199         
200         UnmarshallingEventHandler handler = child.createUnmarshaller(context);
201         context.pushContentHandler(handler,memento);
202         return handler;
203     }
204     
205     protected final Object JavaDoc spawnChildFromEnterElement(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts)
206             throws SAXException JavaDoc {
207         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
208         ueh.enterElement(uri,local,qname,atts);
209         return ueh.owner();
210     }
211     
212     protected final Object JavaDoc spawnChildFromEnterAttribute(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
213             throws SAXException JavaDoc {
214         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
215         ueh.enterAttribute(uri,local,qname);
216         return ueh.owner();
217     }
218     
219     protected final Object JavaDoc spawnChildFromText(Class JavaDoc clazz, int memento, String JavaDoc value)
220             throws SAXException JavaDoc {
221         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
222         ueh.text(value);
223         return ueh.owner();
224     }
225
226     // these methods can be used if a child object can be nullable
227
protected final Object JavaDoc spawnChildFromLeaveElement(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
228             throws SAXException JavaDoc {
229         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
230         ueh.leaveElement(uri,local,qname);
231         return ueh.owner();
232     }
233
234     protected final Object JavaDoc spawnChildFromLeaveAttribute(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
235             throws SAXException JavaDoc {
236         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
237         ueh.leaveAttribute(uri,local,qname);
238         return ueh.owner();
239     }
240     
241     protected final Element spawnWildcard( int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts )
242             throws SAXException JavaDoc {
243         UnmarshallingEventHandler ueh = context.getGrammarInfo().createUnmarshaller(uri,local,context);
244         
245         if(ueh!=null) {
246             context.pushContentHandler(ueh,memento);
247             ueh.enterElement(uri,local,qname,atts);
248             return (Element)ueh.owner();
249         } else {
250             // if no class is available to unmarshal this element, discard
251
// the sub-tree by feeding events to discarder.
252
context.pushContentHandler( new Discarder(context), memento );
253             context.getCurrentHandler().enterElement(uri,local,qname,atts);
254             return null; // return null so that the discarder will be ignored
255
}
256     }
257     
258 //
259
//
260
// spawn a new child handler.
261
// used for super class and RELAXNG interleave handling.
262
//
263

264     
265     protected final void spawnHandlerFromEnterElement(
266         UnmarshallingEventHandler unm, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts )
267             throws SAXException JavaDoc {
268         
269         context.pushContentHandler(unm,memento);
270         unm.enterElement(uri,local,qname,atts);
271     }
272     
273     protected final void spawnHandlerFromEnterAttribute(
274         UnmarshallingEventHandler unm, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
275             throws SAXException JavaDoc {
276         
277         context.pushContentHandler(unm,memento);
278         unm.enterAttribute(uri,local,qname);
279     }
280     
281     protected final void spawnHandlerFromFromText(
282         UnmarshallingEventHandler unm, int memento, String JavaDoc value)
283             throws SAXException JavaDoc {
284         
285         context.pushContentHandler(unm,memento);
286         unm.text(value);
287     }
288     
289     protected final void spawnHandlerFromLeaveElement(
290         UnmarshallingEventHandler unm, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
291             throws SAXException JavaDoc {
292         
293         context.pushContentHandler(unm,memento);
294         unm.leaveElement(uri,local,qname);
295     }
296     
297     protected final void spawnHandlerFromLeaveAttribute(
298         UnmarshallingEventHandler unm, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
299             throws SAXException JavaDoc {
300         
301         context.pushContentHandler(unm,memento);
302         unm.leaveAttribute(uri,local,qname);
303     }
304     
305     protected final void spawnHandlerFromText(
306         UnmarshallingEventHandler unm, int memento, String JavaDoc text )
307             throws SAXException JavaDoc {
308         
309         context.pushContentHandler(unm,memento);
310         unm.text(text);
311     }
312     
313     
314 //
315
//
316
// revert to parent
317
//
318
//
319
protected final void revertToParentFromEnterElement(String JavaDoc uri,String JavaDoc local, String JavaDoc qname,Attributes JavaDoc atts)
320             throws SAXException JavaDoc {
321         context.popContentHandler();
322         context.getCurrentHandler().enterElement(uri,local,qname,atts);
323     }
324     protected final void revertToParentFromLeaveElement(String JavaDoc uri,String JavaDoc local, String JavaDoc qname)
325             throws SAXException JavaDoc {
326         context.popContentHandler();
327         context.getCurrentHandler().leaveElement(uri,local,qname);
328     }
329     protected final void revertToParentFromEnterAttribute(String JavaDoc uri,String JavaDoc local, String JavaDoc qname)
330             throws SAXException JavaDoc {
331         context.popContentHandler();
332         context.getCurrentHandler().enterAttribute(uri,local,qname);
333     }
334     protected final void revertToParentFromLeaveAttribute(String JavaDoc uri,String JavaDoc local, String JavaDoc qname)
335             throws SAXException JavaDoc {
336         context.popContentHandler();
337         context.getCurrentHandler().leaveAttribute(uri,local,qname);
338     }
339     protected final void revertToParentFromText(String JavaDoc value)
340             throws SAXException JavaDoc {
341         context.popContentHandler();
342         context.getCurrentHandler().text(value);
343     }
344 }
345
Popular Tags