KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > MessageString


1 /*
2  * $Id: MessageString.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.TreeSet JavaDoc;
33
34 /**
35  * Contains extra information about Messages
36  *
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 3.1
40  */

41 public class MessageString {
42     
43     public static final String JavaDoc module = MessageString.class.getName();
44     
45     protected String JavaDoc message;
46     protected String JavaDoc fieldName;
47     protected String JavaDoc toFieldName;
48     protected Throwable JavaDoc sourceError;
49     protected Locale JavaDoc locale;
50     protected String JavaDoc propertyResource;
51     protected String JavaDoc propertyName;
52     protected boolean isError = true;
53     
54     public static List JavaDoc getMessagesForField(String JavaDoc fieldName, boolean convertToStrings, List JavaDoc messageStringList) {
55         if (fieldName == null) {
56             return Collections.EMPTY_LIST;
57         }
58         Set JavaDoc fieldSet = new TreeSet JavaDoc();
59         fieldSet.add(fieldName);
60         return getMessagesForField(fieldSet, convertToStrings, messageStringList);
61     }
62     public static List JavaDoc getMessagesForField(String JavaDoc fieldName1, String JavaDoc fieldName2, String JavaDoc fieldName3, String JavaDoc fieldName4, boolean convertToStrings, List JavaDoc messageStringList) {
63         Set JavaDoc fieldSet = new TreeSet JavaDoc();
64         if (fieldName1 != null && fieldName1.length() > 0) fieldSet.add(fieldName1);
65         if (fieldName2 != null && fieldName2.length() > 0) fieldSet.add(fieldName2);
66         if (fieldName3 != null && fieldName3.length() > 0) fieldSet.add(fieldName3);
67         if (fieldName4 != null && fieldName4.length() > 0) fieldSet.add(fieldName4);
68         return getMessagesForField(fieldSet, convertToStrings, messageStringList);
69     }
70     public static List JavaDoc getMessagesForField(Set JavaDoc fieldNameSet, boolean convertToStrings, List JavaDoc messageStringList) {
71         if (messageStringList == null || fieldNameSet == null || fieldNameSet.size() == 0) {
72             return Collections.EMPTY_LIST;
73         }
74         List JavaDoc outList = new ArrayList JavaDoc(messageStringList.size());
75         Iterator JavaDoc messageStringIter = messageStringList.iterator();
76         while (messageStringIter.hasNext()) {
77             Object JavaDoc messageStringCur = messageStringIter.next();
78             if (messageStringCur instanceof MessageString) {
79                 MessageString messageString = (MessageString) messageStringCur;
80                 if (messageString.isForField(fieldNameSet)) {
81                     if (convertToStrings) {
82                         outList.add(messageString.toString());
83                     } else {
84                         outList.add(messageString);
85                     }
86                 }
87             } else {
88                 // not a MessageString, don't know if it is for this field so skip it
89
continue;
90             }
91         }
92         return outList;
93     }
94     
95     /**
96      * @param message
97      * @param fieldName
98      * @param locale
99      * @param propertyResource
100      * @param propertyName
101      */

102     public MessageString(String JavaDoc message, String JavaDoc fieldName, String JavaDoc propertyResource, String JavaDoc propertyName, Locale JavaDoc locale, boolean isError) {
103         this.message = message;
104         this.fieldName = fieldName;
105         this.locale = locale;
106         this.propertyResource = propertyResource;
107         this.propertyName = propertyName;
108         this.isError = isError;
109     }
110     /**
111      * @param message
112      * @param fieldName
113      */

114     public MessageString(String JavaDoc message, String JavaDoc fieldName, boolean isError) {
115         this.message = message;
116         this.fieldName = fieldName;
117         this.isError = isError;
118     }
119     /**
120      * @param message
121      * @param fieldName
122      * @param toFieldName
123      * @param sourceError
124      */

125     public MessageString(String JavaDoc message, String JavaDoc fieldName, String JavaDoc toFieldName, Throwable JavaDoc sourceError) {
126         this.message = message;
127         this.fieldName = fieldName;
128         this.toFieldName = toFieldName;
129         this.sourceError = sourceError;
130         this.isError = true;
131     }
132     /**
133      * @param message
134      * @param sourceError
135      */

136     public MessageString(String JavaDoc message, Throwable JavaDoc sourceError) {
137         this.message = message;
138         this.sourceError = sourceError;
139         this.isError = true;
140     }
141     
142     /**
143      * @return Returns the fieldName.
144      */

145     public String JavaDoc getFieldName() {
146         return fieldName;
147     }
148     /**
149      * @param fieldName The fieldName to set.
150      */

151     public void setFieldName(String JavaDoc fieldName) {
152         this.fieldName = fieldName;
153     }
154     public boolean isForField(Set JavaDoc fieldNameSet) {
155         if (fieldNameSet == null) {
156             return true;
157         }
158         return fieldNameSet.contains(this.fieldName);
159     }
160     public boolean isForField(String JavaDoc fieldName) {
161         if (this.fieldName == null) {
162             if (fieldName == null) {
163                 return true;
164             } else {
165                 return false;
166             }
167         } else {
168             return this.fieldName.equals(fieldName);
169         }
170     }
171     
172     /**
173      * @return Returns the message.
174      */

175     public String JavaDoc getMessage() {
176         return message;
177     }
178     /**
179      * @param message The message to set.
180      */

181     public void setMessage(String JavaDoc message) {
182         this.message = message;
183     }
184     /**
185      * @return Returns the sourceError.
186      */

187     public Throwable JavaDoc getSourceError() {
188         return sourceError;
189     }
190     /**
191      * @param sourceError The sourceError to set.
192      */

193     public void setSourceError(Throwable JavaDoc sourceError) {
194         this.sourceError = sourceError;
195     }
196     /**
197      * @return Returns the toFieldName.
198      */

199     public String JavaDoc getToFieldName() {
200         return toFieldName;
201     }
202     /**
203      * @param toFieldName The toFieldName to set.
204      */

205     public void setToFieldName(String JavaDoc toFieldName) {
206         this.toFieldName = toFieldName;
207     }
208
209     /**
210      * @return Returns the locale.
211      */

212     public Locale JavaDoc getLocale() {
213         return locale;
214     }
215     /**
216      * @param locale The locale to set.
217      */

218     public void setLocale(Locale JavaDoc locale) {
219         this.locale = locale;
220     }
221     /**
222      * @return Returns the propertyName.
223      */

224     public String JavaDoc getPropertyName() {
225         return propertyName;
226     }
227     /**
228      * @param propertyName The propertyName to set.
229      */

230     public void setPropertyName(String JavaDoc propertyName) {
231         this.propertyName = propertyName;
232     }
233     /**
234      * @return Returns the propertyResource.
235      */

236     public String JavaDoc getPropertyResource() {
237         return propertyResource;
238     }
239     /**
240      * @param propertyResource The propertyResource to set.
241      */

242     public void setPropertyResource(String JavaDoc propertyResource) {
243         this.propertyResource = propertyResource;
244     }
245     
246     /**
247      * @return Returns the isError.
248      */

249     public boolean isError() {
250         return isError;
251     }
252     /**
253      * @param isError The isError to set.
254      */

255     public void setError(boolean isError) {
256         this.isError = isError;
257     }
258
259     public String JavaDoc toString() {
260         return this.message;
261     }
262 }
263
Popular Tags