1 package com.sun.tools.xjc.runtime; 2 3 import javax.xml.bind.DatatypeConverter; 4 import javax.xml.bind.PropertyException; 5 import javax.xml.bind.ValidationEvent; 6 import javax.xml.bind.ValidationEventHandler; 7 import javax.xml.bind.ValidationException; 8 import javax.xml.bind.Validator; 9 import javax.xml.bind.helpers.DefaultValidationEventHandler; 10 11 import org.xml.sax.SAXException ; 12 13 import com.sun.xml.bind.DatatypeConverterImpl; 14 import com.sun.xml.bind.validator.Messages; 15 16 27 28 31 public class ValidatorImpl implements Validator 32 { 33 34 private ValidationEventHandler eventHandler = 35 new DefaultValidationEventHandler(); 36 37 final DefaultJAXBContextImpl jaxbContext; 38 39 public ValidatorImpl( DefaultJAXBContextImpl c ) { 40 DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance); 42 43 jaxbContext = c; 44 } 45 50 private static class EventInterceptor implements ValidationEventHandler { 51 EventInterceptor( ValidationEventHandler _next ) { 52 this.next = _next; 53 } 54 55 private boolean hadError = false; 56 public boolean hadError() { return hadError; } 57 58 59 private final ValidationEventHandler next; 60 61 public boolean handleEvent( ValidationEvent e ) { 62 hadError = true; 63 boolean result; 64 if( next!=null ) { 65 try { 67 result = next.handleEvent(e); 68 } catch( RuntimeException re ) { 69 result = false; 72 } 73 } else { 74 result = false; 77 } 78 return result; 79 } 80 }; 81 82 public boolean validateRoot( Object o ) throws ValidationException { 83 if( o == null ) { 84 throw new IllegalArgumentException ( 85 Messages.format( Messages.MUST_NOT_BE_NULL, "rootObj" ) ); 86 } 87 88 return validate(o,true); 89 } 90 91 public boolean validate( Object o ) throws ValidationException { 92 if( o == null ) { 93 throw new IllegalArgumentException ( 94 Messages.format( Messages.MUST_NOT_BE_NULL, "subrootObj" ) ); 95 } 96 97 return validate(o,false); 98 } 99 100 private boolean validate( Object o, boolean validateId ) 101 throws ValidationException { 102 103 try { 104 105 ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(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(jaxbContext,ei,validateId); 114 context.validate(vo); 115 context.reconcileIDs(); 116 117 return !ei.hadError(); 118 } catch( SAXException e ) { 119 Exception nested = e.getException(); 121 if( nested != null ) { 122 throw new ValidationException( nested ); 123 } else { 124 throw new ValidationException( e ); 125 } 126 } 128 } 129 130 public ValidationEventHandler getEventHandler() { 131 return eventHandler; 132 } 133 134 public void setEventHandler( ValidationEventHandler handler ) { 135 if( handler == null ) { 136 eventHandler = new DefaultValidationEventHandler(); 137 } else { 138 eventHandler = handler; 139 } 140 } 141 142 146 public void setProperty( String name, Object value ) 147 throws PropertyException { 148 149 if( name == null ) { 150 throw new IllegalArgumentException ( 151 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 152 } 153 154 throw new PropertyException(name, value); 155 } 156 157 161 public Object getProperty( String name ) 162 throws PropertyException { 163 164 if( name == null ) { 165 throw new IllegalArgumentException ( 166 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 167 } 168 169 throw new PropertyException(name); 170 } 171 172 } 173 | Popular Tags |