1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package javax.xml.bind.helpers;
18
19 import java.net.URL;
20
21 import javax.xml.bind.ValidationEventHandler;
22 import javax.xml.bind.ValidationEvent;
23 import javax.xml.bind.ValidationEventLocator;
24
25 import org.w3c.dom.Node;
26
27 public class DefaultValidationEventHandler implements ValidationEventHandler {
28
29 public boolean handleEvent(ValidationEvent event) {
30 if (event == null) {
31 throw new IllegalArgumentException();
32 }
33 String severity = null;
34 boolean retVal = false;
35 switch(event.getSeverity()) {
36 case ValidationEvent.WARNING:
37 severity = "[WARNING]: ";
38 retVal = true;
39 break;
40
41 case ValidationEvent.ERROR:
42 severity = "[ERROR]: ";
43 retVal = false;
44 break;
45
46 case ValidationEvent.FATAL_ERROR:
47 severity = "[FATAL_ERROR]: ";
48 retVal = false;
49 break;
50 }
51 String location = getLocation(event);
52 System.out.println("DefaultValidationEventHandler " + severity + " " + event.getMessage() + "\n Location: " + location);
53 return retVal;
54 }
55
56 private String getLocation(ValidationEvent event) {
57 StringBuffer msg = new StringBuffer();
58 ValidationEventLocator locator = event.getLocator();
59 if (locator != null) {
60 URL url = locator.getURL();
61 Object obj = locator.getObject();
62 Node node = locator.getNode();
63 int line = locator.getLineNumber();
64 if(url != null || line != -1) {
65 msg.append("line ").append(line);
66 if(url != null) {
67 msg.append(" of ").append(url);
68 }
69 } else {
70 if (obj != null) {
71 msg.append(" obj: ").append(obj.toString());
72 } else if(node != null) {
73 msg.append(" node: ").append(node.toString());
74 }
75 }
76 } else {
77 msg.append("unavailable");
78 }
79 return msg.toString();
80 }
81
82 }