KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sun.tools.xjc.runtime;
2
3 import javax.xml.bind.ValidationEvent;
4 import javax.xml.bind.ValidationEventLocator;
5 import javax.xml.bind.helpers.ValidationEventImpl;
6
7 import org.xml.sax.ErrorHandler JavaDoc;
8 import org.xml.sax.SAXException JavaDoc;
9 import org.xml.sax.SAXParseException JavaDoc;
10
11 import com.sun.xml.bind.validator.Locator;
12
13 /**
14  * Receives errors through {@link ErrorHandler} and reports to the
15  * {@link SAXUnmarshallerHandler}.
16  *
17  * @author
18  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
19  */

20 public class ErrorHandlerAdaptor implements ErrorHandler JavaDoc {
21     
22     /** the client event handler that will receive the validation events */
23     private final SAXUnmarshallerHandler host;
24     
25     /** the locator object responsible for filling in the validation event
26      * location info **/

27     private final Locator locator;
28    
29     public ErrorHandlerAdaptor(
30         SAXUnmarshallerHandler _host, Locator locator ) {
31         this.host = _host;
32         this.locator = locator;
33     }
34     
35     public void error(SAXParseException JavaDoc exception)
36         throws SAXException JavaDoc {
37             
38         propagateEvent( ValidationEvent.ERROR, exception );
39     }
40     
41     public void warning(SAXParseException JavaDoc exception)
42         throws SAXException JavaDoc {
43             
44         propagateEvent( ValidationEvent.WARNING, exception );
45     }
46     
47     public void fatalError(SAXParseException JavaDoc exception)
48         throws SAXException JavaDoc {
49             
50         propagateEvent( ValidationEvent.FATAL_ERROR, exception );
51     }
52     
53     private void propagateEvent( int severity, SAXParseException JavaDoc saxException )
54         throws SAXException JavaDoc {
55             
56         // get location info:
57
// sax locators simply use the location info embedded in the
58
// sax exception, dom locators keep a reference to their DOMScanner
59
// and call back to figure out where the error occurred.
60
ValidationEventLocator vel =
61             locator.getLocation( saxException );
62
63         ValidationEventImpl ve =
64             new ValidationEventImpl( severity, saxException.getMessage(), vel );
65
66         Exception JavaDoc e = saxException.getException();
67         if( e != null ) {
68             ve.setLinkedException( e );
69         } else {
70             ve.setLinkedException( saxException );
71         }
72         
73         // call the client's event handler.
74
host.handleEvent( ve, severity!=ValidationEvent.FATAL_ERROR );
75     }
76 }
77
Popular Tags