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 java.lang.reflect.Method;
21
22 import net.wotonomy.foundation.NSDictionary;
23 import net.wotonomy.foundation.NSKeyValueCoding;
24
25 /*** Creates objects from dictionaries that were created with
26 * EOKeyValueArchiver.
27 * @author michael@mpowers.net
28 * @author $Author: cgruber $
29 * @version $Revision: 894 $
30 */
31 public class EOKeyValueUnarchiver {
32
33 NSDictionary dict;
34 Object _delegate;
35 protected static final Class[] METHOD_ARGS = new Class[]{ EOKeyValueUnarchiver.class };
36
37 public EOKeyValueUnarchiver(NSDictionary archive) {
38 super();
39 dict = archive;
40 }
41
42 public void awakeObjects() {
43 }
44
45 public boolean decodeBoolForKey(String key) {
46 Object x = dict.objectForKey(key);
47 if (x == null)
48 return false;
49 return (x.equals("true") || x.equals("YES") || x.equals("Y"));
50 }
51
52 public int decodeIntForKey(String key) {
53 Object x = dict.objectForKey(key);
54 if (x == null)
55 return 0;
56 if (x instanceof Number)
57 return ((Number)x).intValue();
58 try {
59 int i = Integer.parseInt(x.toString());
60 return i;
61 } catch (NumberFormatException ex) {
62 return 0;
63 }
64 }
65
66 public Object decodeObjectForKey(String key) {
67 Object x = dict.objectForKey(key);
68 if (x == null)
69 return null;
70 if (x instanceof NSDictionary) {
71 NSDictionary d = (NSDictionary)x;
72 if (d.objectForKey("class") != null) {
73 String cname = d.objectForKey("class").toString();
74 Class _class = null;
75 if (cname.equals("NSNumber")) {
76 if (d.objectForKey("value") != null)
77 return new Long(d.objectForKey("value").toString());
78 } else if (cname.equals("NSDecimalNumber")) {
79 if (d.objectForKey("value") != null)
80 return new java.math.BigDecimal(d.objectForKey("value").toString());
81 } else if (cname.equals("NSString")) {
82 if (d.objectForKey("value") != null)
83 return d.objectForKey("value").toString();
84 } else if (cname.equals("EONull")) {
85 return NSKeyValueCoding.NullValue;
86 } else if (cname.equals("EOFetchSpecification")) {
87 _class = EOFetchSpecification.class;
88 } else if (cname.equals("EOKeyValueQualifier")) {
89 _class = EOKeyValueQualifier.class;
90 } else if (cname.equals("EONotQualifier")) {
91 _class = EONotQualifier.class;
92 } else if (cname.equals("EOAndQualifier")) {
93 _class = EOAndQualifier.class;
94 } else if (cname.equals("EOOrQualifier")) {
95 _class = EOOrQualifier.class;
96 } else if (cname.equals("EOSortOrdering")) {
97 _class = EOSortOrdering.class;
98 } else if (cname.indexOf(".") < 0) {
99 try {
100 _class = Class.forName("net.wotonomy.control." + cname);
101 } catch (ClassNotFoundException ex) {
102 }
103
104 if (_class == null) {
105 try {
106 _class = Class.forName("net.wotonomy.access." + cname);
107 } catch (ClassNotFoundException ex) {
108 }
109 }
110 } else {
111 try {
112 _class = Class.forName(cname);
113 } catch (ClassNotFoundException ex) {
114 }
115 }
116 if (_class == null)
117 return x;
118 try {
119 Method met = _class.getMethod("decodeWithKeyValueUnarchiver", METHOD_ARGS);
120 return met.invoke(null, new Object[]{ new EOKeyValueUnarchiver(d) });
121 } catch (Exception ex) {
122 ex.printStackTrace();
123 return x;
124 }
125 }
126 }
127 return x;
128 }
129
130 public Object decodeObjectReferenceForKey(String key) {
131 return null;
132 }
133
134 public void ensureObjectAwake(Object obj) {
135 }
136
137 public void finishInitializationOfObjects() {
138 }
139
140 public Object parent() {
141 return null;
142 }
143
144 public void setDelegate(Object del) {
145 _delegate = del;
146 }
147 public Object delegate() {
148 return _delegate;
149 }
150
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165