01: package com.jeta.forms.store.jml;
02:
03: import java.util.HashMap;
04: import java.util.Iterator;
05:
06: import com.jeta.forms.store.jml.dom.JMLDocument;
07: import com.jeta.forms.store.jml.dom.JMLNode;
08:
09: /**
10: * This class is responsible for transforming java.util.HashMap objects to/from
11: * XML form.
12: *
13: * @author Jeff Tassin
14: */
15: public class HashMapSerializer implements JMLSerializer {
16:
17: /**
18: * XMLDeserializer implementation <object classname="java.util.HashMap">
19: * <property name="keyname">value</property> <property name="keyname">value</property>
20: * </item> ... <item> </item> </object>
21: */
22: public JMLNode serialize(JMLDocument document, Object obj)
23: throws JMLException {
24:
25: JMLUtils.verifyObjectType(obj, HashMap.class);
26:
27: JMLNode hashNode = JMLUtils.createObjectNode(document, obj);
28: HashMap hash = (HashMap) obj;
29:
30: if (hash != null) {
31: Iterator iter = hash.keySet().iterator();
32: while (iter.hasNext()) {
33: Object key = iter.next();
34: JMLNode itemnode = document.createNode("item");
35: itemnode.appendChild(JMLUtils.createPropertyNode(
36: document, "key", JMLUtils
37: .getPrimitiveHolder(key)));
38: itemnode.appendChild(JMLUtils.createPropertyNode(
39: document, "value", JMLUtils
40: .getPrimitiveHolder(hash.get(key))));
41: hashNode.appendChild(itemnode);
42: }
43: }
44: return hashNode;
45: }
46:
47: }
|