001    //
002    // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.1-05/30/2003 05:06 AM(java_re)-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: 2004.10.11 at 12:13:34 EDT 
006    //
007    
008    /*
009     * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
010     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
011     */
012    
013    /*
014     * @(#)$Id: AbstractUnmarshallingEventHandlerImpl.java,v 1.9 2003/05/14 18:36:17 lschwenk Exp $
015     */
016    package astronomy.data.spectra.impl.runtime;
017    
018    import java.util.StringTokenizer;
019    
020    import javax.xml.bind.Element;
021    import javax.xml.bind.ParseConversionEvent;
022    import javax.xml.bind.ValidationEvent;
023    import javax.xml.bind.helpers.ParseConversionEventImpl;
024    import javax.xml.bind.helpers.ValidationEventImpl;
025    import javax.xml.bind.helpers.ValidationEventLocatorImpl;
026    
027    import org.xml.sax.Attributes;
028    import org.xml.sax.SAXException;
029    
030    import com.sun.xml.bind.unmarshaller.Messages;
031    
032    /**
033     * Convenient default implementation of
034     * {@link com.sun.xml.bind.unmarshaller.UnmarshallingEventHandler}
035     * to minimize code generation.
036     * 
037     * <p>
038     * For historical reasons, sometimes this type is used where
039     * {@link com.sun.xml.bind.unmarshaller.UnmarshallingEventHandler}
040     * should be used.   
041     * 
042     * Once an exception is in the form of UnmarshalException, we consider
043     * it to be already reported to the client app.
044     */
045    public abstract class AbstractUnmarshallingEventHandlerImpl implements UnmarshallingEventHandler
046    {
047        public AbstractUnmarshallingEventHandlerImpl(UnmarshallingContext _ctxt,
048            String _stateTextTypes ) {
049            
050            this.context = _ctxt;
051            this.stateTextTypes = _stateTextTypes;
052        }
053        public final UnmarshallingContext context;
054        
055        /**
056         * Text type of states encoded into a string.
057         * 'L' means a list state.
058         */
059        private final String stateTextTypes;
060        
061    //
062    //
063    // methods that will be provided by the generated code.
064    //
065    //    
066        // internal events
067        public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException {
068            unexpectedEnterElement(uri,local,qname,atts);
069        }
070        public void leaveElement(String uri, String local, String qname) throws SAXException {
071            unexpectedLeaveElement(uri,local,qname);
072        }
073        public final void text(String text) throws SAXException {
074            if(isListState()) {
075                // in list state, we don't need to care about whitespaces.
076                // if the text is all whitespace, this won't generate a text event,
077                // so it would be just fine.
078                
079                StringTokenizer tokens = new StringTokenizer(text);
080                if( tokens.countTokens()==1 ) {
081                    handleText(text);
082                } else {
083                    while(tokens.hasMoreTokens())
084                        // the handler can be switched during the text processing,
085                        // so the current handler has to be obtained inside the loop
086                        context.getCurrentHandler().text(tokens.nextToken());
087                }
088            } else {
089                // otherwise process this token
090                handleText(text);
091            }
092        }
093        protected void handleText(String s) throws SAXException {
094            unexpectedText(s);
095        }
096        public void enterAttribute(String uri, String local, String qname) throws SAXException {
097            unexpectedEnterAttribute(uri,local,qname);
098        }
099        public void leaveAttribute(String uri, String local, String qname) throws SAXException {
100            unexpectedLeaveAttribute(uri,local,qname);
101        }
102        public void leaveChild(int nextState) throws SAXException {
103            this.state = nextState;
104        }
105        
106        
107        /**
108         * Checks if the current state is marked as a list state.
109         */
110        protected final boolean isListState() {
111            return stateTextTypes.charAt(state)=='L';
112        }
113        
114        
115        /** Current state of this automaton. */
116        public int state;
117        
118        
119        
120        
121    //
122    //
123    // utility methods
124    //
125    //
126        /** Called when a RuntimeException is thrown during unmarshalling a text. */
127        protected void handleUnexpectedTextException( String text, RuntimeException e ) throws SAXException {
128            // report this as an error
129            reportError( Messages.format(Messages.UNEXPECTED_TEXT,text), e, true );
130        }
131        
132        /**
133         * Last resort when something goes terribly wrong within the unmarshaller.
134         */
135        protected void handleGenericException( Exception e ) throws SAXException {
136            reportError( e.getMessage(), e, false );
137        }
138        
139        
140        protected final void dump() {
141            System.err.println("state is :"+state);
142        }
143        private void reportError( String msg, boolean canRecover ) throws SAXException {
144            reportError( msg, null, canRecover );
145        }
146        private void reportError( String msg, Exception nested, boolean canRecover ) throws SAXException {
147            context.handleEvent( new ValidationEventImpl(
148                canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR,
149                msg, 
150                new ValidationEventLocatorImpl(context.getLocator()),
151                nested ), canRecover );
152        }
153        protected final void unexpectedEnterElement( String uri, String local, String qname, Attributes atts ) throws SAXException {
154            // notify the error
155            reportError( Messages.format(Messages.UNEXPECTED_ENTER_ELEMENT, uri, local ), true );
156            // then recover by ignoring the whole element.
157            context.pushContentHandler(new Discarder(context),state);
158            context.getCurrentHandler().enterElement(uri,local,qname,atts);
159        }
160        protected final void unexpectedLeaveElement( String uri, String local, String qname ) throws SAXException {
161            reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri, local ), false );
162        }
163        protected final void unexpectedEnterAttribute( String uri, String local, String qname ) throws SAXException {
164            reportError( Messages.format(Messages.UNEXPECTED_ENTER_ATTRIBUTE, uri, local ), false );
165        }
166        protected final void unexpectedLeaveAttribute( String uri, String local, String qname ) throws SAXException {
167            reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri, local ), false );
168        }
169        protected final void unexpectedText( String str ) throws SAXException {
170            // make str printable
171            str = str.replace('\r',' ').replace('\n',' ').replace('\t',' ').trim();
172            
173            reportError( Messages.format(Messages.UNEXPECTED_TEXT, str ), true );
174        }
175        protected final void unexpectedLeaveChild() throws SAXException {
176            // I believe this is really a bug of the compiler,
177            // since when an object spawns a child object, it must be "prepared"
178            // to receive this event.
179            dump();
180            throw new InternalError( 
181                Messages.format( Messages.UNEXPECTED_LEAVE_CHILD ) );
182        }
183        /**
184         * This method is called by the generated derived class
185         * when a datatype parse method throws an exception.
186         */
187        protected void handleParseConversionException(Exception e) throws SAXException {
188            if( e instanceof RuntimeException )
189                throw (RuntimeException)e;  // don't catch the runtime exception. just let it go.
190            
191            // wrap it into a ParseConversionEvent and report it
192            ParseConversionEvent pce = new ParseConversionEventImpl(
193                ValidationEvent.ERROR, e.getMessage(), 
194                new ValidationEventLocatorImpl(context.getLocator()), e );
195            context.handleEvent(pce,true);
196        }
197        
198        
199    //
200    //    
201    // spawn a new child object
202    //
203    //    
204        private UnmarshallingEventHandler spawnChild( Class clazz, int memento ) {
205            
206            UnmarshallableObject child;
207            try {
208                child = (UnmarshallableObject)clazz.newInstance();
209            } catch (InstantiationException e) {
210                throw new InstantiationError(e.getMessage());
211            } catch (IllegalAccessException e) {
212                throw new IllegalAccessError(e.getMessage());
213            }
214            
215            UnmarshallingEventHandler handler = child.createUnmarshaller(context);
216            context.pushContentHandler(handler,memento);
217            return handler;
218        }
219        
220        protected final Object spawnChildFromEnterElement(Class clazz, int memento, String uri, String local, String qname, Attributes atts)
221                throws SAXException {
222            UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
223            ueh.enterElement(uri,local,qname,atts);
224            return ueh.owner();
225        }
226        
227        protected final Object spawnChildFromEnterAttribute(Class clazz, int memento, String uri, String local, String qname)
228                throws SAXException {
229            UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
230            ueh.enterAttribute(uri,local,qname);
231            return ueh.owner();
232        }
233        
234        protected final Object spawnChildFromText(Class clazz, int memento, String value)
235                throws SAXException {
236            UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
237            ueh.text(value);
238            return ueh.owner();
239        }
240    
241        // these methods can be used if a child object can be nullable
242        protected final Object spawnChildFromLeaveElement(Class clazz, int memento, String uri, String local, String qname)
243                throws SAXException {
244            UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
245            ueh.leaveElement(uri,local,qname);
246            return ueh.owner();
247        }
248    
249        protected final Object spawnChildFromLeaveAttribute(Class clazz, int memento, String uri, String local, String qname)
250                throws SAXException {
251            UnmarshallingEventHandler ueh = spawnChild(clazz,memento);
252            ueh.leaveAttribute(uri,local,qname);
253            return ueh.owner();
254        }
255        
256        protected final Element spawnWildcard( int memento, String uri, String local, String qname, Attributes atts )
257                throws SAXException {
258            UnmarshallingEventHandler ueh = context.getGrammarInfo().createUnmarshaller(uri,local,context);
259            
260            if(ueh!=null) {
261                context.pushContentHandler(ueh,memento);
262                ueh.enterElement(uri,local,qname,atts);
263                return (Element)ueh.owner();
264            } else {
265                // if no class is available to unmarshal this element, discard
266                // the sub-tree by feeding events to discarder.
267                context.pushContentHandler( new Discarder(context), memento );
268                context.getCurrentHandler().enterElement(uri,local,qname,atts);
269                return null;    // return null so that the discarder will be ignored
270            }
271        }
272    //
273    //    
274    // spawn a new child handler.
275    //      used for super class and RELAXNG interleave handling. 
276    //    
277    
278        
279        protected final void spawnHandlerFromEnterElement(
280            UnmarshallingEventHandler unm, int memento, String uri, String local, String qname, Attributes atts )
281                throws SAXException {
282            
283            context.pushContentHandler(unm,memento);
284            unm.enterElement(uri,local,qname,atts);
285        }
286        
287        protected final void spawnHandlerFromEnterAttribute(
288            UnmarshallingEventHandler unm, int memento, String uri, String local, String qname)
289                throws SAXException {
290            
291            context.pushContentHandler(unm,memento);
292            unm.enterAttribute(uri,local,qname);
293        }
294        
295        protected final void spawnHandlerFromFromText(
296            UnmarshallingEventHandler unm, int memento, String value)
297                throws SAXException {
298            
299            context.pushContentHandler(unm,memento);
300            unm.text(value);
301        }
302        
303        protected final void spawnHandlerFromLeaveElement(
304            UnmarshallingEventHandler unm, int memento, String uri, String local, String qname)
305                throws SAXException {
306            
307            context.pushContentHandler(unm,memento);
308            unm.leaveElement(uri,local,qname);
309        }
310        
311        protected final void spawnHandlerFromLeaveAttribute(
312            UnmarshallingEventHandler unm, int memento, String uri, String local, String qname)
313                throws SAXException {
314            
315            context.pushContentHandler(unm,memento);
316            unm.leaveAttribute(uri,local,qname);
317        }
318        
319        protected final void spawnHandlerFromText(
320            UnmarshallingEventHandler unm, int memento, String text )
321                throws SAXException {
322            
323            context.pushContentHandler(unm,memento);
324            unm.text(text);
325        }
326        
327        
328    //
329    //    
330    // revert to parent
331    //
332    //    
333        protected final void revertToParentFromEnterElement(String uri,String local, String qname,Attributes atts)
334                throws SAXException {
335            context.popContentHandler();
336            context.getCurrentHandler().enterElement(uri,local,qname,atts);
337        }
338        protected final void revertToParentFromLeaveElement(String uri,String local, String qname)
339                throws SAXException {
340            context.popContentHandler();
341            context.getCurrentHandler().leaveElement(uri,local,qname);
342        }
343        protected final void revertToParentFromEnterAttribute(String uri,String local, String qname)
344                throws SAXException {
345            context.popContentHandler();
346            context.getCurrentHandler().enterAttribute(uri,local,qname);
347        }
348        protected final void revertToParentFromLeaveAttribute(String uri,String local, String qname)
349                throws SAXException {
350            context.popContentHandler();
351            context.getCurrentHandler().leaveAttribute(uri,local,qname);
352        }
353        protected final void revertToParentFromText(String value)
354                throws SAXException {
355            context.popContentHandler();
356            context.getCurrentHandler().text(value);
357        }
358    }