1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.model.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 /***
25 * Filter implementation of SAXUnmarshallerHandler.
26 *
27 * <p>
28 * This component internally uses a VerifierFilter to validate SAX events that
29 * goes through this component. Discovered error information is just passed down
30 * to the next component.
31 *
32 * <p>
33 * This will enable the implementation to validate all sources of SAX events in
34 * the RI - XMLReader, DOMScanner
35 *
36 * SAX events will go the VerifierFilter and then to the SAXUnmarshaller...
37 *
38 */
39 public class ValidatingUnmarshaller extends ForkContentHandler implements
40 SAXUnmarshallerHandler {
41
42 /***
43 * Creates a new instance of ValidatingUnmarshaller.
44 */
45 public static ValidatingUnmarshaller create(Grammar grammar,
46 SAXUnmarshallerHandler _core, Locator locator) {
47
48
49
50 Verifier v = new Verifier(new REDocumentDeclaration(grammar),
51 new ErrorHandlerAdaptor(_core, locator));
52 v.setPanicMode(true);
53
54 return new ValidatingUnmarshaller(new VerifierFilter(v), _core);
55 }
56
57 private ValidatingUnmarshaller(VerifierFilter filter,
58 SAXUnmarshallerHandler _core) {
59
60 super(filter, _core);
61 this.core = _core;
62 }
63
64
65 public Object getResult() throws JAXBException, IllegalStateException {
66 return core.getResult();
67 }
68
69 public void handleEvent(ValidationEvent event, boolean canRecover)
70 throws SAXException {
71
72
73 core.handleEvent(event, canRecover);
74 }
75
76 private final SAXUnmarshallerHandler core;
77
78 private final AttributesImpl xsiLessAtts = new AttributesImpl();
79
80 public void startElement(String nsUri, String local, String qname,
81 Attributes atts) throws SAXException {
82
83
84 xsiLessAtts.clear();
85 int len = atts.getLength();
86 for (int i = 0; i < len; i++) {
87 String aUri = atts.getURI(i);
88 String aLocal = atts.getLocalName(i);
89 if (aUri.equals("http://www.w3.org/2001/XMLSchema-instance")
90 && (aLocal.equals("schemaLocation") || aLocal
91 .equals("noNamespaceSchemaLocation")
92
93 continue;
94
95
96 xsiLessAtts.addAttribute(aUri, aLocal, atts.getQName(i), atts
97 .getType(i), atts.getValue(i));
98 }
99
100 super.startElement(nsUri, local, qname, xsiLessAtts);
101 }
102 }