KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.slide.projector.descriptor;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
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 JavaDoc ELEMENT_NAME = "map";
19     
20     public final static String JavaDoc ALL = "*";
21     public final static MapValueDescriptor ANY = new MapValueDescriptor(new ParameterDescriptor(MapValueDescriptor.ALL, new ParameterMessage("mapEntry/any"), new AnyValueDescriptor()));
22     
23     protected List JavaDoc entryDescriptors = new ArrayList JavaDoc();
24     
25     public MapValueDescriptor() {
26     }
27     
28     public MapValueDescriptor(List JavaDoc 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 JavaDoc getEntryDescriptors() {
47         return entryDescriptors;
48     }
49     
50     public Value valueOf(Object JavaDoc 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 JavaDoc ) {
57             return castedMap((Map JavaDoc)value, context);
58         } else {
59             throw new ValueCastException(new ErrorMessage("uncastableMapValue", new Object JavaDoc[] { value }));
60         }
61     }
62
63     private MapValue castedMap(Map JavaDoc map, Context context) throws ValueCastException {
64         Map JavaDoc castedMap = new HashMap JavaDoc(map);
65         for ( Iterator JavaDoc i = entryDescriptors.iterator(); i.hasNext(); ) {
66             ParameterDescriptor parameterDescriptor = (ParameterDescriptor)i.next();
67             ValueDescriptor entryDescriptor = parameterDescriptor.getValueDescriptor();
68             String JavaDoc key = parameterDescriptor.getName();
69             if ( key.equals(ALL) ) {
70                 for ( Iterator JavaDoc j = castedMap.entrySet().iterator(); j.hasNext(); ) {
71                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc)j.next();
72                     Object JavaDoc 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 JavaDoc e) {
77                             throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object JavaDoc[] { 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 JavaDoc object = castedMap.get(key);
90                     if ( object instanceof AnyValue ) {
91                         try {
92                             object = ((AnyValue)object).load(context);
93                         } catch (Exception JavaDoc e) {
94                             throw new ValueCastException(new ErrorMessage("uncastableArrayValue", new Object JavaDoc[] { 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 JavaDoc map = ((MapValue)value).getMap();
110         for ( Iterator JavaDoc i = entryDescriptors.iterator(); i.hasNext(); ) {
111             ParameterDescriptor parameterDescriptor = (ParameterDescriptor)i.next();
112             ValueDescriptor entryDescriptor = parameterDescriptor.getValueDescriptor();
113             String JavaDoc key = parameterDescriptor.getName();
114             if ( key.equals(ALL) ) {
115                 for ( Iterator JavaDoc 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 JavaDoc[] { 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