1 8 package com.sun.japex.testsuite.impl.runtime; 9 10 import javax.xml.bind.ValidationEvent; 11 12 import org.relaxng.datatype.Datatype; 13 import org.xml.sax.SAXException ; 14 import org.xml.sax.helpers.AttributesImpl ; 15 16 import com.sun.msv.grammar.IDContextProvider2; 17 import com.sun.msv.util.LightStack; 18 import com.sun.msv.util.StartTagInfo; 19 import com.sun.msv.util.StringRef; 20 import com.sun.msv.verifier.Acceptor; 21 import com.sun.msv.verifier.regexp.StringToken; 22 import com.sun.xml.bind.JAXBAssertionError; 23 import com.sun.xml.bind.JAXBObject; 24 import com.sun.xml.bind.RIElement; 25 import com.sun.xml.bind.marshaller.IdentifiableObject; 26 import com.sun.xml.bind.serializer.AbortSerializationException; 27 import com.sun.xml.bind.serializer.Util; 28 import com.sun.xml.bind.validator.Messages; 29 30 36 public class MSVValidator implements XMLSerializer, IDContextProvider2 37 { 38 39 private Acceptor acceptor; 40 41 42 private final ValidationContext context; 43 44 45 private final ValidatableObject target; 46 47 final DefaultJAXBContextImpl jaxbContext; 48 49 55 private final LightStack stack = new LightStack(); 56 57 public NamespaceContext2 getNamespaceContext() { 58 return context.getNamespaceContext(); 59 } 60 61 64 private MSVValidator( DefaultJAXBContextImpl _jaxbCtx, ValidationContext _ctxt, ValidatableObject vo ) { 65 jaxbContext = _jaxbCtx; 66 acceptor = vo.createRawValidator().createAcceptor(); 67 context = _ctxt; 68 target = vo; 69 } 70 71 74 public static void validate( DefaultJAXBContextImpl jaxbCtx, ValidationContext context, ValidatableObject vo ) 75 throws SAXException { 76 try { 77 new MSVValidator(jaxbCtx,context,vo)._validate(); 78 } catch( RuntimeException e ) { 79 context.reportEvent(vo,e); 85 } 86 } 87 88 89 private void _validate() throws SAXException { 90 context.getNamespaceContext().startElement(); 91 92 target.serializeURIs(this); 94 95 endNamespaceDecls(); 96 97 target.serializeAttributes(this); 98 99 endAttributes(); 100 101 target.serializeBody(this); 103 writePendingText(); 104 105 context.getNamespaceContext().endElement(); 106 107 if(!acceptor.isAcceptState(null)) { 108 StringRef ref = new StringRef(); 111 acceptor.isAcceptState(ref); 112 context.reportEvent(target,ref.str); 113 } 114 } 115 116 public void endNamespaceDecls() throws SAXException { 117 context.getNamespaceContext().endNamespaceDecls(); 118 } 119 120 public void endAttributes() throws SAXException { 121 if(!acceptor.onEndAttributes( null, null )) { 122 StringRef ref = new StringRef(); 127 StartTagInfo sti = new StartTagInfo( 128 currentElementUri,currentElementLocalName,currentElementLocalName, 129 emptyAttributes,this); 130 acceptor.onEndAttributes( sti, ref ); 131 context.reportEvent(target,ref.str); 132 } 133 } 134 135 136 private StringBuffer buf = new StringBuffer (); 137 138 public final void text( String text, String fieldName ) throws SAXException { 139 if(text==null) { 140 reportMissingObjectError(fieldName); 141 return; 142 } 143 144 if(buf.length()!=0) 145 buf.append(' '); 146 buf.append(text); 147 } 148 149 public void reportMissingObjectError(String fieldName) throws SAXException { 150 reportError(Util.createMissingObjectError(target,fieldName)); 151 } 152 153 154 private String attNamespaceUri; 156 private String attLocalName; 157 private boolean insideAttribute; 158 159 public void startAttribute( String uri, String local ) { 160 this.attNamespaceUri = uri; 162 this.attLocalName = local; 163 insideAttribute = true; 164 } 165 166 public void endAttribute() throws SAXException { 167 insideAttribute = false; 168 if(!acceptor.onAttribute2( attNamespaceUri, attLocalName, 169 attLocalName , 170 buf.toString(), 171 this, null, null )) { 172 173 StringRef ref = new StringRef(); 177 acceptor.onAttribute2( attNamespaceUri, attLocalName, attLocalName, 178 buf.toString(), this, ref, null ); 179 180 context.reportEvent(target,ref.str); 181 } 182 183 buf = new StringBuffer (); 184 } 185 186 private void writePendingText() throws SAXException { 187 if(!acceptor.onText2( buf.toString(), this, null, null )) { 189 StringRef ref = new StringRef(); 192 acceptor.onText2( buf.toString(), this, ref, null ); 193 context.reportEvent(target,ref.str); 194 } 195 196 if(buf.length()>1024) 197 buf = new StringBuffer (); 198 else 199 buf.setLength(0); 200 } 201 202 private String currentElementUri; 203 private String currentElementLocalName; 204 205 public void startElement( String uri, String local ) throws SAXException { 206 writePendingText(); 207 208 context.getNamespaceContext().startElement(); 209 210 stack.push(acceptor); 211 212 StartTagInfo sti = new StartTagInfo(uri,local,local,emptyAttributes,this); 213 214 Acceptor child = acceptor.createChildAcceptor( sti, null ); 219 if( child==null ) { 220 StringRef ref = new StringRef(); 223 child = acceptor.createChildAcceptor( sti, ref ); 224 context.reportEvent(target,ref.str); 225 } 226 227 this.currentElementUri = uri; 228 this.currentElementLocalName = local; 229 230 acceptor = child; 231 } 232 233 public void endElement() throws SAXException { 234 writePendingText(); 235 236 if(!acceptor.isAcceptState(null)) { 237 StringRef ref = new StringRef(); 240 acceptor.isAcceptState(ref); 241 context.reportEvent(target,ref.str); 242 } 243 244 Acceptor child = acceptor; 246 acceptor = (Acceptor)stack.pop(); 247 if(!acceptor.stepForward( child, null )) { 248 StringRef ref = new StringRef(); 251 acceptor.stepForward( child, ref ); 253 context.reportEvent(target,ref.str); 254 } 255 256 context.getNamespaceContext().endElement(); 257 } 258 259 260 public void childAsAttributes( JAXBObject o, String fieldName ) throws SAXException { 261 263 } 269 270 public void childAsURIs( JAXBObject o, String fieldName ) throws SAXException { 271 } 273 274 275 276 private static final AttributesImpl emptyAttributes = new AttributesImpl (); 277 278 279 public static final String DUMMY_ELEMENT_NS = 280 "http://java.sun.com/jaxb/xjc/dummy-elements"; 281 282 public void childAsBody( JAXBObject o, String fieldName ) throws SAXException { 283 final ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o); 285 286 if(vo==null) { 287 reportMissingObjectError(fieldName); 288 return; 289 } 290 291 if( insideAttribute ) childAsAttributeBody(vo,fieldName); 292 else childAsElementBody(o,vo); 293 } 294 295 private void childAsElementBody( Object o, ValidatableObject vo ) throws SAXException { 296 String intfName = vo.getPrimaryInterface().getName(); 297 intfName = intfName.replace('$','.'); 298 299 321 322 StartTagInfo sti = new StartTagInfo( 324 DUMMY_ELEMENT_NS, 325 intfName, 326 intfName, 327 emptyAttributes, 328 this ); 329 330 331 Acceptor child = acceptor.createChildAcceptor(sti,null); 332 if(child==null) { 333 StringRef ref = new StringRef(); 335 child = acceptor.createChildAcceptor(sti,ref); 336 context.reportEvent(target,ref.str); 337 } 338 339 if(o instanceof RIElement) { 340 RIElement rie = (RIElement)o; 341 if(!child.onAttribute2( 342 rie.____jaxb_ri____getNamespaceURI(), 343 rie.____jaxb_ri____getLocalName(), 344 rie.____jaxb_ri____getLocalName(), 345 "", 346 null, null, null )) 347 348 context.reportEvent(target, 350 Messages.format( Messages.INCORRECT_CHILD_FOR_WILDCARD, 351 rie.____jaxb_ri____getNamespaceURI(), 352 rie.____jaxb_ri____getLocalName() )); 353 } 354 355 child.onEndAttributes(sti,null); 356 357 358 if(!acceptor.stepForward(child,null)) { 359 throw new JAXBAssertionError(); 362 } 363 364 365 context.validate(vo); 367 368 } 369 370 private void childAsAttributeBody( ValidatableObject vo, String fieldName ) throws SAXException { 371
|