KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > testsuite > impl > runtime > AbstractUnmarshallingEventHandlerImpl


1 //
2
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.3-b18-fcs
3
// See <a HREF="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
4
// Any modifications to this file will be lost upon recompilation of the source schema.
5
// Generated on: 2005.06.14 at 12:02:17 PDT
6
//
7

8 package com.sun.japex.testsuite.impl.runtime;
9
10 import java.util.StringTokenizer JavaDoc;
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 JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc _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 JavaDoc stateTextTypes;
53     
54 //
55
//
56
// methods that will be provided by the generated code.
57
//
58
//
59
// internal events
60
public void enterElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts) throws SAXException JavaDoc {
61         unexpectedEnterElement(uri,local,qname,atts);
62     }
63     public void leaveElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
64         unexpectedLeaveElement(uri,local,qname);
65     }
66     public final void text(String JavaDoc text) throws SAXException JavaDoc {
67         if(isListState()) {
68             // in list state, we don't need to care about whitespaces.
69
// if the text is all whitespace, this won't generate a text event,
70
// so it would be just fine.
71

72             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(text);
73             if( tokens.countTokens()==1 ) {
74                 handleText(text);
75             } else {
76                 while(tokens.hasMoreTokens())
77                     // the handler can be switched during the text processing,
78
// so the current handler has to be obtained inside the loop
79
context.getCurrentHandler().text(tokens.nextToken());
80             }
81         } else {
82             // otherwise process this token
83
handleText(text);
84         }
85     }
86     protected void handleText(String JavaDoc s) throws SAXException JavaDoc {
87         unexpectedText(s);
88     }
89     public void enterAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
90         unexpectedEnterAttribute(uri,local,qname);
91     }
92     public void leaveAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
93         unexpectedLeaveAttribute(uri,local,qname);
94     }
95     public void leaveChild(int nextState) throws SAXException JavaDoc {
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
// utility methods
117
//
118
//
119
/** Called when a RuntimeException is thrown during unmarshalling a text. */
120     protected void handleUnexpectedTextException( String JavaDoc text, RuntimeException JavaDoc e ) throws SAXException JavaDoc {
121         // report this as an error
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 JavaDoc e ) throws SAXException JavaDoc {
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 JavaDoc msg, boolean canRecover ) throws SAXException JavaDoc {
137         reportError( msg, null, canRecover );
138     }
139     private void reportError( String JavaDoc msg, Exception JavaDoc nested, boolean canRecover ) throws SAXException JavaDoc {
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 JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts ) throws SAXException JavaDoc {
147         // notify the error
148
reportError( Messages.format(Messages.UNEXPECTED_ENTER_ELEMENT, uri, local ), true );
149         // then recover by ignoring the whole element.
150
context.pushContentHandler(new Discarder(context),state);
151         context.getCurrentHandler().enterElement(uri,local,qname,atts);
152     }
153     protected final void unexpectedLeaveElement( String JavaDoc uri, String JavaDoc local, String JavaDoc qname ) throws SAXException JavaDoc {
154         reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri, local ), false );
155     }
156     protected final void unexpectedEnterAttribute( String JavaDoc uri, String JavaDoc local, String JavaDoc qname ) throws SAXException JavaDoc {
157         reportError( Messages.format(Messages.UNEXPECTED_ENTER_ATTRIBUTE, uri, local ), false );
158     }
159     protected final void unexpectedLeaveAttribute( String JavaDoc uri, String JavaDoc local, String JavaDoc qname ) throws SAXException JavaDoc {
160         reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri, local ), false );
161     }
162     protected final void unexpectedText( String JavaDoc str ) throws SAXException JavaDoc {
163         // make str printable
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 JavaDoc {
169         // I believe this is really a bug of the compiler,
170
// since when an object spawns a child object, it must be "prepared"
171
// to receive this event.
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 JavaDoc e) throws SAXException JavaDoc {
181         if( e instanceof RuntimeException JavaDoc )
182             throw (RuntimeException JavaDoc)e; // don't catch the runtime exception. just let it go.
183

184         // wrap it into a ParseConversionEvent and report it
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
// spawn a new child object
195
//
196
//
197
private UnmarshallingEventHandler spawnChild( Class JavaDoc clazz, int memento ) {
198         
199         UnmarshallableObject child;
200         try {
201             child = (UnmarshallableObject)clazz.newInstance();
202         } catch (InstantiationException JavaDoc e) {
203             throw new InstantiationError JavaDoc(e.getMessage());
204         } catch (IllegalAccessException JavaDoc e) {
205             throw new IllegalAccessError JavaDoc(e.getMessage());
206         }
207         
208         UnmarshallingEventHandler handler = child.createUnmarshaller(context);
209         context.pushContentHandler(handler,memento);
210         return handler;
211     }
212     
213     protected final Object JavaDoc spawnChildFromEnterElement(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts)
214             throws SAXException JavaDoc {
215         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
216         ueh.enterElement(uri,local,qname,atts);
217         return ueh.owner();
218     }
219     
220     protected final Object JavaDoc spawnChildFromEnterAttribute(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
221             throws SAXException JavaDoc {
222         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
223         ueh.enterAttribute(uri,local,qname);
224         return ueh.owner();
225     }
226     
227     protected final Object JavaDoc spawnChildFromText(Class JavaDoc clazz, int memento, String JavaDoc value)
228             throws SAXException JavaDoc {
229         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
230         ueh.text(value);
231         return ueh.owner();
232     }
233
234     // these methods can be used if a child object can be nullable
235
protected final Object JavaDoc spawnChildFromLeaveElement(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
236             throws SAXException JavaDoc {
237         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
238         ueh.leaveElement(uri,local,qname);
239         return ueh.owner();
240     }
241
242     protected final Object JavaDoc spawnChildFromLeaveAttribute(Class JavaDoc clazz, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
243             throws SAXException JavaDoc {
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 JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts )
250             throws SAXException JavaDoc {
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             // if no class is available to unmarshal this element, discard
259
// the sub-tree by feeding events to discarder.
260
context.pushContentHandler( new Discarder(context), memento );
261             context.getCurrentHandler().enterElement(uri,local,qname,atts);
262             return null; // return null so that the discarder will be ignored
263
}
264     }
265     
266 //
267
//
268
// spawn a new child handler.
269
// used for super class and RELAXNG interleave handling.
270
//
271

272     
273     protected final void spawnHandlerFromEnterElement(
274         UnmarshallingEventHandler unm, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts )
275             throws SAXException JavaDoc {
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 JavaDoc uri, String JavaDoc local, String JavaDoc qname)
283             throws SAXException JavaDoc {
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 JavaDoc value)
291             throws SAXException JavaDoc {
292         
293         context.pushContentHandler(unm,memento);
294         unm.text(value);
295     }
296     
297     protected final void spawnHandlerFromLeaveElement(
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.leaveElement(uri,local,qname);
303     }
304     
305     protected final void spawnHandlerFromLeaveAttribute(
306         UnmarshallingEventHandler unm, int memento, String JavaDoc uri, String JavaDoc local, String JavaDoc qname)
307             throws SAXException JavaDoc {
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 JavaDoc text )
315             throws SAXException JavaDoc {
316         
317         context.pushContentHandler(unm,memento);
318         unm.text(text);
319     }
320     
321     
322 //
323
//
324
// revert to parent
325
//
326
//
327
protected final void revertToParentFromEnterElement(String JavaDoc uri,String JavaDoc local, String JavaDoc qname,Attributes JavaDoc atts)
328             throws SAXException JavaDoc {
329         context.popContentHandler();
330         context.getCurrentHandler().enterElement(uri,local,qname,atts);
331     }
332     protected final void revertToParentFromLeaveElement(String JavaDoc uri,String JavaDoc local, String JavaDoc qname)
333             throws SAXException JavaDoc {
334         context.popContentHandler();
335         context.getCurrentHandler().leaveElement(uri,local,qname);
336     }
337     protected final void revertToParentFromEnterAttribute(String JavaDoc uri,String JavaDoc local, String JavaDoc qname)
338             throws SAXException JavaDoc {
339         context.popContentHandler();
340         context.getCurrentHandler().enterAttribute(uri,local,qname);
341     }
342     protected final void revertToParentFromLeaveAttribute(String JavaDoc uri,String JavaDoc local, String JavaDoc qname)
343             throws SAXException JavaDoc {
344         context.popContentHandler();
345         context.getCurrentHandler().leaveAttribute(uri,local,qname);
346     }
347     protected final void revertToParentFromText(String JavaDoc value)
348             throws SAXException JavaDoc {
349         context.popContentHandler();
350         context.getCurrentHandler().text(value);
351     }
352 }
353
Popular Tags