KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sun.tools.xjc.runtime;
2
3 import javax.xml.bind.ValidationEvent;
4 import javax.xml.bind.helpers.ValidationEventImpl;
5 import javax.xml.bind.helpers.ValidationEventLocatorImpl;
6
7 import org.xml.sax.Attributes JavaDoc;
8 import org.xml.sax.ContentHandler JavaDoc;
9 import org.xml.sax.SAXException JavaDoc;
10
11 /**
12  * Redirects events to another SAX ContentHandler.
13  *
14  * <p>
15  * Note that the SAXException returned by the ContentHandler is
16  * unreported. So we have to catch them and report it, then rethrow
17  * it if necessary.
18  *
19  * @author
20  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
21  */

22 public class UnmarshallingEventHandlerAdaptor implements UnmarshallingEventHandler {
23     
24     protected final UnmarshallingContext context;
25
26     /** This handler will receive SAX events. */
27     protected final ContentHandler JavaDoc handler;
28
29     public UnmarshallingEventHandlerAdaptor(UnmarshallingContext _ctxt,ContentHandler JavaDoc _handler) throws SAXException JavaDoc {
30         this.context = _ctxt;
31         this.handler = _handler;
32         
33         // emulate the start of documents
34
try {
35             handler.setDocumentLocator(context.getLocator());
36             handler.startDocument();
37             declarePrefixes( context.getAllDeclaredPrefixes() );
38         } catch( SAXException JavaDoc e ) {
39             error(e);
40         }
41     }
42
43     public Object JavaDoc owner() {
44         return null;
45     }
46
47
48     // nest level of elements.
49
private int depth = 0;
50         
51     public void enterAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
52     }
53
54     public void enterElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts) throws SAXException JavaDoc {
55         depth++;
56         context.pushAttributes(atts,true);
57         try {
58             declarePrefixes(context.getNewlyDeclaredPrefixes());
59             handler.startElement(uri,local,qname,atts);
60         } catch( SAXException JavaDoc e ) {
61             error(e);
62         }
63     }
64
65     public void leaveAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
66     }
67
68     public void leaveElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
69         try {
70             handler.endElement(uri,local,qname);
71             undeclarePrefixes(context.getNewlyDeclaredPrefixes());
72         } catch( SAXException JavaDoc e ) {
73             error(e);
74         }
75         context.popAttributes();
76         
77         depth--;
78         if(depth==0) {
79             // emulate the end of the document
80
try {
81                 undeclarePrefixes(context.getAllDeclaredPrefixes());
82                 handler.endDocument();
83             } catch( SAXException JavaDoc e ) {
84                 error(e);
85             }
86             context.popContentHandler();
87         }
88     }
89     
90     private void declarePrefixes( String JavaDoc[] prefixes ) throws SAXException JavaDoc {
91         for( int i=prefixes.length-1; i>=0; i-- )
92             handler.startPrefixMapping(
93                 prefixes[i],
94                 context.getNamespaceURI(prefixes[i]) );
95     }
96     
97     private void undeclarePrefixes( String JavaDoc[] prefixes ) throws SAXException JavaDoc {
98         for( int i=prefixes.length-1; i>=0; i-- )
99             handler.endPrefixMapping( prefixes[i] );
100     }
101
102     public void text(String JavaDoc s) throws SAXException JavaDoc {
103         try {
104             handler.characters(s.toCharArray(),0,s.length());
105         } catch( SAXException JavaDoc e ) {
106             error(e);
107         }
108     }
109     
110     private void error( SAXException JavaDoc e ) throws SAXException JavaDoc {
111         context.handleEvent(new ValidationEventImpl(
112             ValidationEvent.ERROR,
113             e.getMessage(),
114             new ValidationEventLocatorImpl(context.getLocator()),
115             e
116         ), false);
117     }
118
119     public void leaveChild(int nextState) throws SAXException JavaDoc {
120     }
121 }
122
Popular Tags