001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval;
013:
014: import java.io.IOException;
015: import java.io.Serializable;
016: import java.util.List;
017:
018: import net.sf.oval.context.OValContext;
019: import net.sf.oval.internal.Log;
020:
021: /**
022: * An instance of this class provides detailed information about a single constraint violation
023: * that occured during validation.
024: *
025: * @author Sebastian Thomschke
026: */
027: public class ConstraintViolation implements Serializable {
028: private final static Log LOG = Log
029: .getLog(ConstraintViolation.class);
030:
031: private final static long serialVersionUID = 1L;
032:
033: private final ConstraintViolation[] causes;
034: private final OValContext context;
035: private final String errorCode;
036: private final String message;
037: private final int severity;
038:
039: private transient Object validatedObject;
040: private transient Object invalidValue;
041:
042: public ConstraintViolation(final String errorCode,
043: final String message, final int severity,
044: final Object validatedObject, final Object invalidValue,
045: final OValContext context) {
046: this .errorCode = errorCode;
047: this .message = message;
048: this .severity = severity;
049: this .validatedObject = validatedObject;
050: this .invalidValue = invalidValue;
051: this .context = context;
052: causes = null;
053: }
054:
055: public ConstraintViolation(final String errorCode,
056: final String message, final int severity,
057: final Object validatedObject, final Object invalidValue,
058: final OValContext context,
059: final ConstraintViolation... causes) {
060: this .errorCode = errorCode;
061: this .message = message;
062: this .severity = severity;
063: this .validatedObject = validatedObject;
064: this .invalidValue = invalidValue;
065: this .context = context;
066: this .causes = causes;
067: }
068:
069: public ConstraintViolation(final String errorCode,
070: final String message, final int severity,
071: final Object validatedObject, final Object invalidValue,
072: final OValContext context,
073: final List<ConstraintViolation> causes) {
074: this .errorCode = errorCode;
075: this .message = message;
076: this .severity = severity;
077: this .validatedObject = validatedObject;
078: this .invalidValue = invalidValue;
079: this .context = context;
080: this .causes = causes.toArray(new ConstraintViolation[causes
081: .size()]);
082: }
083:
084: /**
085: * @return the causes
086: */
087: public ConstraintViolation[] getCauses() {
088: return causes.clone();
089: }
090:
091: /**
092: * @return Returns the context where the constraint violation occured.
093: */
094: public OValContext getContext() {
095: return context;
096: }
097:
098: /**
099: * @return the error code
100: */
101: public String getErrorCode() {
102: return errorCode;
103: }
104:
105: /**
106: * @return Returns the value that was validated.
107: */
108: public Object getInvalidValue() {
109: return invalidValue;
110: }
111:
112: /**
113: * @return the message
114: */
115: public String getMessage() {
116: return message;
117: }
118:
119: /**
120: * @return the severity
121: */
122: public int getSeverity() {
123: return severity;
124: }
125:
126: /**
127: * @return the validatedObject
128: */
129: public Object getValidatedObject() {
130: return validatedObject;
131: }
132:
133: /**
134: * see http://java.sun.com/developer/technicalArticles/ALT/serialization/
135: *
136: * @param in
137: * @throws IOException
138: * @throws ClassNotFoundException
139: */
140: private void readObject(final java.io.ObjectInputStream in)
141: throws IOException, ClassNotFoundException {
142: in.defaultReadObject();
143: if (in.readBoolean()) {
144: validatedObject = in.readObject();
145: }
146: if (in.readBoolean()) {
147: invalidValue = in.readObject();
148: }
149: }
150:
151: @Override
152: public String toString() {
153: return getClass().getName() + ": " + message;
154: }
155:
156: /**
157: * see http://java.sun.com/developer/technicalArticles/ALT/serialization/
158: *
159: * @param out
160: * @throws IOException
161: */
162: private synchronized void writeObject(
163: final java.io.ObjectOutputStream out) throws IOException {
164: out.defaultWriteObject();
165: if (validatedObject instanceof Serializable) {
166: // indicate validatedObject implements Serializable
167: out.writeBoolean(true);
168: out.writeObject(validatedObject);
169: } else {
170: LOG
171: .warn("Field 'validatedObject' not serialized because the field value object "
172: + validatedObject
173: + " of type "
174: + invalidValue.getClass()
175: + " does not implement "
176: + Serializable.class.getName());
177:
178: // indicate validatedObject does not implement Serializable
179: out.writeBoolean(false);
180: }
181:
182: if (invalidValue instanceof Serializable) {
183: // indicate value implements Serializable
184: out.writeBoolean(true);
185: out.writeObject(invalidValue);
186: } else {
187: LOG
188: .warn(
189: "Field 'invalidValue' could not be serialized because the field value object {} does not implement java.io.Serializable.",
190: invalidValue);
191: // indicate value does not implement Serializable
192: out.writeBoolean(false);
193: }
194: }
195: }
|