KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > FieldValidator


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 package com.raptus.owxv3.api;
7
8
9 import java.util.*;
10 import java.text.*;
11 import com.raptus.owxv3.*;
12 import com.raptus.owxv3.api.components.ComponentConstants;
13
14 import org.apache.struts.util.*;
15
16
17 /**
18  *
19  * <hr>
20  * <table width="100%" border="0">
21  * <tr>
22  * <td width="24%"><b>Filename</b></td><td width="76%">FieldValidator.java</td>
23  * </tr>
24  * <tr>
25  * <td width="24%"><b>Author</b></td><td width="76%">REEA</td>
26  * </tr>
27  * <tr>
28  * <td width="24%"><b>Date</b></td><td width="76%">22th of April 2002</td>
29  * </tr>
30  * </table>
31  * <hr>
32  * <table width="100%" border="0">
33  * <tr>
34  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
35  * </tr>
36  * </table>
37  * <hr>
38  * This class implements validation for fields
39  */

40
41 public class FieldValidator extends Object JavaDoc
42 {
43     /**
44      *Locale used in case of date validator
45      */

46     protected Locale locale;
47     
48     /**
49      *
50      */

51     protected String JavaDoc modifiedValue;
52     
53     /**
54      *
55      */

56     protected String JavaDoc defaultCurrency=null;
57     
58     protected Vector currencyList1=null;
59     protected Vector currencyList2=null;
60     protected Vector currencyList3=null;
61     protected Vector currencyList4=null;
62     
63     /**
64      *Constructor
65      */

66     public FieldValidator(Locale l,MessageResources mr)
67     {
68         locale=l;
69         defaultCurrency=mr.getMessage(l,ComponentConstants.CMP_FIELDS_CURRENCY_DEFAULT);
70         StringTokenizer st=null;
71         int i=0;
72         String JavaDoc s=mr.getMessage(l,ComponentConstants.CMP_FIELDS_CURRENCY_1);
73         
74         if( s!=null && !s.equals("") )
75         {
76             st=new StringTokenizer(s,",");
77             currencyList1=new Vector(st.countTokens());
78             while(st.hasMoreTokens())
79             {
80                 currencyList1.add(st.nextToken().trim());
81             }//end while
82
}//end if
83

84         s=mr.getMessage(l,ComponentConstants.CMP_FIELDS_CURRENCY_2);
85         
86         if( s!=null && !s.equals("") )
87         {
88             st=new StringTokenizer(s,",");
89             currencyList2=new Vector(st.countTokens());
90             while(st.hasMoreTokens())
91             {
92                 currencyList2.add(st.nextToken().trim());
93             }//end while
94
}//end if
95

96         s=mr.getMessage(l,ComponentConstants.CMP_FIELDS_CURRENCY_3);
97         
98         if( s!=null && !s.equals("") )
99         {
100             st=new StringTokenizer(s,",");
101             currencyList3=new Vector( st.countTokens() );
102             while(st.hasMoreTokens())
103             {
104                 currencyList3.add( st.nextToken().trim() );
105             }//end while
106
}//end if
107

108         s=mr.getMessage(l,ComponentConstants.CMP_FIELDS_CURRENCY_4);
109         if( s!=null && !s.equals("") )
110         {
111             st=new StringTokenizer(s,",");
112             currencyList4=new Vector( st.countTokens() );
113             while(st.hasMoreTokens())
114             {
115                 currencyList4.add(st.nextToken().trim());
116             }//end while
117
}//end if
118

119     }
120     
121     /**
122      *Validate method returns true if field is valid false otherwise
123      */

124     public boolean validate(int fieldType, String JavaDoc fieldValue)
125     {
126         modifiedValue=fieldValue;
127         if(fieldType==Constants.FIELDTYPE_NUMBER)
128         {
129             return validateNumber(fieldValue);
130         }
131         else if(fieldType==Constants.FIELDTYPE_TEXT)
132         {
133             return validateText(fieldValue);
134         }
135         else if(fieldType==Constants.FIELDTYPE_PRICE)
136         {
137             return validatePrice(fieldValue);
138         }
139         else if(fieldType==Constants.FIELDTYPE_DATE)
140         {
141             return validateDate(fieldValue);
142         }
143         
144         //unknown type
145

146         return true;
147     }
148     
149     /**
150      *Method for validating a number
151      */

152     public boolean validateNumber(String JavaDoc value)
153     {
154         try
155         {
156             double d=Double.parseDouble(value);
157         }
158         catch(NumberFormatException JavaDoc e)
159         {
160             return false;
161         }
162         return true;
163     }//end validateNumber
164

165
166     /**
167      *Valdiate text, just return true this time
168      */

169     public boolean validateText(String JavaDoc value)
170     {
171         return true;
172     }
173
174     /**
175      *Validate price
176      */

177     public boolean validatePrice(String JavaDoc value)
178     {
179         StringTokenizer st=new StringTokenizer(value);
180         if(! st.hasMoreTokens()) return false;
181
182         String JavaDoc token1=st.nextToken();
183         String JavaDoc token2=null;
184         if(st.hasMoreTokens())
185         {
186             token2=st.nextToken();
187         }
188         Double JavaDoc number=null;
189         String JavaDoc currency=null;
190         String JavaDoc numberpart=null;
191         //presume token1 is number
192
if(token1.endsWith(".-"))
193         {
194             numberpart=token1.substring(0,token1.length()-2)+".00";
195         }
196         if(token1.equals("0"))
197         {
198             numberpart = "0.0";
199         }
200         else
201         {
202             numberpart=token1;
203         }
204         try
205         {
206             number=new Double JavaDoc(numberpart);
207 // 20021217/acuix: q&d need possibility to input zero or minus zero values
208
// if(number.doubleValue()<=0)
209
// {
210
//first token is number but it is <=0
211
// return false;
212
// }
213
}
214         catch(NumberFormatException JavaDoc e)
215         {
216         }
217
218         if(number==null)
219         {
220             //first token NOT number, than it should be currency
221
String JavaDoc valid=validateCurrency(token1);
222             if(valid==null) return false;
223
224             //first token valid currency, second token should be number
225
if(token2==null) return false;
226             
227             if(token2.endsWith(".-"))
228             {
229                 numberpart=token2.substring(0,token2.length()-2)+".00";
230             }
231             else
232             {
233                 numberpart=token2;
234             }
235             try
236             {
237                 number=new Double JavaDoc(numberpart);
238 // if(number.doubleValue()<=0)
239
// {
240
//second token is number but it is <=0
241
// return false;
242
// }
243
}
244             catch(NumberFormatException JavaDoc e)
245             {
246                 //secnod token is not valid number
247
return false;
248             }
249             numberpart=number.toString();
250             if(numberpart.indexOf('.')>0 && numberpart.indexOf('.')<numberpart.length()-3)
251             {
252                 modifiedValue=valid+" "+numberpart.substring(0,numberpart.indexOf('.')+3);
253             }
254             else
255             {
256                 modifiedValue=valid+" "+numberpart;
257             }
258         }
259         else
260         {
261             //first token valid NUMBER
262
if(token2==null)
263             {
264                 //no more tokens use default currency
265
numberpart=number.toString();
266                 if(numberpart.indexOf('.') > 0 && numberpart.indexOf('.') < numberpart.length() - 3)
267                 {
268                     modifiedValue=defaultCurrency + " " + numberpart.substring(0,numberpart.indexOf('.') + 3);
269                 }
270                 else
271                 {
272                    modifiedValue = defaultCurrency + " " + numberpart;
273                 }
274             }//end token2==null
275
else
276             {
277                 //token2 should be valid currency
278
String JavaDoc valid=validateCurrency(token2);
279                 if(valid==null) return false;
280
281                 //token2 valid currency
282
numberpart=number.toString();
283                 if(numberpart.indexOf('.')>0 && numberpart.indexOf('.')<numberpart.length()-3)
284                 {
285                     modifiedValue=valid+" "+numberpart.substring(0,numberpart.indexOf('.')+3);
286                 }
287                 else
288                 {
289                     modifiedValue=valid+" "+numberpart;
290                 }
291             }
292             
293         }//end big else
294

295         return true;
296     }
297     
298     /**
299      *Validate currency, return currency to be used to display
300      *or null if invalid currency
301      */

302     public String JavaDoc validateCurrency(String JavaDoc value)
303     {
304       if(currencyList1!=null)
305       {
306           if(currencyList1.contains(value)) return (String JavaDoc)currencyList1.elementAt(0);
307       }
308       if(currencyList2!=null)
309       {
310           if(currencyList2.contains(value)) return (String JavaDoc)currencyList2.elementAt(0);
311       }
312       if(currencyList3!=null)
313       {
314           if(currencyList3.contains(value)) return (String JavaDoc)currencyList3.elementAt(0);
315       }
316       if(currencyList4!=null)
317       {
318           if(currencyList4.contains(value)) return (String JavaDoc)currencyList4.elementAt(0);
319       }
320       return null;
321     }
322     
323     /**
324      *Validates date
325      */

326     public boolean validateDate(String JavaDoc value)
327     {
328         SimpleDateFormat df=(SimpleDateFormat)DateFormat.getDateInstance(DateFormat.SHORT,locale);
329         df.applyPattern(Constants.DEFAULT_DATEPATTERN);
330         
331         try
332         {
333             df.parse(value);
334         }catch(ParseException e)
335         {
336             return false;
337         }
338         
339         return true;
340     }
341     
342     public String JavaDoc getModifiedVlue()
343     {
344         return modifiedValue;
345     }
346     
347 }
348
Popular Tags