1
2
3
4
5
6
7
8 package net.sf.jour.rt.view.config.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
23 * unreported. So we have to catch them and report it, then rethrow
24 * it if necessary.
25 *
26 * @author
27 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
28 */
29 public class UnmarshallingEventHandlerAdaptor implements UnmarshallingEventHandler {
30
31 protected final UnmarshallingContext context;
32
33 /*** This handler will receive SAX events. */
34 protected final ContentHandler handler;
35
36 public UnmarshallingEventHandlerAdaptor(UnmarshallingContext _ctxt,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
56 private int depth = 0;
57
58 public void enterAttribute(String uri, String local, String qname) throws SAXException {
59 }
60
61 public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException {
62 depth++;
63 context.pushAttributes(atts,true);
64 try {
65 declarePrefixes(context.getNewlyDeclaredPrefixes());
66 handler.startElement(uri,local,qname,atts);
67 } catch( SAXException e ) {
68 error(e);
69 }
70 }
71
72 public void leaveAttribute(String uri, String local, String qname) throws SAXException {
73 }
74
75 public void leaveElement(String uri, String local, String qname) throws SAXException {
76 try {
77 handler.endElement(uri,local,qname);
78 undeclarePrefixes(context.getNewlyDeclaredPrefixes());
79 } catch( SAXException e ) {
80 error(e);
81 }
82 context.popAttributes();
83
84 depth--;
85 if(depth==0) {
86
87 try {
88 undeclarePrefixes(context.getAllDeclaredPrefixes());
89 handler.endDocument();
90 } catch( SAXException e ) {
91 error(e);
92 }
93 context.popContentHandler();
94 }
95 }
96
97 private void declarePrefixes( String[] prefixes ) throws SAXException {
98 for( int i=prefixes.length-1; i>=0; i-- )
99 handler.startPrefixMapping(
100 prefixes[i],
101 context.getNamespaceURI(prefixes[i]) );
102 }
103
104 private void undeclarePrefixes( String[] prefixes ) throws SAXException {
105 for( int i=prefixes.length-1; i>=0; i-- )
106 handler.endPrefixMapping( prefixes[i] );
107 }
108
109 public void text(String s) throws SAXException {
110 try {
111 handler.characters(s.toCharArray(),0,s.length());
112 } catch( SAXException e ) {
113 error(e);
114 }
115 }
116
117 private void error( SAXException e ) throws SAXException {
118 context.handleEvent(new ValidationEventImpl(
119 ValidationEvent.ERROR,
120 e.getMessage(),
121 new ValidationEventLocatorImpl(context.getLocator()),
122 e
123 ), false);
124 }
125
126 public void leaveChild(int nextState) throws SAXException {
127 }
128 }