KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  *
3  */

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