KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.slide.projector.descriptor;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.apache.slide.projector.Context;
7 import org.apache.slide.projector.i18n.ErrorMessage;
8 import org.apache.slide.projector.value.AnyValue;
9 import org.apache.slide.projector.value.ArrayValue;
10 import org.apache.slide.projector.value.NullValue;
11 import org.apache.slide.projector.value.Value;
12
13 public class ArrayValueDescriptor implements ValueDescriptor {
14     protected ValueDescriptor entryValueDescriptor;
15
16     public ArrayValueDescriptor() {
17     }
18
19     public ArrayValueDescriptor(ValueDescriptor entryValueDescriptor) {
20         this.entryValueDescriptor = entryValueDescriptor;
21     }
22
23     public void setEntryValueDescriptor(ValueDescriptor entryValueDescriptor) {
24         this.entryValueDescriptor = entryValueDescriptor;
25     }
26
27     public ValueDescriptor getEntryValueDescriptor() {
28         return entryValueDescriptor;
29     }
30
31     public Value valueOf(Object JavaDoc value, Context context) throws ValueCastException {
32         Value[] array = null;
33         if ( value instanceof String JavaDoc[] ) {
34             array = new Value[((String JavaDoc[])value).length];
35             for ( int i = 0; i < array.length; i++ ) {
36                 array[i] = entryValueDescriptor.valueOf(((String JavaDoc [])value)[i], context);
37             }
38         } else if ( value instanceof List JavaDoc ) {
39             array = new Value[((List JavaDoc)value).size()];
40             int counter = 0;
41             for ( Iterator JavaDoc i = ((List JavaDoc)value).iterator(); i.hasNext(); ) {
42                 Object JavaDoc entry = i.next();
43                 if ( entry instanceof AnyValue ) {
44                     try {
45                         array[counter] = entryValueDescriptor.valueOf(((AnyValue)entry).load(context), context);
46                     } catch (Exception JavaDoc e) {
47                         throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object JavaDoc[] { entry }));
48                     }
49                 } else {
50                     array[counter] = entryValueDescriptor.valueOf(entry, context);
51                 }
52                 counter++;
53             }
54         } else if ( value instanceof ArrayValue ) {
55             array = new Value[((ArrayValue)value).getArray().length];
56             for ( int i = 0; i < array.length; i++ ) {
57                 Object JavaDoc entry = ((ArrayValue)value).getArray()[i];
58                 if ( entry instanceof AnyValue ) {
59                     try {
60                         array[i] = entryValueDescriptor.valueOf(((AnyValue)entry).load(context), context);
61                     } catch (Exception JavaDoc e) {
62                         throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object JavaDoc[] { value }));
63                     }
64                 } else {
65                     array[i] = entryValueDescriptor.valueOf(entry, context);
66                 }
67             }
68         } else {
69             throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object JavaDoc[] { value }));
70         }
71         return new ArrayValue(array);
72     }
73     
74     public void validate(Value value, Context context) throws ValidationException {
75         try {
76             Value[] array = ((ArrayValue)value).getArray();
77             for ( int i = 0; i < array.length; i++ ) {
78                 if ( !(array[i] instanceof NullValue) ) {
79                     entryValueDescriptor.validate(array[i], context);
80                 }
81             }
82         } catch ( ValidationException exception ) {
83             throw new ValidationException(new ErrorMessage("invalidNestedValue", new Object JavaDoc[] { value }), exception);
84         }
85     }
86 }
Popular Tags