📄 modulehandler.java
字号:
/*
* MC2 -- j2me spreadsheet
*
* Copyright (c) 2004-2006 Michael Zemljanukha (mixaz@mail.ru)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Created on Oct 15, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.wapindustrial.calc;
//#ifndef NOGUI
import startup.Startup;
//#endif
/**
* This class handles installed modules, and it also represents a default
* module in context of which everything is being executed.
*/
public class ModuleHandler extends FunctionModule {
public static ModuleHandler MODULEHANDLER = new ModuleHandler();
private FunctionModule modules[] = {
Bfunc.BFUNC,
MathModule.MATHMODULE,
ScreenLite.SCREENLITE,
Operator.OPERATOR,
//#ifdef HELPFUNCTIONS
HelpFunctions.HELPFUNCTIONS,
//#endif
//#ifdef SERVER_UPLOADER
ServerUploader.SERVERUPLOADER,
//#endif
//#ifndef NOGUI
Startup.STARTUP,
//#else
//#ifdef SERVERMODULE
ServerModule.SERVERMODULE,
//#endif
//#endif
//#ifdef SIEMENS_API
SiemensFileSystem.SIEMENSFILESYSTEM,
//#endif
//#ifdef FILE_SYSTEM_JAVA
FileSystemJava.FILESYSTEMJAVA,
//#endif
this // must be the last one
};
private int modulesCount = modules.length;
/* (non-Javadoc)
* @see com.wapindustrial.calc.FunctionModule#initializeNames()
*/
public void initializeNames() {
// for names defined in ModuleHandler context (parsers)
// TODO make this #DEFINED constants
//#ifndef NOGUI
namesArray = new byte[10000];
table = new ModuleName[200];
// namesArray = new byte[1000];
// table = new ModuleName[20];
//#else
//# namesArray = new byte[3000];
//# table = new ModuleName[100];
//#endif
namesArrayCount = 0;
namesCount = 0;
// initialize other modules (except the last one - must be the ModuleHandler
for(int i=0; i<modulesCount-1; i++)
modules[i].initializeNames();
// will set some MC2 Lisp runtime variables here
}
public NameObjectBase findNameInAllModules( String name ) {
NameObjectBase nm = null;
for(int i=0; i<modulesCount; i++)
if( (nm=modules[i].findName( name ))!=null)
break;
return nm;
}
// with cache
public NameObjectBase createName( String name ) throws EvaluateException {
NameObjectBase nm = findNameInAllModules(name);
if( nm == null ) {
// no such name? will load the module which will load all its names
String classname = getClassName( name );
if( classname != null ) {
FunctionModule fo = getFunctionModule( classname );
if( fo != null ) {
debug("initializing names for "+name);
fo.initializeNames();
debug("names initialized");
// now name must appear in the table
nm = fo.findName( name );
}
}
if( nm == null ) {
// hasn't been initiated
// we cannot initialize BFUNC here because it uses the same namespace as user names
// so BFUNC initializatio is made in static statement
try {
nm = addDataName(name);
}
catch(ArrayIndexOutOfBoundsException e) {
// TODO Autoextend tables?
debug("tables overflow? namesArrayCount="+namesArrayCount+" namesCount="+namesCount);
throw new EvaluateException("name table overflow");
}
}
}
return nm;
}
// retrieves class name where's the Name located
// returns null if there's no class name prefix
private static final String getClassName( String name ) {
// remove function name
int nn = name.lastIndexOf( '.' );
if( nn < 0 )
return null;
return name.substring( 0, nn );
}
// retrieves Function Object for given class name
// loads appropriate module if necessary, caches them in a static modules table
static FunctionModule getFunctionModule( String classname )
throws EvaluateException {
try {
debug("loading class: "+classname);
Class cls = Class.forName( classname );
debug("loaded");
return (FunctionModule)cls.newInstance();
} catch (InstantiationException ie) {
throw new EvaluateException("Error instantiating class for builtin name: " + classname);
} catch (IllegalAccessException iae) {
throw new EvaluateException("Class does not conform FunctionObject interface: " + classname);
} catch (ClassNotFoundException cnfe) {
throw new EvaluateException("Reference to undefined name: " + classname );
}
}
//#ifdef JAVA_COMPILER
public String compileToJavaModule(String functionName, String lispcode) throws ParseException, EvaluateException {
String className = getClassName(functionName);
if( className==null ) {
throw new IllegalArgumentException("modulename must have '.'");
}
String functorName = functionName.substring(className.length()+1);
String packageName = getClassName(className);
if(packageName!=null)
className = className.substring(packageName.length()+1);
startNamesCount = namesCount;
startNamesArrayCount = namesArrayCount;
addFunctionName( functionName ); // put name of module routine
LispObject rez = parse(lispcode);
// preprocess!
rez = rez.preprocess(Bfunc.BFUNC.table[Bfunc.INDEX__IF]);
// System.out.println(rez.toString());
String instanceName = functorName.toUpperCase();
StringBuffer sb = new StringBuffer(1000);
if(packageName!=null)
sb.append("package "+packageName+";\n\n");
sb.append("import com.wapindustrial.calc.*;\n\n");
sb.append("public class "+className+" extends FunctionModule {\n");
sb.append("public static final "+className+' '+instanceName+"= new "+className+"();\n");
sb.append("public "+className+"() {}\n");
sb.append("protected String getModuleClassName() { return \""+packageName+'.'+className+"\"; }\n");
StringBuffer constBuf = new StringBuffer();
StringBuffer constBuf1 = new StringBuffer();
StringBuffer tableBuf = new StringBuffer();
StringBuffer nameBuf = new StringBuffer();
nameBuf.append("private static final String _namesArray = \n");
tableBuf.append("public void initializeNames() {\n");
tableBuf.append("namesArray = _namesArray.getBytes();\n");
tableBuf.append("namesArrayCount = namesArray.length;\n");
tableBuf.append("table = new ModuleName[] {\n");
int namesArrayCount1 = 0;
int nnames = 0;
debug("namesCount="+namesCount);
System.out.println("namesCount="+namesCount);
for(int i=startNamesCount; i<namesCount; i++) {
ModuleName nm = table[i];
// if( nm instanceof UserNameObject )
// continue;
//
String constName = nm.getNameLabel();
// byte name
// nameBuf.append("\t'\\");
// nameBuf.append(Integer.toString(namesArray[nm.offset],8));
// for(int j=0; j<namesArray[nm.offset]; j++) {
// nameBuf.append("','");
// nameBuf.append((char)namesArray[nm.offset+j+1]);
// }
// nameBuf.append("',\n");
// constants
constBuf.append("\tpublic static final int NAME_");
constBuf.append(constName);
constBuf.append(" = ");
constBuf.append( Integer.toString(nm.offset-startNamesArrayCount) );
constBuf.append(";\n");
// constants
constBuf1.append("\tpublic static final int INDEX_");
constBuf1.append(constName);
constBuf1.append(" = ");
constBuf1.append( Integer.toString(i-startNamesCount) );
constBuf1.append(";\n");
// table
if (nm instanceof ModuleNameData) {
tableBuf.append("\tnew ModuleNameData(NAME_"+constName+"),\n");
}
else if(nm instanceof ModuleName) {
tableBuf.append("\tnew ModuleName(NAME_"+constName+"),\n");
}
namesArrayCount1 += namesArray[nm.offset]+1;
nnames++;
}
// writting string constants
int nn = startNamesArrayCount;
while(nn < namesArrayCount) {
int len = namesArray[nn];
nameBuf.append("\t\"\\");
if( len < 64 ) {
nameBuf.append('0');
}
if( len < 8 ) {
nameBuf.append('0');
}
nameBuf.append(Integer.toString(len, 8));
for (int j = 0; j < namesArray[nn]; j++) {
char cc = (char) namesArray[nn + j + 1];
if(cc=='\n')
nameBuf.append("\\n");
else if(cc=='\\')
nameBuf.append("\\\\");
else if(cc=='"')
nameBuf.append("\\\"");
else
nameBuf.append(cc);
}
nameBuf.append("\" + \t// " + (nn-namesArrayCount) + "\n");
nn += len+1;
}
nameBuf.append(" \"\";\n");
tableBuf.append("};\n");
tableBuf.append("namesCount = table.length;\n");
tableBuf.append("};\n"); // end of initializeNames()
sb.append(nameBuf);
sb.append(constBuf);
sb.append(constBuf1);
sb.append(tableBuf);
sb.append("public LispObject evaluate(ModuleName modulename, FunctorList sexp) throws EvaluateException {\n");
sb.append("LispObject rez = NIL;\n");
sb.append("if(modulename==table[0]){\n");
sb.append("ModuleName BFUNC_table[] = Bfunc.BFUNC.table;\n");
sb.append("ModuleName MATHMODULE_table[] = MathModule.MATHMODULE.table;\n");
sb.append("ModuleName SCREENLITE_table[] = ScreenLite.SCREENLITE.table;\n");
sb.append("rez = \n");
rez.toJavaBuffer(sb,0,null);
sb.append(".interpret();\n");
sb.append("}\n"); // end of if()
sb.append("return rez;\n");
sb.append("}\n"); // end of evaluate()
sb.append("}\n"); // end of class
return sb.toString();
}
protected String getModuleClassName() {
return null;
}
//#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -