1 10 package org.mmbase.datatypes; 11 12 import java.util.*; 13 14 import java.util.regex.Pattern ; 15 import org.mmbase.bridge.*; 16 import org.mmbase.util.Casting; 17 import org.mmbase.util.logging.*; 18 19 28 public class StringDataType extends ComparableDataType implements LengthDataType { 29 private static final Logger log = Logging.getLoggerInstance(StringDataType.class); 30 31 private static final long serialVersionUID = 1L; 33 protected PatternRestriction patternRestriction = new PatternRestriction(Pattern.compile("(?s)\\A.*\\z")); 34 private boolean isPassword = false; 35 protected AbstractLengthDataType.MinRestriction minLengthRestriction = new AbstractLengthDataType.MinRestriction(this, 0); 36 protected AbstractLengthDataType.MaxRestriction maxLengthRestriction = new AbstractLengthDataType.MaxRestriction(this, Long.MAX_VALUE); 37 38 42 public StringDataType(String name) { 43 super(name, String .class); 44 } 45 46 protected void inheritProperties(BasicDataType origin) { 47 super.inheritProperties(origin); 48 if (origin instanceof StringDataType) { 49 StringDataType dataType = (StringDataType)origin; 50 isPassword = dataType.isPassword(); 51 } 52 } 53 54 public static final Pattern DOUBLE_PATTERN; 55 static { 56 final String Digits = "(\\p{Digit}+)"; 58 final String HexDigits = "(\\p{XDigit}+)"; 59 final String Exp = "[eE][+-]?"+Digits; 62 final String fpRegex = 63 ("[\\x00-\\x20]*"+ "[+-]?(" + "NaN|" + "Infinity|" + 68 78 "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ 80 81 "(\\.("+Digits+")("+Exp+")?)|"+ 83 84 "((" + 86 "(0[xX]" + HexDigits + "(\\.)?)|" + 88 89 "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + 91 92 ")[pP][+-]?" + Digits + "))" + 93 "[fFdD]?))" + 94 "[\\x00-\\x20]*"); 96 DOUBLE_PATTERN = Pattern.compile(fpRegex); 97 } 98 public static final Pattern BOOLEAN_PATTERN = Pattern.compile("\\A(1|0|true|false)\\z"); 99 public static final Pattern INTEGER_PATTERN = Pattern.compile("\\A-?[0-9]+\\z"); 100 public static final Pattern NON_NEGATIVE_INTEGER_PATTERN = Pattern.compile("\\A[0-9]+\\z"); 101 public static final Pattern LONG_PATTERN = INTEGER_PATTERN; 102 103 protected void inheritRestrictions(BasicDataType origin) { 104 super.inheritRestrictions(origin); 105 if (origin instanceof StringDataType) { 106 StringDataType dataType = (StringDataType)origin; 107 patternRestriction.inherit(dataType.patternRestriction); 108 minLengthRestriction.inherit(dataType.minLengthRestriction); 109 maxLengthRestriction.inherit(dataType.maxLengthRestriction); 110 } else if (origin instanceof BooleanDataType) { 111 patternRestriction.setValue(BOOLEAN_PATTERN); 112 } else if (origin instanceof IntegerDataType) { 113 PatternRestriction parent = new PatternRestriction(INTEGER_PATTERN); 114 parent.setEnforceStrength(ENFORCE_ABSOLUTE); 115 patternRestriction = new PatternRestriction( parent); 116 } else if (origin instanceof LongDataType) { 117 PatternRestriction parent = new PatternRestriction(LONG_PATTERN); 118 parent.setEnforceStrength(ENFORCE_ABSOLUTE); 119 patternRestriction = new PatternRestriction( parent); 120 } else if (origin instanceof FloatDataType) { 121 PatternRestriction parent = new PatternRestriction(DOUBLE_PATTERN); 122 parent.setEnforceStrength(ENFORCE_ABSOLUTE); 123 patternRestriction = new PatternRestriction( parent); 124 } else if (origin instanceof DoubleDataType) { 125 PatternRestriction parent = new PatternRestriction(DOUBLE_PATTERN); 126 parent.setEnforceStrength(ENFORCE_ABSOLUTE); 127 patternRestriction = new PatternRestriction( parent); 128 } 129 if (origin instanceof NumberDataType) { 130 setMin(null, true); 133 setMax(null, true); 134 } 135 } 136 137 protected void cloneRestrictions(BasicDataType origin) { 138 super.cloneRestrictions(origin); 139 if (origin instanceof StringDataType) { 140 StringDataType dataType = (StringDataType)origin; 141 patternRestriction = new PatternRestriction(dataType.patternRestriction); 142 minLengthRestriction = new AbstractLengthDataType.MinRestriction(this, dataType.minLengthRestriction); 143 maxLengthRestriction = new AbstractLengthDataType.MaxRestriction(this, dataType.maxLengthRestriction); 144 } 145 } 146 147 public long getLength(Object value) { 148 return ((String ) value).length(); 149 } 150 153 public long getMinLength() { 154 return Casting.toLong(minLengthRestriction.getValue()); 155 } 156 157 160 public DataType.Restriction getMinLengthRestriction() { 161 return minLengthRestriction; 162 } 163 164 167 public void setMinLength(long value) { 168 getMinLengthRestriction().setValue(new Long (value)); 169 } 170 171 174 public long getMaxLength() { 175 return Casting.toLong(getMaxLengthRestriction().getValue()); 176 } 177 178 181 public DataType.Restriction getMaxLengthRestriction() { 182 return maxLengthRestriction; 183 } 184 187 public void setMaxLength(long value) { 188 getMaxLengthRestriction().setValue(new Long (value)); 189 } 190 191 192 193 197 public Pattern getPattern() { 198 return patternRestriction.getPattern(); 199 } 200 201 205 public DataType.Restriction getPatternRestriction() { 206 return patternRestriction; 207 } 208 209 214 public void setPattern(Pattern value) { 215 getPatternRestriction().setValue(value); 216 } 217 218 219 223 public boolean isPassword() { 224 return isPassword; 225 } 226 public void setPassword(boolean pw) { 227 edit(); 228 isPassword = pw; 229 } 230 231 public void toXml(org.w3c.dom.Element parent) { 232 super.toXml(parent); 233 getElement(parent, "minLength", "description,class,property,default,unique,required,(minInclusive|minExclusive),(maxIncluse|maxExclusive),minLength").setAttribute("value", Casting.toString(minLengthRestriction.getValue())); 234 getElement(parent, "maxLength", "description,class,property,default,unique,required,(minInclusive|minExclusive),(maxIncluse|maxExclusive),minLength,maxLength").setAttribute("value", Casting.toString(maxLengthRestriction.getValue())); 235 getElement(parent, "pattern", "description,class,property,default,unique,required,(minInclusive|minExclusive),(maxIncluse|maxExclusive),minLength,maxLength,length,pattern").setAttribute("value", Casting.toString(patternRestriction.getPattern().pattern())); 236 237 } 238 239 protected Collection validateCastValue(Collection errors, Object castValue, Object value, Node node, Field field) { 240 errors = super.validateCastValue(errors, castValue, value, node, field); 241 errors = patternRestriction.validate(errors, castValue, node, field); 242 errors = minLengthRestriction.validate(errors, castValue, node, field); 243 errors = maxLengthRestriction.validate(errors, castValue, node, field); 244 return errors; 245 } 246 247 protected StringBuffer toStringBuffer() { 248 StringBuffer buf = super.toStringBuffer(); 249 Pattern p = getPattern(); 250 if (p != null && ! (p.pattern().equals(".*"))) { 251 buf.append(" pattern:").append(p.pattern()); 252 } 253 if (isPassword()) { 254 buf.append(" password"); 255 } 256 return buf; 257 } 258 259 protected class PatternRestriction extends AbstractRestriction { 260 PatternRestriction(PatternRestriction source) { 261 super(source); 262 } 263 PatternRestriction(Pattern v) { 264 super("pattern", v); 265 } 266 Pattern getPattern() { 267 return (Pattern ) value; 268 } 269 protected boolean simpleValid(Object v, Node node, Field field) { 270 String s = Casting.toString(v); 271 boolean res = value == null ? true : getPattern().matcher(s).matches(); 272 return res; 274 } 275 } 276 277 public static void main(String [] argv) { 278 Pattern p = Pattern.compile(argv[0]); 279 System.out.println(p.matcher(argv[1]).matches()); 280 } 281 282 } 283 | Popular Tags |