Java Reference
In-Depth Information
very precise details to help the user find what went wrong. Let's put some code together that
illustrates this usage.
Write a handler implementation and then modify your class used in Validating Against a
Schema During Marshaling and Unmarshaling to accept your handler. Finally, derange your
XML document slightly in order to make it emit a warning or error. Here, declare the “b” pre-
fix twice:
<b:book xmlns:b='http://ns.soacookbook.com/venetianblind'
'xmlns:b='http://ns.soacookbook.com/x'> ...
Example 2-24 is the implementation of your handler, which simply collects some data and
formats a string with some information. You could collect this information in a database or
perform some other operation here instead.
Example2-24.Custom validation handler
class MyHandler implements ValidationEventHandler {
public boolean handleEvent(ValidationEvent event) {
int severity = event.getSeverity();
if (severity == ValidationEvent.WARNING) {
String msg = event.getMessage();
ValidationEventLocator vel = event.getLocator();
int line = vel.getLineNumber();
String warn = "**** WARNING! Msg is %s." +
"See source line %d.";
System.console().printf(warn, msg, line);
//print warning, but proceed.
return true;
} else {
String err = "**** Got an Error of type %s.";
System.console().printf(err,
event.getLinkedException().getClass().getName());
//ERROR--quit parsing
return false;
}
}
}
The main method that performs the actual unmarshaling is basically the same, except that you
have to set the handler:
Search WWH ::




Custom Search