View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2001 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
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) { //Load a class without package
99  					try {
100 						_class = Class.forName("net.wotonomy.control." + cname);
101 					} catch (ClassNotFoundException ex) {
102 					}
103 					//search for the class in access
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  * $Log$
154  * Revision 1.2  2006/02/16 16:47:14  cgruber
155  * Move some classes in to "internal" packages and re-work imports, etc.
156  *
157  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
158  *
159  * Revision 1.1  2006/02/16 13:19:57  cgruber
160  * Check in all sources in eclipse-friendly maven-enabled packages.
161  *
162  * Revision 1.2  2003/08/11 18:17:41  chochos
163  * decoding of objects now works properly
164  *
165  */