KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > values > types > MapType


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.values.types;
5
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.LinkedHashMap JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Properties JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.oddjob.arooa.ArooaHandler;
17 import org.oddjob.arooa.ArooaContext;
18 import org.oddjob.arooa.ArooaConstants;
19 import org.oddjob.arooa.handlers.MappedPropertyHandler;
20 import org.oddjob.arooa.reflect.IntrospectionHelper;
21
22 /**
23  * A wrapper for a Map. This type also implements map so that it
24  * can be used merge values into Variables job if required.
25  *
26  * @oddjob.description A type which creates a mapping from
27  * the given name to the given value. Alternatively it can be
28  * thought of as a list of name/value pairs.
29  * <p>
30  * Other Map type values will be merged in with this map if
31  * the value is un-named. If a name is supplied they will remain
32  * as mapped maps.
33  * <p>
34  * A mapped type can be used as an input where it will have the
35  * same format as a properties file.
36  *
37  * @oddjob.example
38  *
39  * A simple map.
40  *
41  * <pre>
42  * ...
43  * &lt;map&gt;
44  * &lt;value name="FRUIT" value="apple"/&gt;
45  * &lt;value name="answer" value="42"/>
46  * &lt;/map&gt;
47  * ...
48  * </pre>
49  *
50  * @oddjob.example
51  *
52  * Other mapped types being merged in.
53  * <pre>
54  * ...
55  * &lt;map&gt;
56  * &lt;system/&gt;
57  * &lt;environment command="env"/>
58  * &lt;/map&gt;
59  * ...
60  * </pre>
61  *
62  */

63 public class MapType implements Map JavaDoc {
64
65     private Map JavaDoc map = new LinkedHashMap JavaDoc();
66     
67     public void setValue(String JavaDoc name, Object JavaDoc value) {
68         map.put(name, value);
69     }
70
71     /**
72      * Return the value for this type.
73      * <p>
74      * If the requested type is String, or InputStream the map is
75      * converted into a text equivelent of the form
76      * key=value. Thus it can be written out as a properies file.
77      * <p>
78      * If the requested type is Properties the Map is converted
79      * to a Properties type.
80      * <p>
81      * Otherwise a Map type is returned. The map may be empty but
82      * null will never be returned.
83      *
84      * @param requested The desired class.
85      * @return The wrapped value.
86      */

87     public Object JavaDoc valueFor(Class JavaDoc requested) {
88         Map JavaDoc resolved = resolveMap(requested);
89         if (requested.isAssignableFrom(Object JavaDoc.class)) {
90             return resolved;
91         }
92         if (requested.isAssignableFrom(String JavaDoc.class)) {
93             return propertiesFrom(resolved);
94         }
95         if (requested.isAssignableFrom(InputStream JavaDoc.class)) {
96             return new ByteArrayInputStream JavaDoc(propertiesFrom(resolved).getBytes());
97         }
98         if (requested.isAssignableFrom(Properties JavaDoc.class)) {
99             Properties JavaDoc results = new Properties JavaDoc();
100             for (Iterator JavaDoc it = resolved.entrySet().iterator(); it.hasNext(); ) {
101                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
102                 results.setProperty(entry.getKey().toString(), entry.getValue().toString());
103             }
104             return results;
105         }
106         return resolved;
107     }
108
109     private Map JavaDoc resolveMap(Class JavaDoc required) {
110         Map JavaDoc results = new LinkedHashMap JavaDoc();
111         for (Iterator JavaDoc it = map.entrySet().iterator(); it.hasNext(); ) {
112             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
113             Object JavaDoc o = IntrospectionHelper.valueFor(entry.getValue());
114             results.put(entry.getKey(), o);
115         }
116         return results;
117     }
118     
119     public ArooaHandler handlerFor(ArooaContext context) {
120         context.set(ArooaConstants.ELEMENT_NAME, "value");
121         return new MappedPropertyHandler();
122     }
123     
124     /* (non-Javadoc)
125      * @see java.util.Map#clear()
126      */

127     public void clear() {
128         throw new UnsupportedOperationException JavaDoc("This map is read only!");
129     }
130
131     /* (non-Javadoc)
132      * @see java.util.Map#containsKey(java.lang.Object)
133      */

134     public boolean containsKey(Object JavaDoc key) {
135         return map.containsKey(key);
136     }
137     
138     /* (non-Javadoc)
139      * @see java.util.Map#containsValue(java.lang.Object)
140      */

141     public boolean containsValue(Object JavaDoc value) {
142         return map.containsValue(value);
143     }
144     
145     /* (non-Javadoc)
146      * @see java.util.Map#entrySet()
147      */

148     public Set JavaDoc entrySet() {
149         return map.entrySet();
150     }
151     
152     /* (non-Javadoc)
153      * @see java.util.Map#get(java.lang.Object)
154      */

155     public Object JavaDoc get(Object JavaDoc key) {
156         return map.get(key);
157     }
158         
159     /* (non-Javadoc)
160      * @see java.util.Map#isEmpty()
161      */

162     public boolean isEmpty() {
163         return map.isEmpty();
164     }
165     
166     /* (non-Javadoc)
167      * @see java.util.Map#keySet()
168      */

169     public Set JavaDoc keySet() {
170         return map.keySet();
171     }
172     
173     /* (non-Javadoc)
174      * @see java.util.Map#put(java.lang.Object, java.lang.Object)
175      */

176     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
177         throw new UnsupportedOperationException JavaDoc("This map is read only!");
178     }
179     
180     /* (non-Javadoc)
181      * @see java.util.Map#putAll(java.util.Map)
182      */

183     public void putAll(Map JavaDoc t) {
184         throw new UnsupportedOperationException JavaDoc("This map is read only!");
185     }
186     
187     /* (non-Javadoc)
188      * @see java.util.Map#remove(java.lang.Object)
189      */

190     public Object JavaDoc remove(Object JavaDoc key) {
191         return map.remove(key);
192     }
193     
194     /* (non-Javadoc)
195      * @see java.util.Map#size()
196      */

197     public int size() {
198         return map.size();
199     }
200     
201     /* (non-Javadoc)
202      * @see java.util.Map#values()
203      */

204     public Collection JavaDoc values() {
205         return map.values();
206     }
207     
208     /**
209      * Converts a map into a String which can be used as
210      * the contents of a properties file.
211      *
212      * @param map The map.
213      * @return The map as a String in properties format.
214      */

215     public static String JavaDoc propertiesFrom(Map JavaDoc map) {
216         String JavaDoc sep = System.getProperty(SystemType.LINE_SEPARATOR);
217         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
218         for (Iterator JavaDoc it = map.entrySet().iterator(); it.hasNext(); ) {
219             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
220             buf.append(entry.getKey());
221             buf.append("=");
222             buf.append(entry.getValue());
223             buf.append(sep);
224         }
225         return buf.toString();
226     }
227 }
228
Popular Tags