001: // Copyright (c) Corporation for National Research Initiatives
002:
003: package org.python.core;
004:
005: import java.util.*;
006:
007: class CollectionProxy {
008: public static final CollectionProxy NoProxy = new EnumerationProxy(
009: null);
010:
011: private static boolean checkedJava2 = false;
012:
013: private static CollectionProxy java2Proxy = null;
014:
015: public CollectionProxy instanceFindCollection(Object object) {
016: return null;
017: }
018:
019: public static CollectionProxy findCollection(Object object) {
020: if (object == null)
021: return NoProxy;
022:
023: if (!checkedJava2) {
024: checkedJava2 = true;
025: try {
026: Class c = Class
027: .forName("org.python.core.CollectionProxy2");
028: Class.forName("java.util.Collection");
029: java2Proxy = (CollectionProxy) c.newInstance();
030: } catch (Throwable t) {
031: }
032: }
033: if (java2Proxy != null) {
034: CollectionProxy ret = java2Proxy
035: .instanceFindCollection(object);
036: if (ret != null)
037: return ret;
038: }
039:
040: if (object instanceof Vector) {
041: return new VectorProxy(((Vector) object));
042: }
043: if (object instanceof Enumeration) {
044: return new EnumerationProxy(((Enumeration) object));
045: }
046: if (object instanceof Dictionary) {
047: return new DictionaryProxy(((Dictionary) object));
048: }
049:
050: return NoProxy;
051: }
052:
053: /** The basic functions to implement a mapping* */
054: public int __len__() {
055: throw Py.AttributeError("__len__");
056: }
057:
058: public PyObject __finditem__(int key) {
059: return __finditem__(new PyInteger(key));
060: }
061:
062: public PyObject __finditem__(PyObject key) {
063: throw Py.AttributeError("__getitem__");
064: }
065:
066: public PyObject __getitem__(int key) {
067: PyObject ret = __finditem__(key);
068: if (ret == null)
069: throw Py.KeyError("" + key);
070: return ret;
071: }
072:
073: public PyObject __getitem__(PyObject key) {
074: PyObject ret = __finditem__(key);
075: if (ret == null)
076: throw Py.KeyError(key.toString());
077: return ret;
078: }
079:
080: public void __setitem__(PyObject key, PyObject value) {
081: throw Py.AttributeError("__setitem__");
082: }
083:
084: public void __delitem__(PyObject key) {
085: throw Py.AttributeError("__delitem__");
086: }
087: }
088:
089: class EnumerationProxy extends CollectionProxy {
090: Enumeration proxy;
091:
092: int counter;
093:
094: public EnumerationProxy(Enumeration proxy) {
095: this .proxy = proxy;
096: this .counter = 0;
097: }
098:
099: public PyObject __finditem__(int key) {
100: if (key != this .counter) {
101: throw Py
102: .ValueError("enumeration indices must be consecutive ints starting at 0");
103: }
104: this .counter++;
105: if (this .proxy.hasMoreElements()) {
106: return Py.java2py(this .proxy.nextElement());
107: } else {
108: return null;
109: }
110: }
111:
112: public PyObject __finditem__(PyObject key) {
113: if (key instanceof PyInteger) {
114: return __finditem__(((PyInteger) key).getValue());
115: } else {
116: throw Py.TypeError("only integer keys accepted");
117: }
118: }
119: }
120:
121: class VectorProxy extends CollectionProxy {
122: Vector proxy;
123:
124: public VectorProxy(Vector proxy) {
125: this .proxy = proxy;
126: }
127:
128: public int __len__() {
129: return this .proxy.size();
130: }
131:
132: public PyObject __finditem__(int key) {
133: try {
134: return Py.java2py(this .proxy.elementAt(key));
135: } catch (ArrayIndexOutOfBoundsException exc) {
136: return null;
137: }
138: }
139:
140: public PyObject __finditem__(PyObject key) {
141: if (key instanceof PyInteger) {
142: return __finditem__(((PyInteger) key).getValue());
143: } else {
144: throw Py.TypeError("only integer keys accepted");
145: }
146: }
147:
148: public void __setitem__(PyObject key, PyObject value) {
149: if (key instanceof PyInteger) {
150: this .proxy.setElementAt(Py.tojava(value, Object.class),
151: ((PyInteger) key).getValue());
152: } else {
153: throw Py.TypeError("only integer keys accepted");
154: }
155: }
156:
157: public void __delitem__(PyObject key) {
158: if (key instanceof PyInteger) {
159: this .proxy.removeElementAt(((PyInteger) key).getValue());
160: } else {
161: throw Py.TypeError("only integer keys accepted");
162: }
163: }
164: }
165:
166: class DictionaryProxy extends CollectionProxy {
167: Dictionary proxy;
168:
169: public DictionaryProxy(Dictionary proxy) {
170: this .proxy = proxy;
171: }
172:
173: public int __len__() {
174: return this .proxy.size();
175: }
176:
177: public PyObject __finditem__(int key) {
178: throw Py.TypeError("loop over non-sequence");
179: }
180:
181: public PyObject __finditem__(PyObject key) {
182: return Py.java2py(this .proxy.get(Py.tojava(key, Object.class)));
183: }
184:
185: public void __setitem__(PyObject key, PyObject value) {
186: this .proxy.put(Py.tojava(key, Object.class), Py.tojava(value,
187: Object.class));
188: }
189:
190: public void __delitem__(PyObject key) {
191: this .proxy.remove(Py.tojava(key, Object.class));
192: }
193: }
|