1 19 20 package com.sslexplorer.input.validators; 21 22 import java.util.Properties ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 27 import com.sslexplorer.boot.CodedException; 28 import com.sslexplorer.boot.PropertyDefinition; 29 import com.sslexplorer.boot.PropertyValidator; 30 import com.sslexplorer.boot.Util; 31 import com.sslexplorer.core.CoreException; 32 33 51 public class StringValidator implements PropertyValidator { 52 53 final static Log log = LogFactory.getLog(IntegerValidator.class); 54 55 private int minLength = 0; 56 private int maxLength = 255; 57 private String regExp = "", pattern = ""; 58 private boolean trim; 59 60 protected int regExpErrCode = ErrorConstants.ERR_STRING_DOESNT_MATCH_REGEXP; 62 63 72 public StringValidator(int minLength, int maxLength, String regExp, String pattern, boolean trim) { 73 this.minLength = minLength; 74 this.maxLength = maxLength; 75 this.regExp = regExp; 76 this.pattern = pattern; 77 this.trim = trim; 78 } 79 80 84 public StringValidator() { 85 } 86 87 93 public void validate(PropertyDefinition definition, String value, Properties properties) throws CodedException { 94 if ("true".equalsIgnoreCase(properties == null ? "true" : properties.getProperty("trim", String.valueOf(trim)))) { 96 value = value.trim(); 97 } 98 99 int min = minLength; 101 try { 102 if (properties != null && properties.containsKey("minLength")) 103 min = Integer.parseInt(properties.getProperty("minLength")); 104 } catch (NumberFormatException nfe) { 105 log.error("Failed to get minimum value for validator.", nfe); 106 throw new CoreException(ErrorConstants.ERR_INTERNAL_ERROR, 107 ErrorConstants.CATEGORY_NAME, 108 ErrorConstants.BUNDLE_NAME, 109 null, 110 value); 111 } 112 int max = maxLength; 113 try { 114 if (properties != null && properties.containsKey("maxLength")) 115 max = Integer.parseInt(properties.getProperty("maxLength")); 116 } catch (NumberFormatException nfe) { 117 log.error("Failed to get maximum value for validator.", nfe); 118 throw new CoreException(ErrorConstants.ERR_INTERNAL_ERROR, 119 ErrorConstants.CATEGORY_NAME, 120 ErrorConstants.BUNDLE_NAME, 121 null, 122 value); 123 } 124 125 if (value.length() < min) { 127 throw new CoreException(ErrorConstants.ERR_STRING_TOO_SHORT, 128 ErrorConstants.CATEGORY_NAME, 129 ErrorConstants.BUNDLE_NAME, 130 null, 131 String.valueOf(min), 132 String.valueOf(max), 133 value, 134 null); 135 } 136 if (value.length() > max) { 137 throw new CoreException(ErrorConstants.ERR_STRING_TOO_LONG, 138 ErrorConstants.CATEGORY_NAME, 139 ErrorConstants.BUNDLE_NAME, 140 null, 141 String.valueOf(min), 142 String.valueOf(max), 143 value, 144 null); 145 } 146 147 String regExp = properties == null ? this.regExp : Util.trimmedOrBlank(properties.getProperty("regExp", this.regExp)); 149 if (regExp != null && !regExp.equals("") && !value.matches(regExp)) { 150 throw new CoreException(regExpErrCode, 151 ErrorConstants.CATEGORY_NAME, 152 ErrorConstants.BUNDLE_NAME, 153 null, 154 String.valueOf(regExp), 155 value, 156 null, 157 null); 158 159 } 160 161 String pattern = Util.trimmedOrBlank(properties == null ? this.pattern : properties.getProperty("pattern", this.pattern)); 163 if (!pattern.equals("")) { 164 pattern = Util.parseSimplePatternToRegExp(pattern); 165 if(!value.matches(pattern)) { 166 throw new CoreException(ErrorConstants.ERR_STRING_DOESNT_MATCH_SIMPLE_PATTERN, 167 ErrorConstants.CATEGORY_NAME, 168 ErrorConstants.BUNDLE_NAME, 169 null, 170 String.valueOf(pattern), 171 value, 172 null, 173 null); 174 } 175 } 176 } 177 178 } 179 | Popular Tags |