KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > vo > jaxb > employee > impl > runtime > AbstractUnmarshallingEventHandlerImpl


1 //
2
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-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: 2006.10.05 at 10:03:40 CEST
6
//
7

8 package net.sf.dozer.util.mapping.vo.jaxb.employee.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 UnmarshallingEventHandler}
28  * to minimize code generation.
29  *
30  * <p>
31  * For historical reasons, sometimes this type is used where
32  * {@link UnmarshallingEventHandler} should be used.
33  *
34  * Once an exception is in the form of UnmarshalException, we consider
35  * it to be already reported to the client app.
36  */

37 public abstract class AbstractUnmarshallingEventHandlerImpl implements UnmarshallingEventHandler
38 {
39     public AbstractUnmarshallingEventHandlerImpl(UnmarshallingContext _ctxt,
40         String JavaDoc _stateTextTypes ) {
41         
42         this.context = _ctxt;
43         this.stateTextTypes = _stateTextTypes;
44     }
45     public final UnmarshallingContext context;
46     
47     /**
48      * Text type of states encoded into a string.
49      * 'L' means a list state.
50      */

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

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

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

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

179     protected void handleParseConversionException(Exception JavaDoc e) throws SAXException JavaDoc {
180         if( e instanceof RuntimeException JavaDoc )
181             throw (RuntimeException JavaDoc)e; // don't catch the runtime exception. just let it go.
182

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

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