View Javadoc

1   //
2   // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.6-01/24/2006 06:15 PM(kohsuke)-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: 2012.10.03 at 04:27:47 AM CEST 
6   //
7   
8   package org.jdtaus.mojo.resource.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
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 _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 stateTextTypes;
52      
53  //
54  //
55  // methods that will be provided by the generated code.
56  //
57  //    
58      // internal events
59      public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException {
60          unexpectedEnterElement(uri,local,qname,atts);
61      }
62      public void leaveElement(String uri, String local, String qname) throws SAXException {
63          unexpectedLeaveElement(uri,local,qname);
64      }
65      public final void text(String text) throws SAXException {
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 tokens = new StringTokenizer(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 s) throws SAXException {
86          unexpectedText(s);
87      }
88      public void enterAttribute(String uri, String local, String qname) throws SAXException {
89          unexpectedEnterAttribute(uri,local,qname);
90      }
91      public void leaveAttribute(String uri, String local, String qname) throws SAXException {
92          unexpectedLeaveAttribute(uri,local,qname);
93      }
94      public void leaveChild(int nextState) throws SAXException {
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 text, RuntimeException e ) throws SAXException {
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 e ) throws SAXException {
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 msg, boolean canRecover ) throws SAXException {
136         reportError( msg, null, canRecover );
137     }
138     private void reportError( String msg, Exception nested, boolean canRecover ) throws SAXException {
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 uri, String local, String qname, Attributes atts ) throws SAXException {
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 uri, String local, String qname ) throws SAXException {
153         reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri, local ), false );
154     }
155     protected final void unexpectedEnterAttribute( String uri, String local, String qname ) throws SAXException {
156         reportError( Messages.format(Messages.UNEXPECTED_ENTER_ATTRIBUTE, uri, local ), false );
157     }
158     protected final void unexpectedLeaveAttribute( String uri, String local, String qname ) throws SAXException {
159         reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri, local ), false );
160     }
161     protected final void unexpectedText( String str ) throws SAXException {
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 {
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 e) throws SAXException {
180         if( e instanceof RuntimeException )
181             throw (RuntimeException)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 clazz, int memento ) {
197         
198         UnmarshallableObject child;
199         try {
200             child = (UnmarshallableObject)clazz.newInstance();
201         } catch (InstantiationException e) {
202             throw new InstantiationError(e.getMessage());
203         } catch (IllegalAccessException e) {
204             throw new IllegalAccessError(e.getMessage());
205         }
206         
207         UnmarshallingEventHandler handler = child.createUnmarshaller(context);
208         context.pushContentHandler(handler,memento);
209         return handler;
210     }
211     
212     protected final Object spawnChildFromEnterElement(Class clazz, int memento, String uri, String local, String qname, Attributes atts)
213             throws SAXException {
214         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
215         ueh.enterElement(uri,local,qname,atts);
216         return ueh.owner();
217     }
218     
219     protected final Object spawnChildFromEnterAttribute(Class clazz, int memento, String uri, String local, String qname)
220             throws SAXException {
221         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
222         ueh.enterAttribute(uri,local,qname);
223         return ueh.owner();
224     }
225     
226     protected final Object spawnChildFromText(Class clazz, int memento, String value)
227             throws SAXException {
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 spawnChildFromLeaveElement(Class clazz, int memento, String uri, String local, String qname)
235             throws SAXException {
236         UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
237         ueh.leaveElement(uri,local,qname);
238         return ueh.owner();
239     }
240 
241     protected final Object spawnChildFromLeaveAttribute(Class clazz, int memento, String uri, String local, String qname)
242             throws SAXException {
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 uri, String local, String qname, Attributes atts )
249             throws SAXException {
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 uri, String local, String qname, Attributes atts )
274             throws SAXException {
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 uri, String local, String qname)
282             throws SAXException {
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 value)
290             throws SAXException {
291         
292         context.pushContentHandler(unm,memento);
293         unm.text(value);
294     }
295     
296     protected final void spawnHandlerFromLeaveElement(
297         UnmarshallingEventHandler unm, int memento, String uri, String local, String qname)
298             throws SAXException {
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 uri, String local, String qname)
306             throws SAXException {
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 text )
314             throws SAXException {
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 uri,String local, String qname,Attributes atts)
327             throws SAXException {
328         context.popContentHandler();
329         context.getCurrentHandler().enterElement(uri,local,qname,atts);
330     }
331     protected final void revertToParentFromLeaveElement(String uri,String local, String qname)
332             throws SAXException {
333         context.popContentHandler();
334         context.getCurrentHandler().leaveElement(uri,local,qname);
335     }
336     protected final void revertToParentFromEnterAttribute(String uri,String local, String qname)
337             throws SAXException {
338         context.popContentHandler();
339         context.getCurrentHandler().enterAttribute(uri,local,qname);
340     }
341     protected final void revertToParentFromLeaveAttribute(String uri,String local, String qname)
342             throws SAXException {
343         context.popContentHandler();
344         context.getCurrentHandler().leaveAttribute(uri,local,qname);
345     }
346     protected final void revertToParentFromText(String value)
347             throws SAXException {
348         context.popContentHandler();
349         context.getCurrentHandler().text(value);
350     }
351 }