001: // Copyright 2000 Samuele Pedroni
002:
003: package org.python.core;
004:
005: import java.util.StringTokenizer;
006:
007: public abstract class InternalTables {
008:
009: // x__ --> org.python.core.X__InternalTables
010: // (x|X)__> --> org.python.core.X__InternalTables
011: // >(x|X)__ --> org.python.core.InternalTablesX__
012: // other (X__|__.__) --> other
013: //
014: /**
015: * XXX: These contortions are here to decide between InternalTables1 and
016: * InternalTables2. Since we have deprecated support for JDK 1.1 -- this
017: * should go away and InternalTables1 and InternalTables2 should be merged
018: * and replace this class.
019: */
020: static private InternalTables tryImpl(String id) {
021: try {
022: if (id.indexOf('.') < 0) {
023: boolean glue = true;
024: boolean front = true;
025: if (id.charAt(0) == '>') {
026: id = id.substring(1);
027: front = false;
028: } else if (id.charAt(id.length() - 1) == '>') {
029: id = id.substring(0, id.length() - 1);
030: } else if (!Character.isLowerCase(id.charAt(0)))
031: glue = false;
032: if (glue) {
033: StringBuffer buf = new StringBuffer(
034: "org.python.core.");
035: if (!front) {
036: buf.append("InternalTables");
037: }
038: if (Character.isLowerCase(id.charAt(0))) {
039: buf.append(Character.toUpperCase(id.charAt(0)));
040: buf.append(id.substring(1));
041: } else {
042: buf.append(id);
043: }
044: if (front) {
045: buf.append("InternalTables");
046: }
047: id = buf.toString();
048: }
049: }
050: // System.err.println("*InternalTables*-create-try: "+id);
051: return (InternalTables) Class.forName(id).newInstance();
052: } catch (Throwable e) {
053: // System.err.println(" exc: "+e); // ??dbg
054: return null;
055: }
056: }
057:
058: static InternalTables createInternalTables() {
059: java.util.Properties registry = PySystemState.registry;
060: if (registry == null) {
061: throw new java.lang.IllegalStateException(
062: "Jython interpreter state not initialized. "
063: + "You need to call PySystemState.initialize or "
064: + "PythonInterpreter.initialize.");
065: }
066: String cands = registry
067: .getProperty("python.options.internalTablesImpl");
068: if (cands == null) {
069: String version = System.getProperty("java.version");
070: if (version.compareTo("1.2") >= 0) {
071: cands = ">2:>1";
072: } else {
073: cands = ">1";
074: }
075: } else {
076: cands = cands + ":>2:>1";
077: }
078: StringTokenizer candEnum = new StringTokenizer(cands, ":");
079: while (candEnum.hasMoreTokens()) {
080: InternalTables tbl = tryImpl(candEnum.nextToken().trim());
081: if (tbl != null) {
082: return tbl;
083: }
084: }
085: return null; // XXX: never reached -- throw exception instead?
086: }
087:
088: protected abstract boolean queryCanonical(String name);
089:
090: protected abstract PyJavaClass getCanonical(Class c);
091:
092: protected abstract PyJavaClass getLazyCanonical(String name);
093:
094: protected abstract void putCanonical(Class c, PyJavaClass canonical);
095:
096: protected abstract void putLazyCanonical(String name,
097: PyJavaClass canonical);
098:
099: protected abstract Class getAdapterClass(Class c);
100:
101: protected abstract void putAdapterClass(Class c, Class ac);
102:
103: protected abstract Object getAdapter(Object o, String evc);
104:
105: protected abstract void putAdapter(Object o, String evc, Object ad);
106:
107: public boolean _doesSomeAutoUnload() {
108: return false;
109: }
110:
111: public void _forceCleanup() {
112: }
113:
114: public abstract void _beginCanonical();
115:
116: public abstract void _beginLazyCanonical();
117:
118: public abstract void _beginOverAdapterClasses();
119:
120: public abstract void _beginOverAdapters();
121:
122: public abstract Object _next();
123:
124: public abstract void _flushCurrent();
125:
126: public abstract void _flush(PyJavaClass jc);
127:
128: static public class _LazyRep {
129: public String name;
130:
131: public PackageManager mgr;
132:
133: _LazyRep(String name, PackageManager mgr) {
134: this.name = name;
135: this.mgr = mgr;
136: }
137: }
138:
139: }
|