01: package net.sourceforge.jaxor.util;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: /**
08: * This class collects validation messages and their severity levels.
09: */
10: public class Validation {
11: private List _infos = new ArrayList();
12: private List _errors = new ArrayList();
13:
14: public void add(Validation val) {
15: for (Iterator iterator = val.getInfos().iterator(); iterator
16: .hasNext();) {
17: addInfo((String) iterator.next());
18: }
19: for (Iterator iterator = val.getErrors().iterator(); iterator
20: .hasNext();) {
21: addError((String) iterator.next());
22: }
23: }
24:
25: /**
26: * Adds an error to the validation, using the specified key to lookup the
27: * message in the resource bundle.
28: */
29: public void addError(String message) {
30: _errors.add(message);
31: }
32:
33: /**
34: * Adds some info text to the validation, using the specified key to lookup the
35: * message in the resource bundle.
36: */
37: public void addInfo(String message) {
38: _infos.add(message);
39: }
40:
41: /**
42: * Returns true if this validation contains any error messages.
43: */
44: public boolean hasErrors() {
45: return (!_errors.isEmpty());
46: }
47:
48: public List getErrors() {
49: return _errors;
50: }
51:
52: public List getInfos() {
53: return _infos;
54: }
55:
56: /**
57: * Returns the list of messages that have been added to this validation.
58: */
59: public List getMessages() {
60: List messages = new ArrayList(_errors.size() + _infos.size());
61: messages.addAll(_errors);
62: messages.addAll(_infos);
63: return messages;
64: }
65:
66: /**
67: * Returns the level and messages as a string. Should only be used for debugging.
68: */
69: public String toString() {
70: StringBuffer result = new StringBuffer();
71: result.append(toString("INFO: ", _infos));
72: result.append(toString("ERROR: ", _errors));
73: return result.toString();
74: }
75:
76: private String toString(String type, List messages) {
77: StringBuffer buf = new StringBuffer();
78: for (Iterator itr = messages.iterator(); itr.hasNext();) {
79: buf.append(type);
80: buf.append(itr.next());
81: buf.append("\n");
82: }
83: return buf.toString();
84: }
85: }
|