1 package com.sun.tools.xjc.runtime; 2 3 import javax.xml.bind.ValidationEvent; 4 5 import org.relaxng.datatype.Datatype; 6 import org.xml.sax.SAXException ; 7 import org.xml.sax.helpers.AttributesImpl ; 8 9 import com.sun.msv.grammar.IDContextProvider2; 10 import com.sun.msv.util.LightStack; 11 import com.sun.msv.util.StartTagInfo; 12 import com.sun.msv.util.StringRef; 13 import com.sun.msv.verifier.Acceptor; 14 import com.sun.msv.verifier.regexp.StringToken; 15 import com.sun.xml.bind.JAXBAssertionError; 16 import com.sun.xml.bind.JAXBObject; 17 import com.sun.xml.bind.RIElement; 18 import com.sun.xml.bind.marshaller.IdentifiableObject; 19 import com.sun.xml.bind.serializer.AbortSerializationException; 20 import com.sun.xml.bind.serializer.Util; 21 import com.sun.xml.bind.validator.Messages; 22 23 29 public class MSVValidator implements XMLSerializer, IDContextProvider2 30 { 31 32 private Acceptor acceptor; 33 34 35 private final ValidationContext context; 36 37 38 private final ValidatableObject target; 39 40 final DefaultJAXBContextImpl jaxbContext; 41 42 48 private final LightStack stack = new LightStack(); 49 50 public NamespaceContext2 getNamespaceContext() { 51 return context.getNamespaceContext(); 52 } 53 54 57 private MSVValidator( DefaultJAXBContextImpl _jaxbCtx, ValidationContext _ctxt, ValidatableObject vo ) { 58 jaxbContext = _jaxbCtx; 59 acceptor = vo.createRawValidator().createAcceptor(); 60 context = _ctxt; 61 target = vo; 62 } 63 64 67 public static void validate( DefaultJAXBContextImpl jaxbCtx, ValidationContext context, ValidatableObject vo ) 68 throws SAXException { 69 try { 70 new MSVValidator(jaxbCtx,context,vo)._validate(); 71 } catch( RuntimeException e ) { 72 context.reportEvent(vo,e); 78 } 79 } 80 81 82 private void _validate() throws SAXException { 83 context.getNamespaceContext().startElement(); 84 85 target.serializeURIs(this); 87 88 endNamespaceDecls(); 89 90 target.serializeAttributes(this); 91 92 endAttributes(); 93 94 target.serializeBody(this); 96 writePendingText(); 97 98 context.getNamespaceContext().endElement(); 99 100 if(!acceptor.isAcceptState(null)) { 101 StringRef ref = new StringRef(); 104 acceptor.isAcceptState(ref); 105 context.reportEvent(target,ref.str); 106 } 107 } 108 109 public void endNamespaceDecls() throws SAXException { 110 context.getNamespaceContext().endNamespaceDecls(); 111 } 112 113 public void endAttributes() throws SAXException { 114 if(!acceptor.onEndAttributes( null, null )) { 115 StringRef ref = new StringRef(); 120 StartTagInfo sti = new StartTagInfo( 121 currentElementUri,currentElementLocalName,currentElementLocalName, 122 emptyAttributes,this); 123 acceptor.onEndAttributes( sti, ref ); 124 context.reportEvent(target,ref.str); 125 } 126 } 127 128 129 private StringBuffer buf = new StringBuffer (); 130 131 public final void text( String text, String fieldName ) throws SAXException { 132 if(text==null) { 133 reportMissingObjectError(fieldName); 134 return; 135 } 136 137 if(buf.length()!=0) 138 buf.append(' '); 139 buf.append(text); 140 } 141 142 public void reportMissingObjectError(String fieldName) throws SAXException { 143 reportError(Util.createMissingObjectError(target,fieldName)); 144 } 145 146 147 private String attNamespaceUri; 149 private String attLocalName; 150 private boolean insideAttribute; 151 152 public void startAttribute( String uri, String local ) { 153 this.attNamespaceUri = uri; 155 this.attLocalName = local; 156 insideAttribute = true; 157 } 158 159 public void endAttribute() throws SAXException { 160 insideAttribute = false; 161 if(!acceptor.onAttribute2( attNamespaceUri, attLocalName, 162 attLocalName , 163 buf.toString(), 164 this, null, null )) { 165 166 StringRef ref = new StringRef(); 170 acceptor.onAttribute2( attNamespaceUri, attLocalName, attLocalName, 171 buf.toString(), this, ref, null ); 172 173 context.reportEvent(target,ref.str); 174 } 175 176 buf = new StringBuffer (); 177 } 178 179 private void writePendingText() throws SAXException { 180 if(!acceptor.onText2( buf.toString(), this, null, null )) { 182 StringRef ref = new StringRef(); 185 acceptor.onText2( buf.toString(), this, ref, null ); 186 context.reportEvent(target,ref.str); 187 } 188 189 if(buf.length()>1024) 190 buf = new StringBuffer (); 191 else 192 buf.setLength(0); 193 } 194 195 private String currentElementUri; 196 private String currentElementLocalName; 197 198 public void startElement( String uri, String local ) throws SAXException { 199 writePendingText(); 200 201 context.getNamespaceContext().startElement(); 202 203 stack.push(acceptor); 204 205 StartTagInfo sti = new StartTagInfo(uri,local,local,emptyAttributes,this); 206 207 Acceptor child = acceptor.createChildAcceptor( sti, null ); 212 if( child==null ) { 213 StringRef ref = new StringRef(); 216 child = acceptor.createChildAcceptor( sti, ref ); 217 context.reportEvent(target,ref.str); 218 } 219 220 this.currentElementUri = uri; 221 this.currentElementLocalName = local; 222 223 acceptor = child; 224 } 225 226 public void endElement() throws SAXException { 227 writePendingText(); 228 229 if(!acceptor.isAcceptState(null)) { 230 StringRef ref = new StringRef(); 233 acceptor.isAcceptState(ref); 234 context.reportEvent(target,ref.str); 235 } 236 237 Acceptor child = acceptor; 239 acceptor = (Acceptor)stack.pop(); 240 if(!acceptor.stepForward( child, null )) { 241 StringRef ref = new StringRef(); 244 acceptor.stepForward( child, ref ); 246 context.reportEvent(target,ref.str); 247 } 248 249 context.getNamespaceContext().endElement(); 250 } 251 252 253 public void childAsAttributes( JAXBObject o, String fieldName ) throws SAXException { 254 256 } 262 263 public void childAsURIs( JAXBObject o, String fieldName ) throws SAXException { 264 } 266 267 268 269 private static final AttributesImpl emptyAttributes = new AttributesImpl (); 270 271 272 public static final String DUMMY_ELEMENT_NS = 273 "http://java.sun.com/jaxb/xjc/dummy-elements"; 274 275 public void childAsBody( JAXBObject o, String fieldName ) throws SAXException { 276 final ValidatableObject vo = jaxbContext.getGrammarInfo().castToValidatableObject(o); 278 279 if(vo==null) { 280 reportMissingObjectError(fieldName); 281 return; 282 } 283 284 if( insideAttribute ) childAsAttributeBody(vo,fieldName); 285 else childAsElementBody(o,vo); 286 } 287 288 private void childAsElementBody( Object o, ValidatableObject vo ) throws SAXException { 289 String intfName = vo.getPrimaryInterface().getName(); 290 intfName = intfName.replace('$','.'); 291 292 314 315 StartTagInfo sti = new StartTagInfo( 317 DUMMY_ELEMENT_NS, 318 intfName, 319 intfName, 320 emptyAttributes, 321 this ); 322 323 324 Acceptor child = acceptor.createChildAcceptor(sti,null); 325 if(child==null) { 326 StringRef ref = new StringRef(); 328 child = acceptor.createChildAcceptor(sti,ref); 329 context.reportEvent(target,ref.str); 330 } 331 332 if(o instanceof RIElement) { 333 RIElement rie = (RIElement)o; 334 if(!child.onAttribute2( 335 rie.____jaxb_ri____getNamespaceURI(), 336 rie.____jaxb_ri____getLocalName(), 337 rie.____jaxb_ri____getLocalName(), 338 "", 339 null, null, null )) 340 341 context.reportEvent(target, 343 Messages.format( Messages.INCORRECT_CHILD_FOR_WILDCARD, 344 rie.____jaxb_ri____getNamespaceURI(), 345 rie.____jaxb_ri____getLocalName() )); 346 } 347 348 child.onEndAttributes(sti,null); 349 350 351 if(!acceptor.stepForward(child,null)) { 352 throw new JAXBAssertionError(); 355 } 356 357 358 context.validate(vo); 360 361 } 362 363 private void childAsAttributeBody( ValidatableObject vo, String fieldName ) throws SAXException { 364
|