001//
002// 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 
003// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
004// Any modifications to this file will be lost upon recompilation of the source schema. 
005// Generated on: 2012.10.03 at 04:27:47 AM CEST 
006//
007
008package org.jdtaus.mojo.resource.model.impl.runtime;
009
010import java.util.StringTokenizer;
011
012import javax.xml.bind.Element;
013import javax.xml.bind.ParseConversionEvent;
014import javax.xml.bind.ValidationEvent;
015import javax.xml.bind.helpers.ParseConversionEventImpl;
016import javax.xml.bind.helpers.ValidationEventImpl;
017import javax.xml.bind.helpers.ValidationEventLocatorImpl;
018
019import org.xml.sax.Attributes;
020import org.xml.sax.SAXException;
021
022import com.sun.xml.bind.JAXBAssertionError;
023import com.sun.xml.bind.unmarshaller.Messages;
024
025/**
026 * Convenient default implementation of
027 * {@link UnmarshallingEventHandler}
028 * to minimize code generation.
029 * 
030 * <p>
031 * For historical reasons, sometimes this type is used where
032 * {@link UnmarshallingEventHandler} should be used.   
033 * 
034 * Once an exception is in the form of UnmarshalException, we consider
035 * it to be already reported to the client app.
036 */
037public abstract class AbstractUnmarshallingEventHandlerImpl implements UnmarshallingEventHandler
038{
039    public AbstractUnmarshallingEventHandlerImpl(UnmarshallingContext _ctxt,
040        String _stateTextTypes ) {
041        
042        this.context = _ctxt;
043        this.stateTextTypes = _stateTextTypes;
044    }
045    public final UnmarshallingContext context;
046    
047    /**
048     * Text type of states encoded into a string.
049     * 'L' means a list state.
050     */
051    private final String stateTextTypes;
052    
053//
054//
055// methods that will be provided by the generated code.
056//
057//    
058    // internal events
059    public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException {
060        unexpectedEnterElement(uri,local,qname,atts);
061    }
062    public void leaveElement(String uri, String local, String qname) throws SAXException {
063        unexpectedLeaveElement(uri,local,qname);
064    }
065    public final void text(String text) throws SAXException {
066        if(isListState()) {
067            // in list state, we don't need to care about whitespaces.
068            // if the text is all whitespace, this won't generate a text event,
069            // so it would be just fine.
070            
071            StringTokenizer tokens = new StringTokenizer(text);
072            if( tokens.countTokens()==1 ) {
073                handleText(text);
074            } else {
075                while(tokens.hasMoreTokens())
076                    // the handler can be switched during the text processing,
077                    // so the current handler has to be obtained inside the loop
078                    context.getCurrentHandler().text(tokens.nextToken());
079            }
080        } else {
081            // otherwise process this token
082            handleText(text);
083        }
084    }
085    protected void handleText(String s) throws SAXException {
086        unexpectedText(s);
087    }
088    public void enterAttribute(String uri, String local, String qname) throws SAXException {
089        unexpectedEnterAttribute(uri,local,qname);
090    }
091    public void leaveAttribute(String uri, String local, String qname) throws SAXException {
092        unexpectedLeaveAttribute(uri,local,qname);
093    }
094    public void leaveChild(int nextState) throws SAXException {
095        this.state = nextState;
096    }
097    
098    
099    /**
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}