1 package com.sun.tools.xjc.runtime; 2 3 import javax.xml.bind.JAXBException; 4 import javax.xml.bind.ValidationEvent; 5 6 import org.iso_relax.verifier.impl.ForkContentHandler; 7 import org.xml.sax.Attributes ; 8 import org.xml.sax.SAXException ; 9 import org.xml.sax.helpers.AttributesImpl ; 10 11 import com.sun.msv.grammar.Grammar; 12 import com.sun.msv.verifier.Verifier; 13 import com.sun.msv.verifier.VerifierFilter; 14 import com.sun.msv.verifier.regexp.REDocumentDeclaration; 15 import com.sun.xml.bind.validator.Locator; 16 17 32 public class ValidatingUnmarshaller extends ForkContentHandler 33 implements SAXUnmarshallerHandler { 34 35 38 public static ValidatingUnmarshaller create( Grammar grammar, 39 SAXUnmarshallerHandler _core, 40 Locator locator ) { 41 42 Verifier v = new Verifier( 45 new REDocumentDeclaration(grammar), 46 new ErrorHandlerAdaptor(_core,locator) ); 47 v.setPanicMode( true ); 48 49 return new ValidatingUnmarshaller( 50 new VerifierFilter( v ), _core ); 51 } 52 53 private ValidatingUnmarshaller( VerifierFilter filter, 54 SAXUnmarshallerHandler _core ) { 55 56 super( filter, _core ); 57 this.core = _core; 58 } 59 60 public Object getResult() throws JAXBException, IllegalStateException { 62 return core.getResult(); 63 } 64 65 public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException { 66 core.handleEvent(event,canRecover); 69 } 70 71 private final SAXUnmarshallerHandler core; 72 73 74 private final AttributesImpl xsiLessAtts = new AttributesImpl (); 75 76 public void startElement( String nsUri, String local, String qname, Attributes atts ) throws SAXException { 77 xsiLessAtts.clear(); 79 int len = atts.getLength(); 80 for( int i=0; i<len; i++ ) { 81 String aUri = atts.getURI(i); 82 String aLocal = atts.getLocalName(i); 83 if(aUri.equals("http://www.w3.org/2001/XMLSchema-instance") 84 && (aLocal.equals("schemaLocation") || 85 aLocal.equals("noNamespaceSchemaLocation") )) 87 continue; 88 89 xsiLessAtts.addAttribute( aUri, aLocal, 91 atts.getQName(i), atts.getType(i), atts.getValue(i) ); 92 } 93 94 super.startElement(nsUri,local,qname,xsiLessAtts); 95 } 96 } 97 98 99 100 101 102 103 104 | Popular Tags |