1 23 24 package org.infoglue.cms.util.validators; 25 26 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 46 public class StringValidator extends AbstractValidator { 47 49 private static final String INVALID_LENGTH_ERROR_CODE = "301"; 51 52 53 54 56 private Range range; 58 private String pattern; 60 private String patternErrorCode; 62 63 64 65 68 71 public StringValidator(String fieldName) { 72 this(fieldName, true); 73 } 74 75 78 public StringValidator(String fieldName, boolean isRequired) { 79 this(fieldName, isRequired, new Range()); 80 } 81 82 85 public StringValidator(String fieldName, boolean isRequired, int upperLengthLimit) { 86 this(fieldName, isRequired, 0, upperLengthLimit); 87 } 88 89 92 public StringValidator(String fieldName, boolean isRequired, int lowerLengthLimit, int upperLengthLimit) { 93 this(fieldName, isRequired, new Range(lowerLengthLimit, upperLengthLimit)); 94 } 95 96 103 public StringValidator(String fieldName,boolean isRequired,boolean mustBeUnique,Class objectClass, Integer excludeId, Object excludedObject) 104 { 105 super(fieldName, isRequired, mustBeUnique, objectClass, excludeId, excludedObject); 106 this.range = new Range(); 107 } 108 public StringValidator(String fieldName,boolean isRequired, int lowerLengthLimit, int upperLengthLimit,boolean mustBeUnique,Class objectClass, Integer excludeId, Object excludedObject) 109 { 110 super(fieldName, isRequired, mustBeUnique, objectClass, excludeId, excludedObject); 111 this.range = new Range(lowerLengthLimit, upperLengthLimit); 112 } 113 114 117 public StringValidator(String fieldName, boolean isRequired, Range range) { 118 super(fieldName, isRequired); 119 this.range = range; 120 } 121 122 123 124 125 127 130 public void validate(String value) throws ConstraintException { 131 validateIsRequired(value); 132 if(value == null) { return; 134 } 135 validateLength(value.trim()); 136 validatePattern(value.trim()); 137 138 try { 140 validateUniqueness(value); 141 } catch (SystemException e) { 142 } 143 144 145 failIfAnyExceptionsFound(); 146 } 147 148 150 public void validate(String value, ConstraintExceptionBuffer ceb) { 151 try { 152 validate(value); 153 } catch(ConstraintException e) { 154 ceb.add(e); 155 } 156 } 157 158 161 private void validateLength(String value) { 162 if(!this.range.isWithinLimits(value.length())) { 163 addConstraintException(INVALID_LENGTH_ERROR_CODE); 164 } 165 } 166 167 170 private void validatePattern(String value) { 171 if(this.pattern != null && !RegexpHelper.match(this.pattern, value)) { 172 addConstraintException(this.patternErrorCode); 173 } 174 } 175 176 177 178 181 184 protected void validateIsRequired(Object value) throws ConstraintException { 185 super.validateIsRequired(value); 186 if(value != null) { 187 String stringValue = (String ) value; 189 if(stringValue.trim().length() == 0) { 190 super.validateIsRequired(null); 191 } 192 } 193 } 194 195 196 197 201 204 protected void initializePattern(String pattern, String patternErrorCode) { 205 this.pattern = pattern; 206 this.patternErrorCode = patternErrorCode; 207 } 208 209 210 211 213 214 216 220 public Range getRange() 221 { 222 return range; 223 } 224 225 229 public void setRange(Range range) 230 { 231 this.range = range; 232 } 233 234 } | Popular Tags |