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    package astronomy.data.spectra.impl.runtime;
013    
014    import javax.xml.bind.PropertyException;
015    import javax.xml.bind.ValidationEvent;
016    import javax.xml.bind.ValidationEventHandler;
017    import javax.xml.bind.ValidationException;
018    import javax.xml.bind.Validator;
019    import javax.xml.bind.helpers.DefaultValidationEventHandler;
020    
021    import org.xml.sax.SAXException;
022    
023    import com.sun.xml.bind.validator.Messages;
024    
025    /*
026        TODO:
027        reorganize classes into appropriate packages.
028        to reflect the fact that some of the classes in
029        the marshaller package are used for both marshalling
030        and validation.
031    
032        In particular, the MarshallingContext interface should be
033        renamed. It is not only for marshalling. 
034        (something like "Serializer", maybe).
035    */
036    
037    /**
038     * Validator implementation of JAXB RI.
039     */
040    public class ValidatorImpl implements Validator
041    {
042        /** Validation errors will be reported to this object. */
043        private ValidationEventHandler eventHandler = 
044            new DefaultValidationEventHandler();
045        
046        /**
047         * We need to know whether an validation error was detected or not.
048         * For this purpose, we set up the validation so that this interceptor
049         * will "intercept" errors before the application receives it.
050         */
051        private static class EventInterceptor implements ValidationEventHandler {
052            EventInterceptor( ValidationEventHandler _next ) {
053                this.next = _next;
054            }
055            
056            private boolean hadError = false;
057            public boolean hadError() { return hadError; }
058            
059            /** event will be passed to this component. */
060            private final ValidationEventHandler next;
061            
062            public boolean handleEvent( ValidationEvent e ) {
063                hadError = true;
064                boolean result;
065                if( next!=null ) {
066                    // pass it to the application
067                    try {
068                        result = next.handleEvent(e);
069                    } catch( RuntimeException re ) {
070                        // if the client event handler causes a RuntimeException,
071                        // then we have to return false
072                        result = false;
073                    }
074                } else {
075                    // if no error handler was specified, there is no point
076                    // in continuing the validation.
077                    result = false;
078                }
079                return result;
080            }
081        };
082    
083        public boolean validateRoot( Object o ) throws ValidationException {
084            if( o == null ) {
085                throw new IllegalArgumentException( 
086                    Messages.format( Messages.MUST_NOT_BE_NULL, "rootObj" ) );
087            }
088            
089            return validate(o,true);
090        }
091        
092        public boolean validate( Object o ) throws ValidationException {
093            if( o == null ) {
094                throw new IllegalArgumentException( 
095                    Messages.format( Messages.MUST_NOT_BE_NULL, "subrootObj" ) );
096            }
097            
098            return validate(o,false);
099        }
100        
101        private boolean validate( Object o, boolean validateId ) 
102            throws ValidationException { 
103                
104            try {
105            
106                ValidatableObject vo = Util.toValidatableObject(o);
107                
108                if(vo==null)
109                    throw new ValidationException(
110                        Messages.format( Messages.NOT_VALIDATABLE ) );
111            
112                EventInterceptor ei = new EventInterceptor(eventHandler);
113                ValidationContext context = new ValidationContext(ei,validateId);
114                context.validate(vo);
115                context.reconcileIDs();
116                
117                return !ei.hadError();
118            } catch( SAXException e ) {
119                // TODO exception handling.
120                // we need a consistent mechanism to convert SAXException into JAXBException
121                Exception nested = e.getException();
122                if( e != null ) {
123                    throw new ValidationException( nested );
124                } else {
125                    throw new ValidationException( e );
126                }
127                //return false;
128            }
129        }
130        
131        public ValidationEventHandler getEventHandler() {
132            return eventHandler;
133        }
134        
135        public void setEventHandler( ValidationEventHandler handler ) {
136            if( handler == null ) {
137                eventHandler = new DefaultValidationEventHandler();
138            } else {
139                eventHandler = handler;
140            }
141        }
142        
143        /**
144         * There are no required properties, so simply throw an exception.  Other
145         * providers may have support for properties on Validator, but the RI doesn't
146         */
147        public void setProperty( String name, Object value )
148            throws PropertyException {
149            
150            if( name == null ) {
151                throw new IllegalArgumentException(
152                    Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
153            }
154    
155            throw new PropertyException(name, value);
156        }
157        
158        /**
159         * There are no required properties, so simply throw an exception.  Other
160         * providers may have support for properties on Validator, but the RI doesn't
161         */
162        public Object getProperty( String name )
163            throws PropertyException {
164                
165            if( name == null ) {
166                throw new IllegalArgumentException(
167                    Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) );
168            }
169    
170            throw new PropertyException(name);
171        }
172    
173    }