KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > sunday > unmarshalling > XsdBinderLoggingErrorHandler


1 /**
2  *
3  */

4 package org.jboss.xb.binding.sunday.unmarshalling;
5
6 import org.w3c.dom.DOMError JavaDoc;
7 import org.w3c.dom.DOMErrorHandler JavaDoc;
8 import org.w3c.dom.DOMLocator JavaDoc;
9
10 public class XsdBinderLoggingErrorHandler implements DOMErrorHandler JavaDoc
11 {
12    private static XsdBinderLoggingErrorHandler errorHandler;
13
14    // Hide constructor
15
private XsdBinderLoggingErrorHandler()
16    {
17    }
18
19    public static XsdBinderLoggingErrorHandler newInstance()
20    {
21       if (errorHandler == null)
22       {
23          errorHandler = new XsdBinderLoggingErrorHandler();
24       }
25       return errorHandler;
26    }
27
28    public boolean handleError(DOMError JavaDoc error)
29    {
30       // todo: i do throw exceptions here instead of returning false to stop parsing immediately
31
// since returning false seems to be no different from true (a bug in the parser?)
32
// Although, throwing an exception reports the same error twice but the second time with
33
// location -1:-1
34
switch (error.getSeverity())
35       {
36          case DOMError.SEVERITY_ERROR:
37             XsdBinder.log.error(formatMessage(error));
38          case DOMError.SEVERITY_FATAL_ERROR:
39             XsdBinder.log.fatal(formatMessage(error));
40          case DOMError.SEVERITY_WARNING:
41             XsdBinder.log.warn(formatMessage(error));
42             break;
43       }
44       return false;
45    }
46
47    String JavaDoc formatMessage(DOMError JavaDoc error)
48    {
49       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
50       DOMLocator JavaDoc location = error.getLocation();
51       if (location != null)
52       {
53          buf.append(location.getLineNumber()).append(':').append(location.getColumnNumber());
54       }
55       else
56       {
57          buf.append("[location unavailable]");
58       }
59
60       buf.append(' ').append(error.getMessage());
61       return buf.toString();
62    }
63 }
Popular Tags