1
2
3
4
5
6
7
8 package de.nierbeck.timeTrack.model.impl.runtime;
9
10 import javax.xml.bind.ValidationEvent;
11 import javax.xml.bind.helpers.ValidationEventImpl;
12 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
13
14 import org.xml.sax.Attributes;
15 import org.xml.sax.ContentHandler;
16 import org.xml.sax.SAXException;
17
18 /***
19 * Redirects events to another SAX ContentHandler.
20 *
21 * <p>
22 * Note that the SAXException returned by the ContentHandler is unreported. So
23 * we have to catch them and report it, then rethrow it if necessary.
24 *
25 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
26 */
27 public class UnmarshallingEventHandlerAdaptor implements
28 UnmarshallingEventHandler {
29
30 protected final UnmarshallingContext context;
31
32 /*** This handler will receive SAX events. */
33 protected final ContentHandler handler;
34
35 public UnmarshallingEventHandlerAdaptor(UnmarshallingContext _ctxt,
36 ContentHandler _handler) throws SAXException {
37 this.context = _ctxt;
38 this.handler = _handler;
39
40
41 try {
42 handler.setDocumentLocator(context.getLocator());
43 handler.startDocument();
44 declarePrefixes(context.getAllDeclaredPrefixes());
45 } catch (SAXException e) {
46 error(e);
47 }
48 }
49
50 public Object owner() {
51 return null;
52 }
53
54
55 private int depth = 0;
56
57 public void enterAttribute(String uri, String local, String qname)
58 throws SAXException {
59 }
60
61 public void enterElement(String uri, String local, String qname,
62 Attributes atts) throws SAXException {
63 depth++;
64 context.pushAttributes(atts, true);
65 try {
66 declarePrefixes(context.getNewlyDeclaredPrefixes());
67 handler.startElement(uri, local, qname, atts);
68 } catch (SAXException e) {
69 error(e);
70 }
71 }
72
73 public void leaveAttribute(String uri, String local, String qname)
74 throws SAXException {
75 }
76
77 public void leaveElement(String uri, String local, String qname)
78 throws SAXException {
79 try {
80 handler.endElement(uri, local, qname);
81 undeclarePrefixes(context.getNewlyDeclaredPrefixes());
82 } catch (SAXException e) {
83 error(e);
84 }
85 context.popAttributes();
86
87 depth--;
88 if (depth == 0) {
89
90 try {
91 undeclarePrefixes(context.getAllDeclaredPrefixes());
92 handler.endDocument();
93 } catch (SAXException e) {
94 error(e);
95 }
96 context.popContentHandler();
97 }
98 }
99
100 private void declarePrefixes(String[] prefixes) throws SAXException {
101 for (int i = prefixes.length - 1; i >= 0; i--)
102 handler.startPrefixMapping(prefixes[i], context
103 .getNamespaceURI(prefixes[i]));
104 }
105
106 private void undeclarePrefixes(String[] prefixes) throws SAXException {
107 for (int i = prefixes.length - 1; i >= 0; i--)
108 handler.endPrefixMapping(prefixes[i]);
109 }
110
111 public void text(String s) throws SAXException {
112 try {
113 handler.characters(s.toCharArray(), 0, s.length());
114 } catch (SAXException e) {
115 error(e);
116 }
117 }
118
119 private void error(SAXException e) throws SAXException {
120 context.handleEvent(new ValidationEventImpl(ValidationEvent.ERROR, e
121 .getMessage(), new ValidationEventLocatorImpl(context
122 .getLocator()), e), false);
123 }
124
125 public void leaveChild(int nextState) throws SAXException {
126 }
127 }