001: /*
002: * Copyright 2006-2007 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen;
017:
018: import java.util.List;
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: /**
025: * When a validation failure occurs, eventually a ValidationException is
026: * thrown to the caller. This is a checked exception that should be
027: * handled by the caller. The exception contains the list of ValidationFailures
028: * that represent the validations that have failed. Ultimately, the failures
029: * should be used to notify the end user (or system).
030: *
031: * @author Shellman, Dan
032: */
033: public class ValidationException extends Exception {
034: protected List failures = new ArrayList();
035: protected List warnings = new ArrayList();
036: protected Map namedFailures = new HashMap();
037:
038: /**
039: * Default constructor with no pre-defined validation failures.
040: */
041: public ValidationException() {
042: } //end ValidationException()
043:
044: /**
045: * Constructor with pre-defined validation failures.
046: *
047: * @param validationFailures The List of ValidationFailures for this exception.
048: */
049: public ValidationException(List validationFailures) {
050: int count;
051:
052: count = validationFailures.size();
053: for (int i = 0; i < count; i++) {
054: addFailure((ValidationFailure) validationFailures.get(i));
055: }
056: } //end ValidationException()
057:
058: /**
059: * Constructor with pre-defined validation failures and warnings. Each
060: * List must contain ValidationFailure objects.
061: *
062: * @param validationFailures The List of failures for this exception.
063: * @param validationWarnings The List of warnings for this exception.
064: */
065: public ValidationException(List validationFailures,
066: List validationWarnings) {
067: int count;
068:
069: count = validationFailures.size();
070: for (int i = 0; i < count; i++) {
071: addFailure((ValidationFailure) validationFailures.get(i));
072: }
073:
074: count = validationWarnings.size();
075: for (int i = 0; i < count; i++) {
076: addWarning((ValidationFailure) validationWarnings.get(i));
077: }
078: } //end ValidationException()
079:
080: /**
081: * Add a failure to this exception. This failure will be appended
082: * to the end of the internal list of failures.
083: *
084: * @param failure The ValidationFailure to add to this exception.
085: */
086: public void addFailure(ValidationFailure failure) {
087: failures.add(failure);
088: updateNamedFailures(failure);
089: } //and addFailure()
090:
091: /**
092: * Add a warning to this exception. This warning will be appended
093: * to the end of the internal list of warnings.
094: *
095: * @param warning The warning to add to this exception.
096: */
097: public void addWarning(ValidationFailure warning) {
098: warnings.add(warning);
099: updateNamedFailures(warning);
100: } //and addWarning()
101:
102: /**
103: * Retrieves the List of ValidationFailures associated with this exception
104: * that represent the failures.
105: *
106: * @return Returns a List of ValidationFailures.
107: */
108: public List getFailures() {
109: return Collections.unmodifiableList(failures);
110: } //end getFailures()
111:
112: /**
113: * Retrieves the List of ValidationFailures associated with this exception
114: * that represent the warnings.
115: *
116: * @return Returns a List of warnings.
117: */
118: public List getWarnings() {
119: return Collections.unmodifiableList(warnings);
120: } //end getWarnings()
121:
122: /**
123: * Retrieves the List of failures (and warnings) that are associated with
124: * a particular "name." The name is defined in the configuration, and is
125: * associated with a particular use of a validator. This is interesting
126: * when mapping back to some form of presentation layer.
127: *
128: * @param validatorName The name to find failures against.
129: *
130: * @return Returns a List of ValidationFailure objects (or an empty
131: * List if there are none associated with the given name).
132: * Will not return null.
133: */
134: public List getNamedFailures(String validatorName) {
135: List list;
136:
137: list = (List) namedFailures.get(validatorName);
138: if (list == null) {
139: return new ArrayList();
140: }
141:
142: return Collections.unmodifiableList(list);
143: } //end getNamedFailures()
144:
145: /**
146: * Retrieves the list of failure messages (List of String objects).
147: *
148: * @return Returns the List of failure messages (as Strings).
149: */
150: public List getFailureMessages() {
151: List list = new ArrayList();
152:
153: for (int i = 0; i < failures.size(); i++) {
154: list
155: .add(((ValidationFailure) failures.get(i))
156: .getMessage());
157: }
158:
159: return list;
160: } //end getFailureMessages()
161:
162: /**
163: * Retrieves the list of warning messages (List of String objects).
164: *
165: * @return Returns the List of warning messages (as Strings).
166: */
167: public List getWarningMessages() {
168: List list = new ArrayList();
169:
170: for (int i = 0; i < warnings.size(); i++) {
171: list
172: .add(((ValidationFailure) warnings.get(i))
173: .getMessage());
174: }
175:
176: return list;
177: } //end getWarningMessages()
178:
179: private void updateNamedFailures(ValidationFailure theFailure) {
180: List list;
181: String name;
182:
183: name = theFailure.getName();
184: list = (List) namedFailures.get(name);
185: if (list == null) {
186: list = new ArrayList();
187: namedFailures.put(name, list);
188: }
189:
190: list.add(theFailure);
191: } //end updateNamedFailures()
192: }
|