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 java.text.MessageFormat;
008:
009: import javax.xml.bind.ValidationEvent;
010: import javax.xml.bind.ValidationEventLocator;
011:
012: /**
013: * Default implementation of the ValidationEvent interface.
014: *
015: * <p>
016: * JAXB providers are allowed to use whatever class that implements
017: * the ValidationEvent interface. This class is just provided for a
018: * convenience.
019: *
020: * @author <ul><li>Kohsuke Kawaguchi, Sun Microsystems, Inc.</li></ul>
021: * @version $Revision: 1.1 $
022: * @see javax.xml.bind.Validator
023: * @see javax.xml.bind.ValidationEventHandler
024: * @see javax.xml.bind.ValidationEvent
025: * @see javax.xml.bind.ValidationEventLocator
026: * @since JAXB1.0
027: */
028: public class ValidationEventImpl implements ValidationEvent {
029:
030: /**
031: * Create a new ValidationEventImpl.
032: *
033: * @param _severity The severity value for this event. Must be one of
034: * ValidationEvent.WARNING, ValidationEvent.ERROR, or
035: * ValidationEvent.FATAL_ERROR
036: * @param _message The text message for this event - may be null.
037: * @param _locator The locator object for this event - may be null.
038: * @throws IllegalArgumentException if an illegal severity field is supplied
039: */
040: public ValidationEventImpl(int _severity, String _message,
041: ValidationEventLocator _locator) {
042:
043: this (_severity, _message, _locator, null);
044: }
045:
046: /**
047: * Create a new ValidationEventImpl.
048: *
049: * @param _severity The severity value for this event. Must be one of
050: * ValidationEvent.WARNING, ValidationEvent.ERROR, or
051: * ValidationEvent.FATAL_ERROR
052: * @param _message The text message for this event - may be null.
053: * @param _locator The locator object for this event - may be null.
054: * @param _linkedException An optional linked exception that may provide
055: * additional information about the event - may be null.
056: * @throws IllegalArgumentException if an illegal severity field is supplied
057: */
058: public ValidationEventImpl(int _severity, String _message,
059: ValidationEventLocator _locator, Throwable _linkedException) {
060:
061: setSeverity(_severity);
062: this .message = _message;
063: this .locator = _locator;
064: this .linkedException = _linkedException;
065: }
066:
067: private int severity;
068: private String message;
069: private Throwable linkedException;
070: private ValidationEventLocator locator;
071:
072: public int getSeverity() {
073: return severity;
074: }
075:
076: /**
077: * Set the severity field of this event.
078: *
079: * @param _severity Must be one of ValidationEvent.WARNING,
080: * ValidationEvent.ERROR, or ValidationEvent.FATAL_ERROR.
081: * @throws IllegalArgumentException if an illegal severity field is supplied
082: */
083: public void setSeverity(int _severity) {
084:
085: if (_severity != ValidationEvent.WARNING
086: && _severity != ValidationEvent.ERROR
087: && _severity != ValidationEvent.FATAL_ERROR) {
088: throw new IllegalArgumentException(Messages
089: .format(Messages.ILLEGAL_SEVERITY));
090: }
091:
092: this .severity = _severity;
093: }
094:
095: public String getMessage() {
096: return message;
097: }
098:
099: /**
100: * Set the message field of this event.
101: *
102: * @param _message String message - may be null.
103: */
104: public void setMessage(String _message) {
105: this .message = _message;
106: }
107:
108: public Throwable getLinkedException() {
109: return linkedException;
110: }
111:
112: /**
113: * Set the linked exception field of this event.
114: *
115: * @param _linkedException Optional linked exception - may be null.
116: */
117: public void setLinkedException(Throwable _linkedException) {
118: this .linkedException = _linkedException;
119: }
120:
121: public ValidationEventLocator getLocator() {
122: return locator;
123: }
124:
125: /**
126: * Set the locator object for this event.
127: *
128: * @param _locator The locator - may be null.
129: */
130: public void setLocator(ValidationEventLocator _locator) {
131: this .locator = _locator;
132: }
133:
134: /**
135: * Returns a string representation of this object in a format
136: * helpful to debugging.
137: *
138: * @see Object#equals(Object)
139: */
140: public String toString() {
141: String s;
142: switch (getSeverity()) {
143: case WARNING:
144: s = "WARNING";
145: break;
146: case ERROR:
147: s = "ERROR";
148: break;
149: case FATAL_ERROR:
150: s = "FATAL_ERROR";
151: break;
152: default:
153: s = String.valueOf(getSeverity());
154: break;
155: }
156: return MessageFormat.format(
157: "[severity={0},message={1},locator={2}]", new Object[] {
158: s, getMessage(), getLocator() });
159: }
160: }
|