1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.wotonomy.control;
19
20 import net.wotonomy.foundation.NSArray;
21 import net.wotonomy.foundation.NSDictionary;
22 import net.wotonomy.foundation.NSMutableDictionary;
23 import net.wotonomy.foundation.NSSelector;
24
25 /***
26 * @author ezamudio@nasoft.com
27 * @author $Author: cgruber $
28 * @version $Revision: 894 $
29 */
30 public class EOKeyValueArchiver {
31
32
33 public static final NSSelector sel = new NSSelector("referenceToEncodeForObject",
34 new Class[]{ Object.class });
35 NSMutableDictionary dict = new NSMutableDictionary();
36 private Object _delegate;
37
38 public EOKeyValueArchiver() {
39 super();
40 }
41
42 public void encodeBool(boolean flag, String key) {
43 String v = flag ? "true" : "false";
44 dict.setObjectForKey(v, key);
45 }
46
47 public void encodeInt(int value, String key) {
48 dict.setObjectForKey(new Integer(value), key);
49 }
50
51 public void encodeObject(Object obj, String key) {
52 if (obj == null)
53 return;
54 if (obj instanceof NSArray || obj instanceof NSDictionary) {
55 dict.setObjectForKey(obj, key);
56 return;
57 }
58 if (obj instanceof EOKeyValueArchiving) {
59 EOKeyValueArchiver arch = new EOKeyValueArchiver();
60 ((EOKeyValueArchiving)obj).encodeWithKeyValueArchiver(arch);
61 dict.setObjectForKey(arch.dictionary(), key);
62 return;
63 }
64 if (obj instanceof String)
65 dict.setObjectForKey(obj, key);
66 }
67
68 public void encodeReferenceToObject(Object obj, String key) {
69 if (_delegate != null) {
70 if (sel.implementedByObject(obj)) {
71 try {
72 Object ref = sel.invoke(_delegate, obj);
73 if (ref != null)
74 dict.setObjectForKey(ref, key);
75 } catch (Exception e) {
76 }
77 }
78 }
79 }
80
81 public NSDictionary dictionary() {
82 return new NSDictionary(dict);
83 }
84
85 public void setDelegate(Object delegate) {
86 _delegate = delegate;
87 }
88 public Object delegate() {
89 return _delegate;
90 }
91
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106