Revision 44750 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/function/programming/CreateFnFunction.java
CreateFnFunction.java | ||
---|---|---|
21 | 21 |
import org.gvsig.tools.script.Script; |
22 | 22 |
import org.gvsig.tools.script.ScriptManager; |
23 | 23 |
|
24 |
@SuppressWarnings("UseSpecificCatch") |
|
24 | 25 |
public class CreateFnFunction extends AbstractFunction { |
25 | 26 |
|
26 | 27 |
public static final String NAME = "CREATE_FUNCTION"; |
... | ... | |
54 | 55 |
"They have the form:\n" + |
55 | 56 |
"\n" + |
56 | 57 |
"<pre>\n" + |
57 |
"CREATE FUNCTION getCRS (crs) AS 'org.gvsig.fmap.crs.CRSFactory', 'getCRS' LANGUAGE 'java'\n" +
|
|
58 |
"CREATE FUNCTION parseInt(value) AS 'java.lang.Integer', 'parseInt' LANGUAGE 'java'\n" +
|
|
58 | 59 |
"</pre>\n" + |
59 | 60 |
"\n" + |
60 |
"Defines the \"getCRS\" function that receives a single parameter, and links it to the method\n" +
|
|
61 |
"static 'getCRS' of class 'org.gvsig.fmap.crs.CRSFactory',\n" +
|
|
61 |
"Defines the \"parseInt\" function that receives a single parameter, and links it to the method\n" +
|
|
62 |
"static 'parseInt' of the class 'java.lang.Integer',\n" +
|
|
62 | 63 |
"\n" + |
63 | 64 |
"<b> Functions defined in an external script. </b>\n" + |
64 | 65 |
"\n" + |
... | ... | |
67 | 68 |
"They have the form:\n" + |
68 | 69 |
"\n" + |
69 | 70 |
"<pre>\n" + |
70 |
"CREATE FUNCTION getCRS (crs) AS 'scripts / crs.py', 'getCRS' LANGUAGE 'python'\n" +
|
|
71 |
"CREATE FUNCTION getCRS(crs) AS 'crs.py', 'getCRS' LANGUAGE 'script'\n" +
|
|
71 | 72 |
"</pre>\n" + |
72 | 73 |
"\n" + |
73 | 74 |
"What defines a \"getCRS\" function that is implemented in the python module\n" + |
... | ... | |
81 | 82 |
|
82 | 83 |
@Override |
83 | 84 |
public boolean isHidden() { |
84 |
return false; |
|
85 |
return false;
|
|
85 | 86 |
} |
86 | 87 |
|
87 | 88 |
@Override |
... | ... | |
95 | 96 |
} |
96 | 97 |
|
97 | 98 |
@Override |
98 |
public boolean allowArgNames() { |
|
99 |
return true; |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 | 99 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
104 | 100 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
105 | 101 |
} |
106 | 102 |
|
103 |
private static final int FUNCTION_NAME = 0; |
|
104 |
private static final int PARAMETERS = 1; |
|
105 |
private static final int BODY = 2; |
|
106 |
private static final int SCRIPT_PATH = 3; |
|
107 |
private static final int SCRIPT_FUNCTION = 4; |
|
108 |
private static final int LANGUAGE = 5; |
|
109 |
|
|
110 |
private static final int TYPE_USER_FUNCTION = 0; |
|
111 |
private static final int TYPE_JAVA_FUNCTION = 1; |
|
112 |
private static final int TYPE_SCRIPT_FUNCTION = 2; |
|
113 |
|
|
107 | 114 |
@Override |
108 | 115 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
109 | 116 |
if( !(interpreter.getSymbolTable() instanceof MutableSymbolTable) ) { |
... | ... | |
111 | 118 |
} |
112 | 119 |
MutableSymbolTable symbolTable = (MutableSymbolTable) interpreter.getSymbolTable(); |
113 | 120 |
|
114 |
String name = (String) getObject(interpreter, args, "FUNCTION_NAME"); |
|
115 |
List<String> argNames = (List<String>) getObject(interpreter, args, "PARAMETERS"); |
|
116 |
Code body = args.get("BODY"); |
|
117 |
String script_path = (String) getObject(interpreter, args, "SCRIPT_PATH"); |
|
118 |
String script_function = (String) getObject(interpreter, args, "SCRIPT_FUNCTION"); |
|
119 |
String language = (String) getObject(interpreter, args, "LANGUAGE"); |
|
120 |
Function fn; |
|
121 |
if( body!=null ) { |
|
122 |
fn = new UserFunction(name, argNames, body); |
|
123 |
symbolTable.addFunction(fn); |
|
124 |
return fn; |
|
125 |
} |
|
126 |
if( StringUtils.isBlank(script_path) || StringUtils.isBlank(script_function) ) { |
|
127 |
throw new ExpressionRuntimeException("boydy and, script path or script function, are empty."); |
|
128 |
} |
|
121 |
String name = (String) getObject(interpreter, args, FUNCTION_NAME); |
|
122 |
List<String> argNames = (List<String>) getObject(interpreter, args, PARAMETERS); |
|
123 |
Code body = args.get(BODY); |
|
124 |
String script_path = (String) getObject(interpreter, args, SCRIPT_PATH); |
|
125 |
String script_function = (String) getObject(interpreter, args, SCRIPT_FUNCTION); |
|
126 |
String language = (String) getObject(interpreter, args, LANGUAGE); |
|
127 |
|
|
129 | 128 |
if( StringUtils.isBlank(language) ) { |
130 | 129 |
language = "script"; |
131 | 130 |
} |
132 |
switch(language.toLowerCase()) { |
|
133 |
case "script": |
|
134 |
fn = new ExternalFunction(name, script_path, script_function); |
|
135 |
symbolTable.addFunction(fn); |
|
136 |
return fn; |
|
137 |
case "java": |
|
138 |
fn = new JavaFunction(name, script_path, script_function); |
|
139 |
symbolTable.addFunction(fn); |
|
140 |
return fn; |
|
131 |
|
|
132 |
int type = TYPE_USER_FUNCTION; |
|
133 |
if( !StringUtils.isBlank(script_path) || !StringUtils.isBlank(script_function)) { |
|
134 |
switch(language.toLowerCase()) { |
|
135 |
case "script": |
|
136 |
type = TYPE_SCRIPT_FUNCTION; |
|
137 |
break; |
|
138 |
case "java": |
|
139 |
type = TYPE_JAVA_FUNCTION; |
|
140 |
break; |
|
141 |
default: |
|
142 |
throw new ExpressionRuntimeException("Unsupported language '"+language+"."); |
|
143 |
} |
|
141 | 144 |
} |
142 |
throw new ExpressionRuntimeException("Unsupported language '"+language+"."); |
|
145 |
Function fn = null; |
|
146 |
switch(type) { |
|
147 |
case TYPE_USER_FUNCTION: |
|
148 |
fn = new UserFunction(name, argNames, body); |
|
149 |
symbolTable.addFunction(fn); |
|
150 |
break; |
|
151 |
case TYPE_JAVA_FUNCTION: |
|
152 |
if( StringUtils.isBlank(script_path) || StringUtils.isBlank(script_function)) { |
|
153 |
throw new ExpressionRuntimeException("Requiered classname and methodname."); |
|
154 |
} |
|
155 |
fn = new JavaFunction(name, script_path, script_function); |
|
156 |
symbolTable.addFunction(fn); |
|
157 |
break; |
|
158 |
case TYPE_SCRIPT_FUNCTION: |
|
159 |
if( StringUtils.isBlank(script_path) || StringUtils.isBlank(script_function)) { |
|
160 |
throw new ExpressionRuntimeException("Requiered module and function name."); |
|
161 |
} |
|
162 |
fn = new ExternalFunction(name, script_path, script_function); |
|
163 |
symbolTable.addFunction(fn); |
|
164 |
break; |
|
165 |
} |
|
166 |
return fn; |
|
143 | 167 |
} |
144 | 168 |
|
145 | 169 |
private static class UserFunction extends AbstractFunction { |
... | ... | |
154 | 178 |
} |
155 | 179 |
|
156 | 180 |
@Override |
157 |
public boolean allowArgNames() { |
|
158 |
return true; |
|
159 |
} |
|
160 |
|
|
161 |
@Override |
|
162 | 181 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
163 | 182 |
Object value; |
164 | 183 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
... | ... | |
232 | 251 |
|
233 | 252 |
@Override |
234 | 253 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
254 |
List<ClassLoader> loaders = ExpressionEvaluatorLocator.getManager().getClassLoaders(); |
|
255 |
Class<?> theClass = null; |
|
256 |
for (ClassLoader loader : loaders) { |
|
257 |
try { |
|
258 |
theClass = loader.loadClass(this.fullClassName); |
|
259 |
if( theClass!=null ) { |
|
260 |
break; |
|
261 |
} |
|
262 |
} catch(Throwable th) { |
|
263 |
// Do nothing skip |
|
264 |
} |
|
265 |
} |
|
266 |
if( theClass == null ) { |
|
267 |
throw new ExpressionRuntimeException("Can't localte the class '"+this.fullClassName+"'."); |
|
268 |
} |
|
235 | 269 |
Class[] parameterTypes = new Class[args.length]; |
236 | 270 |
for (int i = 0; i < args.length; i++) { |
237 | 271 |
if( args[i]==null ) { |
... | ... | |
240 | 274 |
parameterTypes[i] = args[i].getClass(); |
241 | 275 |
} |
242 | 276 |
} |
243 |
Class<?> theClass = Class.forName(this.fullClassName); |
|
244 | 277 |
Method method = theClass.getMethod(this.methodName, parameterTypes); |
245 | 278 |
Object value = method.invoke(null, args); |
246 | 279 |
return value; |
Also available in: Unified diff