KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > xjc > runtime > ValidatingUnmarshaller


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 JavaDoc;
8 import org.xml.sax.SAXException JavaDoc;
9 import org.xml.sax.helpers.AttributesImpl JavaDoc;
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 /**
18  * Filter implementation of SAXUnmarshallerHandler.
19  *
20  * <p>
21  * This component internally uses a VerifierFilter to validate
22  * SAX events that goes through this component.
23  * Discovered error information is just passed down to the next component.
24  *
25  * <p>
26  * This will enable the implementation to validate all sources of SAX events
27  * in the RI - XMLReader, DOMScanner
28  *
29  * SAX events will go the VerifierFilter and then to the SAXUnmarshaller...
30  *
31  */

32 public class ValidatingUnmarshaller extends ForkContentHandler
33     implements SAXUnmarshallerHandler {
34     
35     /**
36      * Creates a new instance of ValidatingUnmarshaller.
37      */

38     public static ValidatingUnmarshaller create( Grammar grammar,
39                             SAXUnmarshallerHandler _core,
40                             Locator locator ) {
41         
42         // create a VerifierFilter and configure it
43
// so that error messages will be sent to the core,
44
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     // delegate to the next component
61
public Object JavaDoc getResult() throws JAXBException, IllegalStateException JavaDoc {
62         return core.getResult();
63     }
64
65     public void handleEvent(ValidationEvent event, boolean canRecover ) throws SAXException JavaDoc {
66         // SAXUnmarshallerHandler already checks for RuntimeExceptions, so
67
// there is no need to wrap this call in a try/catch
68
core.handleEvent(event,canRecover);
69     }
70     
71     private final SAXUnmarshallerHandler core;
72     
73     
74     private final AttributesImpl JavaDoc xsiLessAtts = new AttributesImpl JavaDoc();
75     
76     public void startElement( String JavaDoc nsUri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts ) throws SAXException JavaDoc {
77         // create an attributes set for MSV that doesn't contains xsi:schemaLocation
78
xsiLessAtts.clear();
79         int len = atts.getLength();
80         for( int i=0; i<len; i++ ) {
81             String JavaDoc aUri = atts.getURI(i);
82             String JavaDoc aLocal = atts.getLocalName(i);
83             if(aUri.equals("http://www.w3.org/2001/XMLSchema-instance")
84             && (aLocal.equals("schemaLocation") ||
85                 aLocal.equals("noNamespaceSchemaLocation") //||
86
/*aLocal.equals("type")*/))
87                 continue;
88                 
89             // we do handle xsi:nil.
90
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