KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > descriptor > StringValueDescriptor


1 package org.apache.slide.projector.descriptor;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
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 JavaDoc 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 JavaDoc allowedValues = new ArrayList JavaDoc();
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 JavaDoc[] allowedValues) {
31         this.constrained = true;
32         this.allowedValues = new ArrayList JavaDoc(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 JavaDoc 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 JavaDoc[] getAllowedValues() {
78         return (String JavaDoc [])allowedValues.toArray(new String JavaDoc [0]);
79     }
80
81     public Value valueOf(Object JavaDoc value, Context context) throws ValueCastException {
82         if (value instanceof StringValue) {
83             return (StringValue)value;
84         } else if (value instanceof String JavaDoc) {
85             return new StringValue((String JavaDoc)value);
86         } else if (value instanceof String JavaDoc[]) {
87             return new StringValue(((String JavaDoc [])value)[0]);
88         } else if (value instanceof PrintableValue) {
89             return new StringValue(((PrintableValue)value).print(new StringBuffer JavaDoc(256)).toString());
90         }
91         throw new ValueCastException(new ErrorMessage("uncastableStringValue", new Object JavaDoc[] { value }));
92     }
93
94     public void validate(Value value, Context context) throws ValidationException {
95         String JavaDoc valueAsString = value.toString();
96         if ( constrained ) {
97             if ( isEnumrable() ) {
98                 for ( Iterator JavaDoc i = allowedValues.iterator(); i.hasNext(); ) {
99                     if (valueAsString.equals((String JavaDoc)i.next())) {
100                         return;
101                     }
102                 }
103                 throw new ValidationException(new ErrorMessage("invalidStringValue", new String JavaDoc[] { valueAsString }));
104             } else {
105                 if ( minimumLength != -1 && valueAsString.length() < minimumLength ) throw new ValidationException(new ErrorMessage("stringTooShort", new Object JavaDoc[] { new Integer JavaDoc(minimumLength) }));
106                 if ( maximumLength != -1 && valueAsString.length() > maximumLength ) throw new ValidationException(new ErrorMessage("stringTooLong", new Object JavaDoc[] { new Integer JavaDoc(maximumLength) }));
107             }
108         }
109     }
110 }
Popular Tags