1 package org.apache.slide.projector.descriptor; 2 3 import java.util.Iterator ; 4 import java.util.List ; 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 value, Context context) throws ValueCastException { 32 Value[] array = null; 33 if ( value instanceof String [] ) { 34 array = new Value[((String [])value).length]; 35 for ( int i = 0; i < array.length; i++ ) { 36 array[i] = entryValueDescriptor.valueOf(((String [])value)[i], context); 37 } 38 } else if ( value instanceof List ) { 39 array = new Value[((List )value).size()]; 40 int counter = 0; 41 for ( Iterator i = ((List )value).iterator(); i.hasNext(); ) { 42 Object entry = i.next(); 43 if ( entry instanceof AnyValue ) { 44 try { 45 array[counter] = entryValueDescriptor.valueOf(((AnyValue)entry).load(context), context); 46 } catch (Exception e) { 47 throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object [] { 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 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 e) { 62 throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object [] { value })); 63 } 64 } else { 65 array[i] = entryValueDescriptor.valueOf(entry, context); 66 } 67 } 68 } else { 69 throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object [] { 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 [] { value }), exception); 84 } 85 } 86 } | Popular Tags |