1 package org.apache.slide.projector.descriptor; 2 3 import java.util.ArrayList ; 4 import java.util.HashMap ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Map ; 8 9 import org.apache.slide.projector.Context; 10 import org.apache.slide.projector.i18n.ErrorMessage; 11 import org.apache.slide.projector.i18n.ParameterMessage; 12 import org.apache.slide.projector.value.AnyValue; 13 import org.apache.slide.projector.value.MapValue; 14 import org.apache.slide.projector.value.NullValue; 15 import org.apache.slide.projector.value.Value; 16 17 public class MapValueDescriptor implements ValueDescriptor { 18 private final static String ELEMENT_NAME = "map"; 19 20 public final static String ALL = "*"; 21 public final static MapValueDescriptor ANY = new MapValueDescriptor(new ParameterDescriptor(MapValueDescriptor.ALL, new ParameterMessage("mapEntry/any"), new AnyValueDescriptor())); 22 23 protected List entryDescriptors = new ArrayList (); 24 25 public MapValueDescriptor() { 26 } 27 28 public MapValueDescriptor(List entryDescriptors) { 29 this.entryDescriptors = entryDescriptors; 30 } 31 32 public MapValueDescriptor(ParameterDescriptor parameterDescriptor) { 33 entryDescriptors.add(parameterDescriptor); 34 } 35 36 public MapValueDescriptor(ParameterDescriptor[] parameterDescriptors) { 37 for ( int i = 0; i < parameterDescriptors.length; i++ ) { 38 entryDescriptors.add(parameterDescriptors[i]); 39 } 40 } 41 42 public void addEntryDescriptor(ParameterDescriptor parameterDescriptor) { 43 entryDescriptors.add(parameterDescriptor); 44 } 45 46 public List getEntryDescriptors() { 47 return entryDescriptors; 48 } 49 50 public Value valueOf(Object value, Context context) throws ValueCastException { 51 if ( entryDescriptors.isEmpty() ) { 52 throw new ValueCastException(new ErrorMessage("noMapEntryDescriptorsFound")); 53 } 54 if ( value instanceof MapValue ) { 55 return castedMap(((MapValue)value).getMap(), context); 56 } else if ( value instanceof Map ) { 57 return castedMap((Map )value, context); 58 } else { 59 throw new ValueCastException(new ErrorMessage("uncastableMapValue", new Object [] { value })); 60 } 61 } 62 63 private MapValue castedMap(Map map, Context context) throws ValueCastException { 64 Map castedMap = new HashMap (map); 65 for ( Iterator i = entryDescriptors.iterator(); i.hasNext(); ) { 66 ParameterDescriptor parameterDescriptor = (ParameterDescriptor)i.next(); 67 ValueDescriptor entryDescriptor = parameterDescriptor.getValueDescriptor(); 68 String key = parameterDescriptor.getName(); 69 if ( key.equals(ALL) ) { 70 for ( Iterator j = castedMap.entrySet().iterator(); j.hasNext(); ) { 71 Map.Entry entry = (Map.Entry )j.next(); 72 Object value = entry.getValue(); 73 if ( value instanceof AnyValue ) { 74 try { 75 castedMap.put(entry.getKey(), entryDescriptor.valueOf(((AnyValue)value).load(context), context)); 76 } catch (Exception e) { 77 throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object [] { value })); 78 } 79 } else { 80 castedMap.put(entry.getKey(), entryDescriptor.valueOf(value, context)); 81 } 82 } 83 } else { 84 if ( !castedMap.containsKey(key) ) { 85 if ( !parameterDescriptor.isRequired() ) { 86 castedMap.put(key, parameterDescriptor.getDefaultValue()); 87 } 88 } else { 89 Object object = castedMap.get(key); 90 if ( object instanceof AnyValue ) { 91 try { 92 object = ((AnyValue)object).load(context); 93 } catch (Exception e) { 94 throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object [] { object }), e); 95 } 96 } 97 if ( object.equals(parameterDescriptor.getDefaultValue())) { 98 castedMap.put(key, object); 99 } else { 100 castedMap.put(key, entryDescriptor.valueOf(object, context)); 101 } 102 } 103 } 104 } 105 return new MapValue(castedMap); 106 } 107 108 public void validate(Value value, Context context) throws ValidationException { 109 Map map = ((MapValue)value).getMap(); 110 for ( Iterator i = entryDescriptors.iterator(); i.hasNext(); ) { 111 ParameterDescriptor parameterDescriptor = (ParameterDescriptor)i.next(); 112 ValueDescriptor entryDescriptor = parameterDescriptor.getValueDescriptor(); 113 String key = parameterDescriptor.getName(); 114 if ( key.equals(ALL) ) { 115 for ( Iterator j = map.values().iterator(); j.hasNext(); ) { 116 Value entryValue = (Value)j.next(); 117 if ( !(entryValue instanceof NullValue) ) { 118 entryDescriptor.validate(entryValue, context); 119 } 120 } 121 } else { 122 if ( !map.containsKey(key) ) { 123 if ( parameterDescriptor.isRequired() ) { 124 throw new ValidationException(new ErrorMessage("mapEntryMissing", new String [] { key })); 125 } 126 } else { 127 Value entryValue = (Value)map.get(key); 128 if ( !(entryValue instanceof NullValue) ) { 129 entryDescriptor.validate(entryValue, context); 130 } 131 } 132 } 133 } 134 } 135 } | Popular Tags |