KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > validators > StringValidator


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.util.validators;
25
26 //import com.icl.saxon.sort.LowercaseFirstComparer;
27

28 import org.infoglue.cms.exception.ConstraintException;
29 import org.infoglue.cms.exception.SystemException;
30 import org.infoglue.cms.util.ConstraintExceptionBuffer;
31 import org.infoglue.cms.util.RegexpHelper;
32
33 /**
34  * <todo>
35  * Time is running out, the illusion fades away...
36  * This package will be refactored/extended after iteration 1.
37  * - move to com.holigrow.yoda.util.validators ?
38  * - interfaces + factory
39  * - constructor madness (setXXX instead of N constructors?)
40  * - more validators
41  * - more fun
42  * </todo>
43  *
44  * @@author <a HREF="meat_for_the_butcher@@yahoo.com">Patrik Nyborg</a>
45  */

46 public class StringValidator extends AbstractValidator {
47   // --- [Constants] -----------------------------------------------------------
48

49   // error codes
50
private static final String JavaDoc INVALID_LENGTH_ERROR_CODE = "301";
51
52
53
54   // --- [Attributes] ----------------------------------------------------------
55

56   //
57
private Range range;
58   //
59
private String JavaDoc pattern;
60   //
61
private String JavaDoc patternErrorCode;
62
63   
64
65   // --- [Static] --------------------------------------------------------------
66
// --- [Constructors] --------------------------------------------------------
67

68   /**
69    * <todo>remove?</todo>
70    */

71   public StringValidator(String JavaDoc fieldName) {
72     this(fieldName, true);
73   }
74
75   /**
76    * <todo>remove?</todo>
77    */

78   public StringValidator(String JavaDoc fieldName, boolean isRequired) {
79     this(fieldName, isRequired, new Range());
80   }
81
82   /**
83    *
84    */

85   public StringValidator(String JavaDoc fieldName, boolean isRequired, int upperLengthLimit) {
86     this(fieldName, isRequired, 0, upperLengthLimit);
87   }
88
89   /**
90    *
91    */

92   public StringValidator(String JavaDoc fieldName, boolean isRequired, int lowerLengthLimit, int upperLengthLimit) {
93     this(fieldName, isRequired, new Range(lowerLengthLimit, upperLengthLimit));
94   }
95
96     /**
97      * Constructor for StringValidator.
98      * @param fieldName
99      * @param isRequired
100      * @param mustBeUnique
101      * @param objectClass
102      */

103     public StringValidator(String JavaDoc fieldName,boolean isRequired,boolean mustBeUnique,Class JavaDoc objectClass, Integer JavaDoc excludeId, Object JavaDoc excludedObject)
104     {
105         super(fieldName, isRequired, mustBeUnique, objectClass, excludeId, excludedObject);
106         this.range = new Range();
107     }
108     public StringValidator(String JavaDoc fieldName,boolean isRequired, int lowerLengthLimit, int upperLengthLimit,boolean mustBeUnique,Class JavaDoc objectClass, Integer JavaDoc excludeId, Object JavaDoc excludedObject)
109     {
110         super(fieldName, isRequired, mustBeUnique, objectClass, excludeId, excludedObject);
111         this.range = new Range(lowerLengthLimit, upperLengthLimit);
112     }
113
114   /**
115    *
116    */

117   public StringValidator(String JavaDoc fieldName, boolean isRequired, Range range) {
118     super(fieldName, isRequired);
119     this.range = range;
120   }
121
122
123
124
125   // --- [Public] --------------------------------------------------------------
126

127   /**
128    *
129    */

130   public void validate(String JavaDoc value) throws ConstraintException {
131     validateIsRequired(value);
132     if(value == null) { // no validation needed + no need for further null checking
133
return;
134     }
135     validateLength(value.trim());
136     validatePattern(value.trim());
137
138     // ss
139
try {
140         validateUniqueness(value);
141     } catch (SystemException e) {
142     }
143     
144     
145     failIfAnyExceptionsFound();
146   }
147
148   /**
149    */

150   public void validate(String JavaDoc value, ConstraintExceptionBuffer ceb) {
151     try {
152       validate(value);
153     } catch(ConstraintException e) {
154       ceb.add(e);
155     }
156   }
157
158   /**
159    *
160    */

161   private void validateLength(String JavaDoc value) {
162     if(!this.range.isWithinLimits(value.length())) {
163       addConstraintException(INVALID_LENGTH_ERROR_CODE);
164     }
165   }
166
167   /**
168    *
169    */

170   private void validatePattern(String JavaDoc value) {
171     if(this.pattern != null && !RegexpHelper.match(this.pattern, value)) {
172       addConstraintException(this.patternErrorCode);
173     }
174   }
175
176
177
178   // --- [X implementation] ----------------------------------------------------
179
// --- [AbstractValidator Overrides] -----------------------------------------
180

181   /**
182    *
183    */

184   protected void validateIsRequired(Object JavaDoc value) throws ConstraintException {
185     super.validateIsRequired(value);
186     if(value != null) {
187       // <todo>this should be safe but add a Bug test anyway?</todo>
188
String JavaDoc stringValue = (String JavaDoc) value;
189       if(stringValue.trim().length() == 0) {
190         super.validateIsRequired(null);
191       }
192     }
193   }
194
195
196
197   // --- [Package protected] ---------------------------------------------------
198
// --- [Private] -------------------------------------------------------------
199
// --- [Protected] -----------------------------------------------------------
200

201   /**
202    *
203    */

204   protected void initializePattern(String JavaDoc pattern, String JavaDoc patternErrorCode) {
205     this.pattern = pattern;
206     this.patternErrorCode = patternErrorCode;
207   }
208
209
210
211   // --- [Inner classes] -------------------------------------------------------
212

213
214   // --- [Setters / Getters] -------------------------------------------------------
215

216     /**
217      * Returns the range.
218      * @return Range
219      */

220     public Range getRange()
221     {
222         return range;
223     }
224     
225     /**
226      * Sets the range.
227      * @param range The range to set
228      */

229     public void setRange(Range range)
230     {
231         this.range = range;
232     }
233
234 }
Popular Tags