| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| EOKeyValueUnarchiver |
|
| 4.545454545454546;4.545 |
| 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 | 0 | public class EOKeyValueUnarchiver { |
| 32 | ||
| 33 | NSDictionary dict; |
|
| 34 | Object _delegate; |
|
| 35 | 0 | protected static final Class[] METHOD_ARGS = new Class[]{ EOKeyValueUnarchiver.class }; |
| 36 | ||
| 37 | public EOKeyValueUnarchiver(NSDictionary archive) { |
|
| 38 | 0 | super(); |
| 39 | 0 | dict = archive; |
| 40 | 0 | } |
| 41 | ||
| 42 | public void awakeObjects() { |
|
| 43 | 0 | } |
| 44 | ||
| 45 | public boolean decodeBoolForKey(String key) { |
|
| 46 | 0 | Object x = dict.objectForKey(key); |
| 47 | 0 | if (x == null) |
| 48 | 0 | return false; |
| 49 | 0 | return (x.equals("true") || x.equals("YES") || x.equals("Y")); |
| 50 | } |
|
| 51 | ||
| 52 | public int decodeIntForKey(String key) { |
|
| 53 | 0 | Object x = dict.objectForKey(key); |
| 54 | 0 | if (x == null) |
| 55 | 0 | return 0; |
| 56 | 0 | if (x instanceof Number) |
| 57 | 0 | return ((Number)x).intValue(); |
| 58 | try { |
|
| 59 | 0 | int i = Integer.parseInt(x.toString()); |
| 60 | 0 | return i; |
| 61 | 0 | } catch (NumberFormatException ex) { |
| 62 | 0 | return 0; |
| 63 | } |
|
| 64 | } |
|
| 65 | ||
| 66 | public Object decodeObjectForKey(String key) { |
|
| 67 | 0 | Object x = dict.objectForKey(key); |
| 68 | 0 | if (x == null) |
| 69 | 0 | return null; |
| 70 | 0 | if (x instanceof NSDictionary) { |
| 71 | 0 | NSDictionary d = (NSDictionary)x; |
| 72 | 0 | if (d.objectForKey("class") != null) { |
| 73 | 0 | String cname = d.objectForKey("class").toString(); |
| 74 | 0 | Class _class = null; |
| 75 | 0 | if (cname.equals("NSNumber")) { |
| 76 | 0 | if (d.objectForKey("value") != null) |
| 77 | 0 | return new Long(d.objectForKey("value").toString()); |
| 78 | 0 | } else if (cname.equals("NSDecimalNumber")) { |
| 79 | 0 | if (d.objectForKey("value") != null) |
| 80 | 0 | return new java.math.BigDecimal(d.objectForKey("value").toString()); |
| 81 | 0 | } else if (cname.equals("NSString")) { |
| 82 | 0 | if (d.objectForKey("value") != null) |
| 83 | 0 | return d.objectForKey("value").toString(); |
| 84 | 0 | } else if (cname.equals("EONull")) { |
| 85 | 0 | return NSKeyValueCoding.NullValue; |
| 86 | 0 | } else if (cname.equals("EOFetchSpecification")) { |
| 87 | 0 | _class = EOFetchSpecification.class; |
| 88 | 0 | } else if (cname.equals("EOKeyValueQualifier")) { |
| 89 | 0 | _class = EOKeyValueQualifier.class; |
| 90 | 0 | } else if (cname.equals("EONotQualifier")) { |
| 91 | 0 | _class = EONotQualifier.class; |
| 92 | 0 | } else if (cname.equals("EOAndQualifier")) { |
| 93 | 0 | _class = EOAndQualifier.class; |
| 94 | 0 | } else if (cname.equals("EOOrQualifier")) { |
| 95 | 0 | _class = EOOrQualifier.class; |
| 96 | 0 | } else if (cname.equals("EOSortOrdering")) { |
| 97 | 0 | _class = EOSortOrdering.class; |
| 98 | 0 | } else if (cname.indexOf(".") < 0) { //Load a class without package |
| 99 | try { |
|
| 100 | 0 | _class = Class.forName("net.wotonomy.control." + cname); |
| 101 | 0 | } catch (ClassNotFoundException ex) { |
| 102 | 0 | } |
| 103 | //search for the class in access |
|
| 104 | 0 | if (_class == null) { |
| 105 | try { |
|
| 106 | 0 | _class = Class.forName("net.wotonomy.access." + cname); |
| 107 | 0 | } catch (ClassNotFoundException ex) { |
| 108 | 0 | } |
| 109 | 0 | } |
| 110 | } else { |
|
| 111 | try { |
|
| 112 | 0 | _class = Class.forName(cname); |
| 113 | 0 | } catch (ClassNotFoundException ex) { |
| 114 | 0 | } |
| 115 | } |
|
| 116 | 0 | if (_class == null) |
| 117 | 0 | return x; |
| 118 | try { |
|
| 119 | 0 | Method met = _class.getMethod("decodeWithKeyValueUnarchiver", METHOD_ARGS); |
| 120 | 0 | return met.invoke(null, new Object[]{ new EOKeyValueUnarchiver(d) }); |
| 121 | 0 | } catch (Exception ex) { |
| 122 | 0 | ex.printStackTrace(); |
| 123 | 0 | return x; |
| 124 | } |
|
| 125 | } |
|
| 126 | } |
|
| 127 | 0 | return x; |
| 128 | } |
|
| 129 | ||
| 130 | public Object decodeObjectReferenceForKey(String key) { |
|
| 131 | 0 | return null; |
| 132 | } |
|
| 133 | ||
| 134 | public void ensureObjectAwake(Object obj) { |
|
| 135 | 0 | } |
| 136 | ||
| 137 | public void finishInitializationOfObjects() { |
|
| 138 | 0 | } |
| 139 | ||
| 140 | public Object parent() { |
|
| 141 | 0 | return null; |
| 142 | } |
|
| 143 | ||
| 144 | public void setDelegate(Object del) { |
|
| 145 | 0 | _delegate = del; |
| 146 | 0 | } |
| 147 | public Object delegate() { |
|
| 148 | 0 | 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 | */ |