001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.util;
032:
033: import com.jgoodies.validation.Severity;
034: import com.jgoodies.validation.ValidationResult;
035: import com.jgoodies.validation.message.PropertyValidationMessage;
036:
037: /**
038: * A utility class that minimizes the effort to create instances
039: * of {@link PropertyValidationMessage} in validation code.
040: * You can use an instance of this class as a member field of your
041: * validator implementation and delegate the message creation to it.
042: *
043: * @author Karsten Lentzsch
044: * @version $Revision: 1.5 $
045: *
046: * @see com.jgoodies.validation.message.PropertyValidationMessage
047: */
048: public final class PropertyValidationSupport {
049:
050: /**
051: * Refers to the {@link ValidationResult} that is used to add messages to
052: * if no individual result is specified.
053: *
054: * @see #add(String, String)
055: * @see #addError(String, String)
056: * @see #addWarning(String, String)
057: * @see #clearResult()
058: */
059: private ValidationResult defaultResult;
060:
061: /**
062: * Holds the severity that is used in the message creation and adder methods
063: * that use no individual severity.
064: *
065: * @see #create(String, String)
066: * @see #add(String, String)
067: * @see #add(ValidationResult, String, String)
068: */
069: private final Severity defaultSeverity;
070:
071: /**
072: * Refers to the object to be validated.
073: */
074: private final Object target;
075:
076: /**
077: * Describes the validation target's role in the outer context.
078: */
079: private final String role;
080:
081: // Instance Creation ******************************************************
082:
083: /**
084: * Constructs a <code>PropertyValidationSupport</code> instance for the
085: * given validation target and its validation role. The default severity
086: * is set to <code>Severity.WARNING</code>.
087: *
088: * @param target the object to be validated
089: * @param role the validation target's role in the outer context
090: *
091: * @throws NullPointerException if the target or role is <code>null</code>
092: */
093: public PropertyValidationSupport(Object target, String role) {
094: this (Severity.WARNING, target, role);
095: }
096:
097: /**
098: * Constructs a <code>PropertyValidationSupport</code> instance for the
099: * given validation target and its validation role.
100: *
101: * @param defaultSeverity the optional <code>Severity</code> used for
102: * message creation when no severity is specified
103: * @param target the object to be validated
104: * @param role the validation target's role in the outer context
105: *
106: * @throws NullPointerException if the target or role is <code>null</code>
107: * @throws IllegalArgumentException if defaultSeverity is <code>Severity.OK</code>
108: */
109: public PropertyValidationSupport(Severity defaultSeverity,
110: Object target, String role) {
111: this (new ValidationResult(), defaultSeverity, target, role);
112: }
113:
114: /**
115: * Constructs a <code>PropertyValidationSupport</code> instance for the
116: * given default result, default severity, validation target and the given
117: * validation role.
118: *
119: * @param defaultResult the optional <code>ValidationResult</code>
120: * that is used to add <code>ValidationMessage</code>s to
121: * @param defaultSeverity the optional <code>Severity</code> used for
122: * message creation when no severity is specified
123: * @param target the object to be validated
124: * @param role the validation target's role in the outer context
125: *
126: * @throws NullPointerException if the target or role is <code>null</code>
127: * @throws IllegalArgumentException if defaultSeverity is <code>Severity.OK</code>
128: */
129: public PropertyValidationSupport(ValidationResult defaultResult,
130: Severity defaultSeverity, Object target, String role) {
131: if (defaultSeverity == Severity.OK)
132: throw new IllegalArgumentException(
133: "Severity.OK must not be used in validation messages.");
134:
135: this .defaultResult = defaultResult;
136: this .defaultSeverity = defaultSeverity;
137: this .target = target;
138: this .role = role;
139: }
140:
141: // Accessing the ValidationResult *****************************************
142:
143: /**
144: * Sets an empty {@link ValidationResult} as default result.
145: * Useful at the begin of a validation sequence.
146: */
147: public void clearResult() {
148: defaultResult = new ValidationResult();
149: }
150:
151: /**
152: * Returns the default {@link ValidationResult}.
153: *
154: * @return the default validation result
155: */
156: public ValidationResult getResult() {
157: return defaultResult;
158: }
159:
160: // Message Creation *******************************************************
161:
162: /**
163: * Creates and returns an error <code>PropertyValidationMessage</code>
164: * for the given property and message text.
165: *
166: * @param property describes the validated property
167: * @param text the message text
168: * @return a <code>PropertyValidationMessage</code> with error severity
169: * for the given property and text
170: */
171: public PropertyValidationMessage createError(String property,
172: String text) {
173: return create(Severity.ERROR, property, text);
174: }
175:
176: /**
177: * Creates and returns a warning <code>PropertyValidationMessage</code>
178: * for the given property and message text.
179: *
180: * @param property describes the validated property
181: * @param text the message text
182: * @return a <code>PropertyValidationMessage</code> with warning severity
183: * for the given property and text
184: */
185: public PropertyValidationMessage createWarning(String property,
186: String text) {
187: return create(Severity.WARNING, property, text);
188: }
189:
190: /**
191: * Creates and returns a <code>PropertyValidationMessage</code>
192: * for the given property and message text using the default severity.
193: *
194: * @param property describes the validated property
195: * @param text the message text
196: * @return a <code>PropertyValidationMessage</code> with default severity
197: * for the given property and text
198: */
199: public PropertyValidationMessage create(String property, String text) {
200: return create(defaultSeverity, property, text);
201: }
202:
203: /**
204: * Creates and returns an error <code>PropertyValidationMessage</code>
205: * for the given property and message text using the specified severity.
206: *
207: * @param severity the <code>Severity</code> to be used
208: * @param property describes the validated property
209: * @param text the message text
210: * @return a <code>PropertyValidationMessage</code> with the specified severity
211: * for the given property and text
212: */
213: public PropertyValidationMessage create(Severity severity,
214: String property, String text) {
215: return new PropertyValidationMessage(severity, text, target,
216: role, property);
217: }
218:
219: // Adding Messages to the Default ValidationResult ************************
220:
221: /**
222: * Adds an error <code>PropertyValidationMessage</code> to this object's
223: * default <code>ValidationResult</code>.
224: * Uses the given property and message text.
225: *
226: * @param property describes the validated property
227: * @param text the message text
228: */
229: public void addError(String property, String text) {
230: addError(defaultResult, property, text);
231: }
232:
233: /**
234: * Adds a warning <code>PropertyValidationMessage</code> to this object's
235: * default <code>ValidationResult</code>.
236: * Uses the given property and message text.
237: *
238: * @param property describes the validated property
239: * @param text the message text
240: */
241: public void addWarning(String property, String text) {
242: addWarning(defaultResult, property, text);
243: }
244:
245: /**
246: * Adds a <code>PropertyValidationMessage</code> to this object's
247: * default <code>ValidationResult</code>.
248: * Uses the default severity and the given property and message text.
249: *
250: * @param property describes the validated property
251: * @param text the message text
252: */
253: public void add(String property, String text) {
254: add(defaultResult, property, text);
255: }
256:
257: /**
258: * Adds a <code>PropertyValidationMessage</code> to this object's
259: * default <code>ValidationResult</code>. Uses the specified
260: * <code>Severity</code> and given property and message text.
261: *
262: * @param severity the <code>Severity</code> to be used
263: * @param property describes the validated property
264: * @param text the message text
265: */
266: public void add(Severity severity, String property, String text) {
267: add(defaultResult, severity, property, text);
268: }
269:
270: // Adding Messages to a Given ValidationResult ****************************
271:
272: /**
273: * Adds an error <code>PropertyValidationMessage</code> to the specified
274: * <code>ValidationResult</code>.
275: * Uses the given property and message text.
276: *
277: * @param result the result the message will be added to
278: * @param property describes the validated property
279: * @param text the message text
280: */
281: public void addError(ValidationResult result, String property,
282: String text) {
283: result.add(createError(property, text));
284: }
285:
286: /**
287: * Adds a warning <code>PropertyValidationMessage</code> to the specified
288: * <code>ValidationResult</code>.
289: * Uses the given property and message text.
290: *
291: * @param result the result the message will be added to
292: * @param property describes the validated property
293: * @param text the message text
294: */
295: public void addWarning(ValidationResult result, String property,
296: String text) {
297: result.add(createWarning(property, text));
298: }
299:
300: /**
301: * Adds a <code>PropertyValidationMessage</code> to the specified
302: * <code>ValidationResult</code>. Uses this object's default severity
303: * and the given property and message text.
304: *
305: * @param result the result the message will be added to
306: * @param property describes the validated property
307: * @param text the message text
308: */
309: public void add(ValidationResult result, String property,
310: String text) {
311: result.add(create(property, text));
312: }
313:
314: /**
315: * Adds a <code>PropertyValidationMessage</code> to the specified
316: * <code>ValidationResult</code>. Uses the specified severity
317: * and the given property and message text.
318: *
319: * @param result the result the message will be added to
320: * @param severity the severity used for the created message
321: * @param property describes the validated property
322: * @param text the message text
323: *
324: * @throws IllegalArgumentException if severity is <code>Severity.OK</code>
325: */
326: public void add(ValidationResult result, Severity severity,
327: String property, String text) {
328: if (severity == Severity.OK)
329: throw new IllegalArgumentException(
330: "Severity.OK must not be used in validation messages.");
331:
332: result.add(create(severity, property, text));
333: }
334:
335: }
|