001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
003: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004: */
005: package javax.xml.bind.helpers;
006:
007: import org.w3c.dom.Node;
008:
009: import javax.xml.bind.ValidationEvent;
010: import javax.xml.bind.ValidationEventHandler;
011: import javax.xml.bind.ValidationEventLocator;
012: import java.net.URL;
013:
014: /**
015: * <p>
016: * JAXB 1.0 only default validation event handler. This is the default
017: * handler for all objects created from a JAXBContext that is managing
018: * schema-derived code generated by a JAXB 1.0 binding compiler.
019: *
020: * <p>
021: * This handler causes the unmarshal and validate operations to fail on the first
022: * error or fatal error.
023: *
024: * <p>
025: * This handler is not the default handler for JAXB mapped classes following
026: * JAXB 2.0 or later versions. Default validation event handling has changed
027: * and is specified in {@link javax.xml.bind.Unmarshaller} and
028: * {@link javax.xml.bind.Marshaller}.
029: *
030: * @author <ul><li>Ryan Shoemaker, Sun Microsystems, Inc.</li></ul>
031: * @version $Revision: 1.5 $
032: * @see javax.xml.bind.Unmarshaller
033: * @see javax.xml.bind.Validator
034: * @see javax.xml.bind.ValidationEventHandler
035: * @since JAXB1.0
036: */
037: public class DefaultValidationEventHandler implements
038: ValidationEventHandler {
039:
040: public boolean handleEvent(ValidationEvent event) {
041:
042: if (event == null) {
043: throw new IllegalArgumentException();
044: }
045:
046: // calculate the severity prefix and return value
047: String severity = null;
048: boolean retVal = false;
049: switch (event.getSeverity()) {
050: case ValidationEvent.WARNING:
051: severity = Messages.format(Messages.WARNING);
052: retVal = true; // continue after warnings
053: break;
054: case ValidationEvent.ERROR:
055: severity = Messages.format(Messages.ERROR);
056: retVal = false; // terminate after errors
057: break;
058: case ValidationEvent.FATAL_ERROR:
059: severity = Messages.format(Messages.FATAL_ERROR);
060: retVal = false; // terminate after fatal errors
061: break;
062: default:
063: assert false : Messages
064: .format(Messages.UNRECOGNIZED_SEVERITY, event
065: .getSeverity());
066: }
067:
068: // calculate the location message
069: String location = getLocation(event);
070:
071: System.out.println(Messages.format(Messages.SEVERITY_MESSAGE,
072: severity, event.getMessage(), location));
073:
074: // fail on the first error or fatal error
075: return retVal;
076: }
077:
078: /**
079: * Calculate a location message for the event
080: *
081: */
082: private String getLocation(ValidationEvent event) {
083: StringBuffer msg = new StringBuffer();
084:
085: ValidationEventLocator locator = event.getLocator();
086:
087: if (locator != null) {
088:
089: URL url = locator.getURL();
090: Object obj = locator.getObject();
091: Node node = locator.getNode();
092: int line = locator.getLineNumber();
093:
094: if (url != null || line != -1) {
095: msg.append("line " + line);
096: if (url != null)
097: msg.append(" of " + url);
098: } else if (obj != null) {
099: msg.append(" obj: " + obj.toString());
100: } else if (node != null) {
101: msg.append(" node: " + node.toString());
102: }
103: } else {
104: msg.append(Messages.format(Messages.LOCATION_UNAVAILABLE));
105: }
106:
107: return msg.toString();
108: }
109: }
|