KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > actions > Errors


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common.actions;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32
33 /**
34  *
35  *
36  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
37  */

38 public final class Errors {
39   // --- [Constants] -----------------------------------------------------------
40
// --- [Attributes] ----------------------------------------------------------
41

42   // <field name> -> <Collection of errorMessages>
43
private final Map JavaDoc errors = new HashMap JavaDoc();
44
45
46
47   // --- [Static] --------------------------------------------------------------
48
// --- [Constructors] --------------------------------------------------------
49
// --- [Public] --------------------------------------------------------------
50

51   /**
52    *
53    */

54   public final boolean hasErrors() {
55     return !this.errors.isEmpty();
56   }
57
58   /**
59    *
60    */

61   public final boolean hasErrors(String JavaDoc fieldName) {
62     return this.errors.containsKey(fieldName);
63   }
64
65   /**
66    *
67    */

68   public final Collection JavaDoc getErrors(String JavaDoc fieldName) {
69     return (Collection JavaDoc) this.errors.get(fieldName);
70   }
71
72   /**
73    *
74    */

75   public final Collection JavaDoc getAllErrors() {
76     return (Collection JavaDoc) this.errors.values();
77   }
78
79
80   /**
81    *
82    */

83   public final void addError(String JavaDoc fieldName, String JavaDoc errorMessage) {
84     if(getErrors(fieldName) == null) {
85       this.errors.put(fieldName, new ArrayList JavaDoc());
86     }
87     getErrors(fieldName).add(errorMessage);
88   }
89
90
91
92   // --- [X implementation] ----------------------------------------------------
93
// --- [java.lang.Object Overrides] ------------------------------------------
94

95   /**
96    *
97    */

98   public String JavaDoc toString() {
99     final StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<Errors>[ ");
100     
101     for(Iterator JavaDoc fieldNames = this.errors.keySet().iterator(); fieldNames.hasNext(); ) {
102       final String JavaDoc fieldName = (String JavaDoc) fieldNames.next();
103       final Iterator JavaDoc errorMessages = getErrors(fieldName).iterator();
104
105       sb.append(fieldName + "=> { ");
106       sb.append(toString(errorMessages));
107       sb.append("}" + (fieldNames.hasNext() ? ", " : " "));
108     }
109     sb.append("]");
110     return sb.toString();
111   }
112
113
114
115   // --- [Package protected] ---------------------------------------------------
116
// --- [Private] -------------------------------------------------------------
117

118   /**
119    *
120    */

121   private String JavaDoc toString(Iterator JavaDoc errorMessages) {
122     final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
123
124     while(errorMessages.hasNext()) {
125       final String JavaDoc errorMessage = (String JavaDoc) errorMessages.next();
126       sb.append("\"" + errorMessage + "\"" + (errorMessages.hasNext() ? ", " : " "));
127     }
128     return sb.toString();
129   }
130
131
132
133   // --- [Protected] -----------------------------------------------------------
134
// --- [Inner classes] -------------------------------------------------------
135
}
Popular Tags