1 10 package org.mmbase.datatypes; 11 12 import java.util.*; 13 14 import org.mmbase.bridge.*; 15 import org.mmbase.util.Casting; 16 import org.mmbase.util.logging.*; 17 import org.w3c.dom.Element ; 18 19 28 abstract public class AbstractLengthDataType extends BasicDataType implements LengthDataType { 29 private static final Logger log = Logging.getLoggerInstance(LengthDataType.class); 30 31 protected MinRestriction minLengthRestriction = new MinRestriction(this, 0); 32 protected MaxRestriction maxLengthRestriction = new MaxRestriction(this, Long.MAX_VALUE); 33 34 39 public AbstractLengthDataType(String name, Class classType) { 40 super(name, classType); 41 minLengthRestriction.setEnforceStrength(ENFORCE_ABSOLUTE); 42 } 43 44 45 protected void cloneRestrictions(BasicDataType origin) { 46 super.cloneRestrictions(origin); 47 if (origin instanceof AbstractLengthDataType) { 48 AbstractLengthDataType dataType = (AbstractLengthDataType)origin; 49 minLengthRestriction = new MinRestriction(this, dataType.minLengthRestriction); 51 maxLengthRestriction = new MaxRestriction(this, dataType.maxLengthRestriction); 52 } 53 } 54 55 protected void inheritRestrictions(BasicDataType origin) { 56 super.inheritRestrictions(origin); 57 if (origin instanceof AbstractLengthDataType) { 58 AbstractLengthDataType dataType = (AbstractLengthDataType)origin; 59 minLengthRestriction.inherit(dataType.minLengthRestriction); 61 maxLengthRestriction.inherit(dataType.maxLengthRestriction); 62 } 63 } 64 65 68 public abstract long getLength(Object value); 69 70 73 public long getMinLength() { 74 return Casting.toLong(minLengthRestriction.getValue()); 75 } 76 77 80 public DataType.Restriction getMinLengthRestriction() { 81 return minLengthRestriction; 82 } 83 84 87 public void setMinLength(long value) { 88 getMinLengthRestriction().setValue(new Long (value)); 89 } 90 91 94 public long getMaxLength() { 95 return Casting.toLong(getMaxLengthRestriction().getValue()); 96 } 97 98 101 public DataType.Restriction getMaxLengthRestriction() { 102 return maxLengthRestriction; 103 } 104 105 110 public void setMaxLength(long value) { 111 getMaxLengthRestriction().setValue(new Long (value)); 112 } 113 114 protected Collection validateCastValue(Collection errors, Object castValue, Object value, Node node, Field field) { 115 errors = super.validateCastValue(errors, castValue, value, node, field); 116 errors = minLengthRestriction.validate(errors, castValue, node, field); 117 errors = maxLengthRestriction.validate(errors, castValue, node, field); 118 return errors; 119 } 120 121 public void toXml(Element parent) { 122 super.toXml(parent); 123 getElement(parent, "minLength", "description,class,property,default,unique,required,(minInclusive|minExclusive),(maxIncluse|maxExclusive),minLength").setAttribute("value", Casting.toString(minLengthRestriction.getValue())); 124 getElement(parent, "maxLength", "description,class,property,default,unique,required,(minInclusive|minExclusive),(maxIncluse|maxExclusive),minLength,maxLength").setAttribute("value", Casting.toString(maxLengthRestriction.getValue())); 125 126 } 127 128 protected StringBuffer toStringBuffer() { 129 StringBuffer buf = super.toStringBuffer(); 130 if (getMinLength() > 0) { 131 buf.append("minLength:" + getMinLength() + " "); 132 } 133 if (getMaxLength() < Long.MAX_VALUE) { 134 buf.append("maxLength:" + getMaxLength() + " "); 135 } 136 return buf; 137 } 138 139 static class MinRestriction extends StaticAbstractRestriction { 140 MinRestriction(BasicDataType dt, MinRestriction source) { 141 super(dt, source); 142 } 143 144 MinRestriction(BasicDataType dt, long min) { 145 super(dt, "minLength", new Long (min)); 146 } 147 148 protected boolean simpleValid(Object v, Node node, Field field) { 149 if (v == null) return true; long min = Casting.toLong(getValue()); 151 return ((LengthDataType) parent).getLength(v) >= min; 152 } 153 } 154 155 static class MaxRestriction extends StaticAbstractRestriction { 156 MaxRestriction(BasicDataType dt, MaxRestriction source) { 157 super(dt, source); 158 } 159 160 MaxRestriction(BasicDataType dt, long max) { 161 super(dt, "maxLength", new Long (max)); 162 } 163 164 protected boolean simpleValid(Object v, Node node, Field field) { 165 if (v == null) return true; long max = Casting.toLong(getValue()); 167 long length = ((LengthDataType) parent).getLength(v); 168 return length <= max; 169 } 170 } 171 172 } 173 | Popular Tags |