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 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  	//for the delegate
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   * $Log$
95   * Revision 1.2  2006/02/16 16:47:14  cgruber
96   * Move some classes in to "internal" packages and re-work imports, etc.
97   *
98   * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
99   *
100  * Revision 1.1  2006/02/16 13:19:57  cgruber
101  * Check in all sources in eclipse-friendly maven-enabled packages.
102  *
103  * Revision 1.1  2003/08/09 01:17:53  chochos
104  * Part of the EOKeyValueArchiving protocol (archives objects into NSDictionaries and vice-versa)
105  *
106  */