1 8 package com.sun.japex.testsuite.impl.runtime; 9 10 import javax.xml.bind.PropertyException; 11 import javax.xml.bind.ValidationEvent; 12 import javax.xml.bind.ValidationEventHandler; 13 import javax.xml.bind.ValidationException; 14 import javax.xml.bind.Validator; 15 import javax.xml.bind.helpers.DefaultValidationEventHandler; 16 17 import org.xml.sax.SAXException ; 18 19 import com.sun.xml.bind.validator.Messages; 20 21 32 33 36 public class ValidatorImpl implements Validator 37 { 38 39 private ValidationEventHandler eventHandler = 40 new DefaultValidationEventHandler(); 41 42 final DefaultJAXBContextImpl jaxbContext; 43 44 public ValidatorImpl( DefaultJAXBContextImpl c ) { 45 jaxbContext = c; 46 } 47 52 private static class EventInterceptor implements ValidationEventHandler { 53 EventInterceptor( ValidationEventHandler _next ) { 54 this.next = _next; 55 } 56 57 private boolean hadError = false; 58 public boolean hadError() { return hadError; } 59 60 61 private final ValidationEventHandler next; 62 63 public boolean handleEvent( ValidationEvent e ) { 64 hadError = true; 65 boolean result; 66 if( next!=null ) { 67 try { 69 result = next.handleEvent(e); 70 } catch( RuntimeException re ) { 71 result = false; 74 } 75 } else { 76 result = false; 79 } 80 return result; 81 } 82 }; 83 84 public boolean validateRoot( Object o ) throws ValidationException { 85 if( o == null ) { 86 throw new IllegalArgumentException ( 87 Messages.format( Messages.MUST_NOT_BE_NULL, "rootObj" ) ); 88 } 89 90 return validate(o,true); 91 } 92 93 public boolean validate( Object o ) throws ValidationException { 94 if( o == null ) { 95 throw new IllegalArgumentException ( 96 Messages.format( Messages.MUST_NOT_BE_NULL, "subrootObj" ) ); 97 } 98 99 return validate(o,false); 100 } 101 102 private boolean validate( Object o, boolean validateId ) 103 throws ValidationException { 104 105 try { 106 107 ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o); 109 110 if(vo==null) 111 throw new ValidationException( 112 Messages.format( Messages.NOT_VALIDATABLE ) ); 113 114 EventInterceptor ei = new EventInterceptor(eventHandler); 115 ValidationContext context = new ValidationContext(jaxbContext,ei,validateId); 116 context.validate(vo); 117 context.reconcileIDs(); 118 119 return !ei.hadError(); 120 } catch( SAXException e ) { 121 Exception nested = e.getException(); 123 if( nested != null ) { 124 throw new ValidationException( nested ); 125 } else { 126 throw new ValidationException( e ); 127 } 128 } 130 } 131 132 public ValidationEventHandler getEventHandler() { 133 return eventHandler; 134 } 135 136 public void setEventHandler( ValidationEventHandler handler ) { 137 if( handler == null ) { 138 eventHandler = new DefaultValidationEventHandler(); 139 } else { 140 eventHandler = handler; 141 } 142 } 143 144 148 public void setProperty( String name, Object value ) 149 throws PropertyException { 150 151 if( name == null ) { 152 throw new IllegalArgumentException ( 153 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 154 } 155 156 throw new PropertyException(name, value); 157 } 158 159 163 public Object getProperty( String name ) 164 throws PropertyException { 165 166 if( name == null ) { 167 throw new IllegalArgumentException ( 168 Messages.format( Messages.MUST_NOT_BE_NULL, "name" ) ); 169 } 170 171 throw new PropertyException(name); 172 } 173 174 } 175 | Popular Tags |