1 8 package com.mvnforum.jaxb.db.impl.runtime; 9 10 import javax.xml.bind.DatatypeConverter; 11 import javax.xml.bind.PropertyException; 12 import javax.xml.bind.ValidationEvent; 13 import javax.xml.bind.ValidationEventHandler; 14 import javax.xml.bind.ValidationException; 15 import javax.xml.bind.Validator; 16 import javax.xml.bind.helpers.DefaultValidationEventHandler; 17 18 import org.xml.sax.SAXException ; 19 20 import com.sun.xml.bind.DatatypeConverterImpl; 21 import com.sun.xml.bind.validator.Messages; 22 23 34 35 38 public class ValidatorImpl implements Validator 39 { 40 41 private ValidationEventHandler eventHandler = 42 new DefaultValidationEventHandler(); 43 44 final DefaultJAXBContextImpl jaxbContext; 45 46 public ValidatorImpl( DefaultJAXBContextImpl c ) { 47 DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance); 49 50 jaxbContext = c; 51 } 52 57 private static class EventInterceptor implements ValidationEventHandler { 58 EventInterceptor( ValidationEventHandler _next ) { 59 this.next = _next; 60 } 61 62 private boolean hadError = false; 63 public boolean hadError() { return hadError; } 64 65 66 private final ValidationEventHandler next; 67 68 public boolean handleEvent( ValidationEvent e ) { 69 hadError = true; 70 boolean result; 71 if( next!=null ) { 72 try { 74 result = next.handleEvent(e); 75 } catch( RuntimeException re ) { 76 result = false; 79 } 80 } else { 81 result = false; 84 } 85 return result; 86 } 87 }; 88 89 public boolean validateRoot( Object o ) throws ValidationException { 90 if( o == null ) { 91 throw new IllegalArgumentException ( 92 Messages.format( Messages.MUST_NOT_BE_NULL, "rootObj" ) ); 93 } 94 95 return validate(o,true); 96 } 97 98 public boolean validate( Object o ) throws ValidationException { 99 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 = 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 Exception nested = e.getException(); 128 if( nested != null ) { 129 throw new ValidationException( nested ); 130 } else { 131 throw new ValidationException( e ); 132 } 133 } 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 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 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 } 180 | Popular Tags |