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