1 package org.apache.slide.projector.descriptor; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import org.apache.slide.projector.Context; 9 import org.apache.slide.projector.i18n.ErrorMessage; 10 import org.apache.slide.projector.value.PrintableValue; 11 import org.apache.slide.projector.value.StringValue; 12 import org.apache.slide.projector.value.Value; 13 14 public class StringValueDescriptor implements ValueDescriptor { 15 public final static String UNSET = "[unset]"; 16 17 public final static StringValueDescriptor ANY = new StringValueDescriptor(); 18 public final static StringValueDescriptor EMPTY = new StringValueDescriptor(0); 19 public final static StringValueDescriptor NOT_EMPTY = new StringValueDescriptor(1,Integer.MAX_VALUE); 20 21 protected boolean constrained; 22 protected List allowedValues = new ArrayList (); 23 protected int minimumLength = -1, maximumLength =-1; 24 protected Value defaultValue; 25 26 public StringValueDescriptor() { 27 this.constrained = false; 28 } 29 30 public StringValueDescriptor(String [] allowedValues) { 31 this.constrained = true; 32 this.allowedValues = new ArrayList (Arrays.asList(allowedValues)); 33 } 34 35 public StringValueDescriptor(int maximumLength) { 36 this.maximumLength = maximumLength; 37 this.constrained = true; 38 } 39 40 public StringValueDescriptor(int minimumLength, int maximumLength) { 41 this.minimumLength = minimumLength; 42 this.maximumLength = maximumLength; 43 this.constrained = true; 44 } 45 46 public int getMinimumLength() { 47 return minimumLength; 48 } 49 50 public void setMinimumLength(int minimumLength) { 51 this.minimumLength = minimumLength; 52 constrained = true; 53 } 54 55 public int getMaximumLength() { 56 return maximumLength; 57 } 58 59 public void setMaximumLength(int maximumLength) { 60 this.maximumLength = maximumLength; 61 constrained = true; 62 } 63 64 public void addAllowedValue(String value) { 65 constrained = true; 66 allowedValues.add(value); 67 } 68 69 public boolean isConstrained() { 70 return constrained; 71 } 72 73 public boolean isEnumrable() { 74 return ( allowedValues.size() > 0 ); 75 } 76 77 public String [] getAllowedValues() { 78 return (String [])allowedValues.toArray(new String [0]); 79 } 80 81 public Value valueOf(Object value, Context context) throws ValueCastException { 82 if (value instanceof StringValue) { 83 return (StringValue)value; 84 } else if (value instanceof String ) { 85 return new StringValue((String )value); 86 } else if (value instanceof String []) { 87 return new StringValue(((String [])value)[0]); 88 } else if (value instanceof PrintableValue) { 89 return new StringValue(((PrintableValue)value).print(new StringBuffer (256)).toString()); 90 } 91 throw new ValueCastException(new ErrorMessage("uncastableStringValue", new Object [] { value })); 92 } 93 94 public void validate(Value value, Context context) throws ValidationException { 95 String valueAsString = value.toString(); 96 if ( constrained ) { 97 if ( isEnumrable() ) { 98 for ( Iterator i = allowedValues.iterator(); i.hasNext(); ) { 99 if (valueAsString.equals((String )i.next())) { 100 return; 101 } 102 } 103 throw new ValidationException(new ErrorMessage("invalidStringValue", new String [] { valueAsString })); 104 } else { 105 if ( minimumLength != -1 && valueAsString.length() < minimumLength ) throw new ValidationException(new ErrorMessage("stringTooShort", new Object [] { new Integer (minimumLength) })); 106 if ( maximumLength != -1 && valueAsString.length() > maximumLength ) throw new ValidationException(new ErrorMessage("stringTooLong", new Object [] { new Integer (maximumLength) })); 107 } 108 } 109 } 110 } | Popular Tags |