KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > vo > jaxb > employee > impl > runtime > UnmarshallingEventHandlerAdaptor


1 //
2
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-b18-fcs
3
// See <a HREF="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
4
// Any modifications to this file will be lost upon recompilation of the source schema.
5
// Generated on: 2006.10.05 at 10:03:40 CEST
6
//
7

8 package net.sf.dozer.util.mapping.vo.jaxb.employee.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 JavaDoc;
15 import org.xml.sax.ContentHandler JavaDoc;
16 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc handler;
35
36     public UnmarshallingEventHandlerAdaptor(UnmarshallingContext _ctxt,ContentHandler JavaDoc _handler) throws SAXException JavaDoc {
37         this.context = _ctxt;
38         this.handler = _handler;
39         
40         // emulate the start of documents
41
try {
42             handler.setDocumentLocator(context.getLocator());
43             handler.startDocument();
44             declarePrefixes( context.getAllDeclaredPrefixes() );
45         } catch( SAXException JavaDoc e ) {
46             error(e);
47         }
48     }
49
50     public Object JavaDoc owner() {
51         return null;
52     }
53
54
55     // nest level of elements.
56
private int depth = 0;
57         
58     public void enterAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
59     }
60
61     public void enterElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname, Attributes JavaDoc atts) throws SAXException JavaDoc {
62         depth++;
63         context.pushAttributes(atts,true);
64         try {
65             declarePrefixes(context.getNewlyDeclaredPrefixes());
66             handler.startElement(uri,local,qname,atts);
67         } catch( SAXException JavaDoc e ) {
68             error(e);
69         }
70     }
71
72     public void leaveAttribute(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
73     }
74
75     public void leaveElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qname) throws SAXException JavaDoc {
76         try {
77             handler.endElement(uri,local,qname);
78             undeclarePrefixes(context.getNewlyDeclaredPrefixes());
79         } catch( SAXException JavaDoc e ) {
80             error(e);
81         }
82         context.popAttributes();
83         
84         depth--;
85         if(depth==0) {
86             // emulate the end of the document
87
try {
88                 undeclarePrefixes(context.getAllDeclaredPrefixes());
89                 handler.endDocument();
90             } catch( SAXException JavaDoc e ) {
91                 error(e);
92             }
93             context.popContentHandler();
94         }
95     }
96     
97     private void declarePrefixes( String JavaDoc[] prefixes ) throws SAXException JavaDoc {
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 JavaDoc[] prefixes ) throws SAXException JavaDoc {
105         for( int i=prefixes.length-1; i>=0; i-- )
106             handler.endPrefixMapping( prefixes[i] );
107     }
108
109     public void text(String JavaDoc s) throws SAXException JavaDoc {
110         try {
111             handler.characters(s.toCharArray(),0,s.length());
112         } catch( SAXException JavaDoc e ) {
113             error(e);
114         }
115     }
116     
117     private void error( SAXException JavaDoc e ) throws SAXException JavaDoc {
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 JavaDoc {
127     }
128 }
129
Popular Tags