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