Revision 44533

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/resources/org/gvsig/expressionevaluator/impl/TestScript1_2.txt
19 19
$$
20 20

  
21 21
CREATE FUNCTION saluda AS
22
BEGIN
22 23
    IF LEN($ARGS) > 0 THEN
23 24
        SET s TO 'Hola ' || $ARGS[0];
24 25
    ELSE
......
29 30
END
30 31

  
31 32
CREATE FUNCTION test3 AS
33
BEGIN 
32 34
    RETURN saluda('amigos');
33
END
35
END
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestGrammarCompiler.java
126 126
        Compiler compiler = createCompiler();
127 127

  
128 128
        Code code = compiler.compileExpression(source);
129
        assertEquals("IFF((\"V1\" = 11), LET('V2', 22))", code.toString());
129
        assertEquals("IFF((\"V1\" = 11), LET('V2', 22), NULL)", code.toString());
130 130
    }
131 131

  
132 132
    public void testIfThenElse() {
......
144 144
        Compiler compiler = createCompiler();
145 145

  
146 146
        Code code = compiler.compileExpression(source);
147
        assertEquals("IFF((\"V1\" = 11), BLOCK(LET('V2', 22), LET('V1', 10)))", code.toString());
147
        assertEquals("IFF((\"V1\" = 11), BLOCK(LET('V2', 22), LET('V1', 10)), NULL)", code.toString());
148 148
    }
149 149

  
150 150
    public void testWhile() {
......
243 243
        Compiler compiler = createCompiler();
244 244

  
245 245
        Code code = compiler.compileExpression(source);
246
        assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110)", code.toString());
246
        assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110, NULL)", code.toString());
247 247
    }
248 248
    
249 249
    public void testCase3() {
......
256 256
    }
257 257
    
258 258
    public void testDef1() {
259
        String source = "CREATE PROCEDURE test1 AS print('Hola'); END PROCEDURE"; 
259
        String source = "CREATE PROCEDURE test1 AS BEGIN print('Hola'); END PROCEDURE"; 
260 260

  
261 261
        Compiler compiler = createCompiler();
262 262

  
263 263
        Code code = compiler.compileExpression(source);
264
        assertEquals("CREATE_FUNCTION('test1', print('Hola'))", code.toString());
264
        assertEquals("CREATE_FUNCTION('test1', NULL, print('Hola'), NULL, NULL, NULL)", code.toString());
265 265
    }
266 266
    
267 267
    public void testDef2() {
268
        String source = "CREATE PROCEDURE test1 nombre AS print('Hola '+nombre); END PROCEDURE";
268
        String source = "CREATE PROCEDURE test1 nombre AS BEGIN print('Hola '+nombre); END PROCEDURE";
269 269

  
270 270
        Compiler compiler = createCompiler();
271 271

  
272 272
        Code code = compiler.compileExpression(source);
273
        assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), print(('Hola ' + \"nombre\")))", code.toString());
273
        assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), print(('Hola ' + \"nombre\")), NULL, NULL, NULL)", code.toString());
274 274
    }
275 275

  
276 276
    public void testDef3() {
277
        String source = "CREATE PROCEDURE test1 nombre AS RETURN 'Hola '||nombre; END PROCEDURE";
277
        String source = "CREATE PROCEDURE test1 nombre AS BEGIN RETURN 'Hola '||nombre; END PROCEDURE";
278 278

  
279 279
        Compiler compiler = createCompiler();
280 280

  
281 281
        Code code = compiler.compileExpression(source);
282
        assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), RETURN(('Hola ' || \"nombre\")))", code.toString());
282
        assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), RETURN(('Hola ' || \"nombre\")), NULL, NULL, NULL)", code.toString());
283 283
    }
284
    
285
    public void testCreateFunctionExtern1() {
286
        String source = "CREATE FUNCTION test1(nombre) AS 'addons/test/test', test1";
284 287

  
288
        Compiler compiler = createCompiler();
289

  
290
        Code code = compiler.compileExpression(source);
291
        assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), NULL, 'addons/test/test', 'test1', NULL)", code.toString());
292
    }
293
    
285 294
    public void testList() {
286 295
        String source = "set x TO LIST('hola','adios','fin')";
287 296

  
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestGrammarInterpreter.java
294 294
        Compiler compiler = createCompiler();
295 295
        Interpreter interpreter = createInterpreter(symbolTable);
296 296

  
297
        String source = "CREATE PROCEDURE test1 AS RETURN 'Hola'; END PROCEDURE"; 
297
        String source = "CREATE PROCEDURE test1 AS BEGIN RETURN 'Hola'; END PROCEDURE"; 
298 298
        Code code = compiler.compileExpression(source);
299 299
        Object v = interpreter.run(code);
300 300

  
......
310 310
        Compiler compiler = createCompiler();
311 311
        Interpreter interpreter = createInterpreter(symbolTable);
312 312

  
313
        String source = "CREATE PROCEDURE test1 nombre AS RETURN 'Hola ' || nombre; END PROCEDURE";
313
        String source = "CREATE PROCEDURE test1 nombre AS BEGIN RETURN 'Hola ' || nombre; END PROCEDURE";
314 314
        Code code = compiler.compileExpression(source);
315 315
        Object v = interpreter.run(code);
316 316

  
......
326 326
        Compiler compiler = createCompiler();
327 327
        Interpreter interpreter = createInterpreter(symbolTable);
328 328

  
329
        String source = "CREATE PROCEDURE test1 nombre AS RETURN 'Hola ' || nombre; END PROCEDURE";
329
        String source = "CREATE PROCEDURE test1 nombre AS BEGIN RETURN 'Hola ' || nombre; END PROCEDURE";
330 330
        Code code = compiler.compileExpression(source);
331 331
        Object v = interpreter.run(code);
332 332

  
......
342 342
        Compiler compiler = createCompiler();
343 343
        Interpreter interpreter = createInterpreter(symbolTable);
344 344

  
345
        String source = "CREATE PROCEDURE test1 nombre AS RETURN 'Hola ' || nombre; END PROCEDURE";
345
        String source = "CREATE PROCEDURE test1 nombre AS BEGIN RETURN 'Hola ' || nombre; END PROCEDURE";
346 346
        Code code = compiler.compileExpression(source);
347 347
        Object v = interpreter.run(code);
348 348

  
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestSymbolTable.java
52 52

  
53 53
        assertEquals(0, symbolTable.variables().size());
54 54
        assertEquals(0, symbolTable.functions().size());
55
        assertEquals(0, symbolTable.scripts().size());
55
//        assertEquals(0, symbolTable.scripts().size());
56 56
    }
57 57

  
58 58
    public void testGetter2() {
......
65 65
        
66 66
        assertEquals(0, symbolTable.variables().size());
67 67
        assertFalse(symbolTable.functions().isEmpty());
68
        assertEquals(0, symbolTable.scripts().size());
68
//        assertEquals(0, symbolTable.scripts().size());
69 69
    }
70 70

  
71 71
    public void testSetter1() {
......
78 78

  
79 79
        assertEquals(1, symbolTable.variables().size());
80 80
        assertFalse(symbolTable.functions().isEmpty());
81
        assertEquals(0, symbolTable.scripts().size());
81
//        assertEquals(0, symbolTable.scripts().size());
82 82
    }
83 83

  
84 84
    public void testDelegated1() {
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/I18N.java
76 76
        return String.format("An identifier was expected and '%s' was found", literal);
77 77
    }
78 78
    
79
    public static String A_string_literal_was_expected() {
80
        return String.format("A string literal was expected");
81
    }
82
    
79 83
    public static String A_XTokenX_was_expected_and_XliteralX_was_found(String expected, String found) {
80 84
        return String.format("A '%s' was expected and '%s' was found", expected, found);
81 85
    }
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/DefaultExpressionEvaluatorManager.java
48 48
    private Bookmarks<Expression> bookmarks;
49 49
    private History<Expression> history;
50 50
    private SymbolTable inmutableSymbolTable;
51
    private ResourcesStorage scriptsResourcesStorage;
51 52

  
52 53
    public DefaultExpressionEvaluatorManager() {
53 54
        this.symbolTableFactories = new HashMap<>();
54 55
        this.grammarFactories = new HashMap<>();
56
        this.scriptsResourcesStorage = ResourcesStorage.EMPTY_RESOURCESSTORAGE;
55 57
    }
56 58

  
57 59
    @Override
......
226 228

  
227 229
    @Override
228 230
    public Interpreter createInterpreter() {
229
        return new DefaultInterpreter();
231
        Interpreter interpreter = new DefaultInterpreter();
232
        interpreter.setResourcesStorage(this.scriptsResourcesStorage);
233
        return interpreter;
230 234
    }
231 235

  
232 236
    @Override
......
310 314

  
311 315
    @Override
312 316
    public Script locateScript(String name) {
313
        return null;
317
        return loadScript(this.scriptsResourcesStorage, name);
314 318
    }
315 319

  
316 320
    @Override
......
407 411
        }
408 412

  
409 413
    }
414
    
415
    @Override
416
    public ResourcesStorage getScriptsResourcesStorage() {
417
        return this.scriptsResourcesStorage;
418
    }
419

  
420
    @Override
421
    public void setScriptsResourcesStorage(ResourcesStorage scriptsResourcesStorage) {
422
        this.scriptsResourcesStorage = scriptsResourcesStorage;
423
    }
424
    
410 425
}
411 426

  
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/DefaultSymbolTable.java
1 1
package org.gvsig.expressionevaluator.impl;
2 2

  
3 3
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
4
import java.util.ArrayList;
5 4
import java.util.Collection;
6 5
import java.util.HashMap;
7 6
import org.apache.commons.lang3.StringUtils;
8 7
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
9 8
import org.gvsig.expressionevaluator.Function;
10 9
import org.gvsig.expressionevaluator.MutableSymbolTable;
11
import org.gvsig.tools.script.Script;
12 10

  
13 11
/**
14 12
 *
......
24 22
        super();
25 23
        this.vars = new HashMap<>();
26 24
        this.functions = new HashMap<>();
27
        this.scripts = new ArrayList<>();
25
//        this.scripts = new ArrayList<>();
28 26
        this.init();
29 27
    }
30 28
    
......
69 67
        this.functions.remove(name.toUpperCase());
70 68
    }
71 69

  
72
    @Override
73
    public void addScript(Script script) {
74
        if( script == null ) {
75
            throw new IllegalArgumentException("script can't be null");
76
        }
77
        if (this.scripts.contains(script)) {
78
            return;
79
        }
80
        this.scripts.add(script);
81
    }
70
//    @Override
71
//    public void addScript(Script script) {
72
//        if( script == null ) {
73
//            throw new IllegalArgumentException("script can't be null");
74
//        }
75
//        if (this.scripts.contains(script)) {
76
//            return;
77
//        }
78
//        this.scripts.add(script);
79
//    }
80
//
81
//    @Override
82
//    public void removeScript(Script script) {
83
//        if( script == null ) {
84
//            throw new IllegalArgumentException("script can't be null");
85
//        }
86
//        this.scripts.remove(script);
87
//    }
82 88

  
83 89
    @Override
84
    public void removeScript(Script script) {
85
        if( script == null ) {
86
            throw new IllegalArgumentException("script can't be null");
87
        }
88
        this.scripts.remove(script);
89
    }
90

  
91
    @Override
92 90
    public MutableSymbolTable clone() throws CloneNotSupportedException {
93
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
91
        throw new CloneNotSupportedException("Not supported yet."); 
94 92
    }
95 93
}
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/DefaultOptimizer.java
11 11
import org.gvsig.expressionevaluator.Interpreter;
12 12
import org.gvsig.expressionevaluator.SymbolTable;
13 13
import org.gvsig.expressionevaluator.impl.DefaultCodeBuilder.BaseCodes;
14
import org.gvsig.tools.script.Script;
15 14

  
16 15
/**
17 16
 *
......
90 89
            return this.symbolTable.functions();
91 90
        }
92 91
        
92
//        @Override
93
//        public Collection<Script> scripts() {
94
//            return this.symbolTable.scripts();
95
//        }
96
//
93 97
        @Override
94
        public Collection<Script> scripts() {
95
            return this.symbolTable.scripts();
96
        }
97

  
98
        @Override
99 98
        @SuppressWarnings("CloneDoesntCallSuperClone")
100 99
        public SymbolTable clone() throws CloneNotSupportedException {
101 100
            throw new UnsupportedOperationException("Not supported yet.");
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/grammars/BasicGrammarFactory.java
185 185
            stmt.addRule(stmt.optional_identifiers("PARAM_NAMES", ","));
186 186
            stmt.addRule(stmt.optional_any_token(")"));
187 187
            stmt.addRule(stmt.require_any_token("AS"));
188
            stmt.addRule(stmt.optional_any_token("$$","BEGIN"));
189
            stmt.addRule(stmt.require_expressions("BODY", ";"));
190
            stmt.addRule(stmt.require_any_token("$$", "END"));
191
            stmt.addRule(stmt.optional_any_token("FUNCTION", "PROCEDURE"));
188
            stmt.addRule(stmt.optional_any_token("$$","BEGIN")
189
                    .addRuleOnTrue(stmt.require_expressions("BODY", ";"))
190
                    .addRuleOnTrue(stmt.require_any_token("$$", "END"))
191
                    .addRuleOnTrue(stmt.optional_any_token("FUNCTION", "PROCEDURE"))
192
                    .addRuleOnFalse(stmt.optional_literal_string("SCRIPT_PATH")
193
                        .addRuleOnTrue(stmt.require_any_token(","))
194
                        .addRuleOnTrue(stmt.require_identifier("SCRIPT_FUNCTION"))
195
                        .addRuleOnTrue(stmt.optional_any_token("LANGUAGE")
196
                                .addRuleOnTrue(stmt.require_literal_string("LANGUAGE"))
197
                        )
198
                    )
199
            );
200
                    
201
//            stmt.addRule(stmt.require_expressions("BODY", ";"));
202
//            stmt.addRule(stmt.require_any_token("$$", "END"));
203
//            stmt.addRule(stmt.optional_any_token("FUNCTION", "PROCEDURE"));
192 204
            stmt.code(
193 205
                    CreateFnFunction.NAME,
194
                    stmt.args_names("FUNCTION_NAME","PARAM_NAMES","BODY")
206
                    stmt.args_names("FUNCTION_NAME","PARAM_NAMES","BODY", "SCRIPT_PATH", "SCRIPT_FUNCTION", "LANGUAGE")
195 207
            ); 
196 208
            theGrammar.addStatement(stmt);
197 209

  
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/DefaultExpression.java
1 1
package org.gvsig.expressionevaluator.impl;
2 2

  
3
import java.net.URI;
4
import java.net.URL;
5
import java.util.ArrayList;
6
import java.util.Iterator;
7
import java.util.List;
8 3
import java.util.Objects;
9 4
import org.apache.commons.lang3.StringUtils;
10 5
import org.gvsig.expressionevaluator.Code;
......
21 16
import org.gvsig.tools.persistence.PersistenceManager;
22 17
import org.gvsig.tools.persistence.PersistentState;
23 18
import org.gvsig.tools.persistence.exception.PersistenceException;
24
import org.gvsig.tools.script.Script;
25 19
import org.gvsig.tools.script.ScriptManager;
26 20
import org.gvsig.tools.util.LabeledValue;
27
import org.gvsig.tools.util.UnmodifiableBasicList;
28
import org.gvsig.tools.util.UnmodifiableBasicListAdapter;
29
import org.json.JSONArray;
30 21
import org.json.JSONObject;
31 22

  
32 23
/**
......
36 27
public class DefaultExpression implements Expression, LabeledValue<Expression> {
37 28

  
38 29
    private String phrase = null;
39
    private Script userScript = null;
40
    private List<Script> scripts = null;
41
    private UnmodifiableBasicList<Script> unmodifiableScripts = null;
30
//    private Script userScript = null;
31
//    private List<Script> scripts = null;
32
//    private UnmodifiableBasicList<Script> unmodifiableScripts = null;
42 33

  
43 34
    private Code code = null;
44 35
    private Interpreter interpreter;
......
102 93
        if( !StringUtils.isBlank(this.phrase) ) {
103 94
            return false;
104 95
        }
105
        if( this.scripts!=null && !this.scripts.isEmpty() ) {
106
            return false;
107
        }
108
        if( this.userScript!=null && !StringUtils.isBlank(this.userScript.getCode()) ) {
109
            return false;
110
        }
96
//        if( this.scripts!=null && !this.scripts.isEmpty() ) {
97
//            return false;
98
//        }
99
//        if( this.userScript!=null && !StringUtils.isBlank(this.userScript.getCode()) ) {
100
//            return false;
101
//        }
111 102
        return true;
112 103
    }
113 104

  
105
//    @Override
106
//    public Script getUserScript() {
107
//        return this.userScript;
108
//    }
109
//
110
//    @Override
111
//    public UnmodifiableBasicList<Script> getScripts() {
112
//        if (this.unmodifiableScripts == null) {
113
//            if (this.scripts == null) {
114
//                return null;
115
//            }
116
//            this.unmodifiableScripts = new UnmodifiableBasicListAdapter<>(this.scripts);
117
//        }
118
//        return this.unmodifiableScripts;
119
//    }
120
//
114 121
    @Override
115
    public Script getUserScript() {
116
        return this.userScript;
117
    }
118

  
119
    @Override
120
    public UnmodifiableBasicList<Script> getScripts() {
121
        if (this.unmodifiableScripts == null) {
122
            if (this.scripts == null) {
123
                return null;
124
            }
125
            this.unmodifiableScripts = new UnmodifiableBasicListAdapter<>(this.scripts);
126
        }
127
        return this.unmodifiableScripts;
128
    }
129

  
130
    @Override
131 122
    public Expression setPhrase(String phrase) {
132 123
        this.phrase = phrase;
133 124
        this.code = null;
......
135 126
        return this;
136 127
    }
137 128

  
138
    @Override
139
    public Expression setUserScript(String code, String languaje) {
140
        if (this.userScript == null) {
141
            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
142
            this.userScript = scriptMananger.createScript("user", code, languaje);
143
        } else if (this.userScript.getTypeName().equalsIgnoreCase(languaje)) {
144
            this.userScript.setCode(code);
145
        } else {
146
            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
147
            this.userScript = scriptMananger.createScript("user", code, languaje);
148
        }
149
        return this;
150
    }
129
//    @Override
130
//    public Expression setUserScript(String code, String languaje) {
131
//        if (this.userScript == null) {
132
//            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
133
//            this.userScript = scriptMananger.createScript("user", code, languaje);
134
//        } else if (this.userScript.getTypeName().equalsIgnoreCase(languaje)) {
135
//            this.userScript.setCode(code);
136
//        } else {
137
//            ScriptManager scriptMananger = ToolsLocator.getScriptManager();
138
//            this.userScript = scriptMananger.createScript("user", code, languaje);
139
//        }
140
//        return this;
141
//    }
142
//
143
//    @Override
144
//    public Expression setUserScript(Script script) {
145
//        this.userScript = script;
146
//        return this;
147
//    }
148
//
149
//    @Override
150
//    public Expression setUserScript(String code) {
151
//        this.setUserScript(code, "python");
152
//        return this;
153
//    }
154
//
155
//    @Override
156
//    public void removeAllScripts() {
157
//        this.scripts = null;
158
//        this.unmodifiableScripts = null;
159
//    }
160
//
161
//    @Override
162
//    public Expression addScript(Script script) {
163
//        if (this.scripts == null) {
164
//            this.scripts = new ArrayList<>();
165
//        }
166
//        this.scripts.add(script);
167
//        return this;
168
//    }
151 169

  
152 170
    @Override
153
    public Expression setUserScript(Script script) {
154
        this.userScript = script;
155
        return this;
156
    }
157

  
158
    @Override
159
    public Expression setUserScript(String code) {
160
        this.setUserScript(code, "python");
161
        return this;
162
    }
163

  
164
    @Override
165
    public void removeAllScripts() {
166
        this.scripts = null;
167
        this.unmodifiableScripts = null;
168
    }
169

  
170
    @Override
171
    public Expression addScript(Script script) {
172
        if (this.scripts == null) {
173
            this.scripts = new ArrayList<>();
174
        }
175
        this.scripts.add(script);
176
        return this;
177
    }
178

  
179
    @Override
180 171
    public void clear() {
181 172
        this.phrase = null;
182
        this.userScript = null;
183
        this.unmodifiableScripts = null;
184
        this.scripts = null;
173
//        this.userScript = null;
174
//        this.unmodifiableScripts = null;
175
//        this.scripts = null;
185 176
        this.code = null;
186 177
        this.interpreter = null;
187 178
        this.hasNotBeenOptimized = true;
......
260 251
    @Override
261 252
    public void saveToState(PersistentState state) throws PersistenceException {
262 253
        state.set("phrase", this.phrase);
263
        if (this.userScript == null) {
264
            state.setNull("userScript_code");
265
            state.setNull("userScript_language");
266
        } else {
267
            state.set("userScript_code", this.userScript.getCode());
268
            state.set("userScript_language", this.userScript.getTypeName());
269
        }
270
        if (this.scripts != null && !this.scripts.isEmpty()) {
271
            List<URL> l = new ArrayList<>();
272
            for (Script script : this.scripts) {
273
                URL location = script.getURL();
274
                if (location != null) {
275
                    l.add(location);
276
                }
277
            }
278
            if (l.isEmpty()) {
279
                state.setNull("scripts");
280
            } else {
281
                state.set("scripts", l);
282
            }
283
        } else {
284
            state.setNull("scripts");
285
        }
254
//        if (this.userScript == null) {
255
//            state.setNull("userScript_code");
256
//            state.setNull("userScript_language");
257
//        } else {
258
//            state.set("userScript_code", this.userScript.getCode());
259
//            state.set("userScript_language", this.userScript.getTypeName());
260
//        }
261
//        if (this.scripts != null && !this.scripts.isEmpty()) {
262
//            List<URL> l = new ArrayList<>();
263
//            for (Script script : this.scripts) {
264
//                URL location = script.getURL();
265
//                if (location != null) {
266
//                    l.add(location);
267
//                }
268
//            }
269
//            if (l.isEmpty()) {
270
//                state.setNull("scripts");
271
//            } else {
272
//                state.set("scripts", l);
273
//            }
274
//        } else {
275
//            state.setNull("scripts");
276
//        }
286 277
    }
287 278

  
288 279
    @Override
......
292 283
        this.clear();
293 284
        
294 285
        this.phrase = state.getString("phrase");
295
        String userScript_code = state.getString("userScript_code");
296
        String userScript_language = state.getString("userScript_language");
297
        if (StringUtils.isEmpty(userScript_code)) {
298
            this.userScript = null;
299
        } else {
300
            if (StringUtils.isEmpty(userScript_language)) {
301
                userScript_language = "python";
302
            }
303
            this.userScript = scriptManager.createScript("user", userScript_code, userScript_language);
304
        }
305
        Iterator scriptsLocations = state.getIterator("scripts");
306
        if (scriptsLocations != null) {
307
            while (scriptsLocations.hasNext()) {
308
                URI location = (URI) scriptsLocations.next();
309
                Script script = scriptManager.loadScript(location);
310
                if (script != null) {
311
                    if (this.scripts == null) {
312
                        this.scripts = new ArrayList<>();
313
                    }
314
                    this.scripts.add(script);
315
                }
316
            }
317
        }
286
//        String userScript_code = state.getString("userScript_code");
287
//        String userScript_language = state.getString("userScript_language");
288
//        if (StringUtils.isEmpty(userScript_code)) {
289
//            this.userScript = null;
290
//        } else {
291
//            if (StringUtils.isEmpty(userScript_language)) {
292
//                userScript_language = "python";
293
//            }
294
//            this.userScript = scriptManager.createScript("user", userScript_code, userScript_language);
295
//        }
296
//        Iterator scriptsLocations = state.getIterator("scripts");
297
//        if (scriptsLocations != null) {
298
//            while (scriptsLocations.hasNext()) {
299
//                URI location = (URI) scriptsLocations.next();
300
//                Script script = scriptManager.loadScript(location);
301
//                if (script != null) {
302
//                    if (this.scripts == null) {
303
//                        this.scripts = new ArrayList<>();
304
//                    }
305
//                    this.scripts.add(script);
306
//                }
307
//            }
308
//        }
318 309
    }
319 310

  
320 311
    public static void registerPersistence() {
......
323 314
            DynStruct definition = manager.addDefinition(DefaultExpression.class,
324 315
                    "Expression", "Expression persistence definition", null, null);
325 316
            definition.addDynFieldString("phrase").setMandatory(false);
326
            definition.addDynFieldString("userScript_code").setMandatory(false);
327
            definition.addDynFieldString("userScript_language").setMandatory(false);
328
            definition.addDynFieldList("scripts")
329
                    .setClassOfItems(URL.class)
330
                    .setMandatory(false);
317
//            definition.addDynFieldString("userScript_code").setMandatory(false);
318
//            definition.addDynFieldString("userScript_language").setMandatory(false);
319
//            definition.addDynFieldList("scripts")
320
//                    .setClassOfItems(URL.class)
321
//                    .setMandatory(false);
331 322
        }
332 323
    }
333 324

  
......
336 327
        JSONObject expressionJson = new JSONObject();
337 328
        expressionJson.put("phrase", this.phrase);
338 329

  
339
        if (this.userScript != null) {
340
            JSONObject userScriptJson = new JSONObject();
341
            userScriptJson.put("code", this.userScript.getCode());
342
            userScriptJson.put("language", this.userScript.getTypeName());
343
            expressionJson.put("userScript", userScriptJson);
344
        }
345

  
346
        if (this.scripts != null && !this.scripts.isEmpty()) {
347
            JSONArray scriptsJson = new JSONArray();
348
            for (Script script : this.scripts) {
349
                scriptsJson.put(script.getURL());
350
            }
351
            expressionJson.put("scripts", scriptsJson);
352
        }
330
//        if (this.userScript != null) {
331
//            JSONObject userScriptJson = new JSONObject();
332
//            userScriptJson.put("code", this.userScript.getCode());
333
//            userScriptJson.put("language", this.userScript.getTypeName());
334
//            expressionJson.put("userScript", userScriptJson);
335
//        }
336
//
337
//        if (this.scripts != null && !this.scripts.isEmpty()) {
338
//            JSONArray scriptsJson = new JSONArray();
339
//            for (Script script : this.scripts) {
340
//                scriptsJson.put(script.getURL());
341
//            }
342
//            expressionJson.put("scripts", scriptsJson);
343
//        }
353 344
        return expressionJson.toString();
354 345
    }
355 346

  
......
362 353
        if (expressionJson.has("phrase")) {
363 354
            this.phrase = expressionJson.getString("phrase");
364 355
        }
365
        if (expressionJson.has("userScript")) {
366
            String theCode = "";
367
            String theLanguage = "python";
368
            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
369
            if (userScriptJson.has("code")) {
370
                theCode = userScriptJson.getString("code");
371
            }
372
            if (userScriptJson.has("language")) {
373
                theCode = userScriptJson.getString("language");
374
            }
375
            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
376
        }
377
        if (expressionJson.has("scripts")) {
378
            this.scripts = new ArrayList<>();
379
            JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
380
            for (Object object : scriptsJson) {
381
                URI location = (URI) object;
382
                Script script = scriptMananger.loadScript(location);
383
                this.scripts.add(script);
384
            }
385
        }
356
//        if (expressionJson.has("userScript")) {
357
//            String theCode = "";
358
//            String theLanguage = "python";
359
//            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
360
//            if (userScriptJson.has("code")) {
361
//                theCode = userScriptJson.getString("code");
362
//            }
363
//            if (userScriptJson.has("language")) {
364
//                theCode = userScriptJson.getString("language");
365
//            }
366
//            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
367
//        }
368
//        if (expressionJson.has("scripts")) {
369
//            this.scripts = new ArrayList<>();
370
//            JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
371
//            for (Object object : scriptsJson) {
372
//                URI location = (URI) object;
373
//                Script script = scriptMananger.loadScript(location);
374
//                this.scripts.add(script);
375
//            }
376
//        }
386 377
    }
387 378

  
388 379
    @Override
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
1 1
package org.gvsig.expressionevaluator.impl.function.programming;
2 2

  
3
import java.lang.reflect.Method;
3 4
import java.util.ArrayList;
4 5
import java.util.Arrays;
5 6
import java.util.List;
7
import java.util.Objects;
6 8
import org.apache.commons.lang3.Range;
9
import org.apache.commons.lang3.StringUtils;
7 10
import org.gvsig.expressionevaluator.Code;
8 11
import org.gvsig.expressionevaluator.Codes;
9 12
import org.gvsig.expressionevaluator.Function;
......
14 17
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
15 18
import org.gvsig.expressionevaluator.MutableSymbolTable;
16 19
import org.gvsig.expressionevaluator.SymbolTable;
20
import org.gvsig.tools.ToolsLocator;
21
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
22
import org.gvsig.tools.script.Script;
23
import org.gvsig.tools.script.ScriptManager;
17 24

  
18 25
public class CreateFnFunction extends AbstractFunction {
19 26
    
......
22 29
    public CreateFnFunction() {
23 30
        super(Function.GROUP_PROGRAMMING, 
24 31
                NAME, 
25
                Range.between(2, 3),
32
                Range.between(2, 6),
26 33
                null,
27 34
                null,
28 35
                null,
......
56 63
        
57 64
        String name;
58 65
        List<String> argNames;
59
        Code body;
66
        Code body = null;
67
        String script_path = null;
68
        String script_function = null;
69
        String language = null;
60 70
        switch(args.size()) {
61 71
            case 2:
62 72
                name = (String) getObject(interpreter, args, 0);
......
72 82
                }
73 83
                body = args.get(2);
74 84
                break;
85
            case 6:
86
                name = (String) getObject(interpreter, args, 0);
87
                if( args.get(1)==null ) {
88
                    argNames = null;
89
                } else {
90
                    argNames = (List<String>) getObject(interpreter, args, 1);
91
                }
92
                body = args.get(2);
93
                script_path = Objects.toString(args.get(2),null);
94
                script_function = Objects.toString(args.get(3),null);
95
                language = Objects.toString(args.get(4),null);
96
                break;
75 97
            default:
76 98
                throw new ExpressionRuntimeException("Incorrect number of arguments");
77 99
        }
78
        
79
        Function fn = new UserFunction(name, argNames, body);
80
        symbolTable.addFunction(fn);
81
        return fn;
100
        Function fn;
101
        if( body!=null ) {
102
            fn = new UserFunction(name, argNames, body);
103
            symbolTable.addFunction(fn);
104
            return fn;
105
        }
106
        if( StringUtils.isBlank(script_path) || StringUtils.isBlank(script_function) ) {
107
            throw new ExpressionRuntimeException("boydy and, script path or script function, are empty.");
108
        }
109
        if( StringUtils.isBlank(language) ) {
110
            language = "script";
111
        }
112
        switch(language.toLowerCase()) {
113
            case "script":
114
                fn = new ExternalFunction(name, script_path, script_function);
115
                symbolTable.addFunction(fn);
116
                return fn;
117
            case "java":
118
                fn = new JavaFunction(name, script_path, script_function);
119
                symbolTable.addFunction(fn);
120
                return fn;
121
        }
122
        throw new ExpressionRuntimeException("Unsupported language '"+language+".");
82 123
    }
83 124
    
84 125
    private static class UserFunction extends AbstractFunction {
......
126 167
        
127 168
    }
128 169
    
170
    private static class ExternalFunction extends AbstractFunction {
171

  
172
        private final String script_path;
173
        private final String script_function;
174

  
175
        public ExternalFunction(String name, String script_path, String script_function) {
176
            super(GROUP_OTHER, name, Range.between(0, Integer.MAX_VALUE));
177
            this.script_path = script_path;
178
            this.script_function = script_function;
179
        }
180
        
181
        @Override
182
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
183
            ScriptManager scriptManager = ToolsLocator.getScriptManager();
184
            ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getExpressionEvaluatorManager();
185
            ResourcesStorage resourcesStorage = expressionManager.getScriptsResourcesStorage();
186
            Script sc = scriptManager.loadScript(resourcesStorage, script_path);
187
            if( sc == null ) {
188
                throw new ExpressionRuntimeException("Can't locate '"+this.script_path+"'.");
189
            }
190
            Object r = sc.invokeFunction(this.script_function, args);
191
            return r;
192
        }
193
        
194
    }
195

  
196
    private static class JavaFunction extends AbstractFunction {
197

  
198
        private final String fullClassName;
199
        private final String methodName;
200

  
201
        public JavaFunction(String name, String fullClassName, String methodName) {
202
            super(GROUP_OTHER, name, Range.between(0, Integer.MAX_VALUE));
203
            this.fullClassName = fullClassName;
204
            this.methodName = methodName;
205
        }
206
        
207
        @Override
208
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
209
            Class[] parameterTypes = new Class[args.length];
210
            for (int i = 0; i < args.length; i++) {
211
                if( args[i]==null ) {
212
                    parameterTypes[i] = null;
213
                } else {
214
                    parameterTypes[i] = args[i].getClass();
215
                }
216
            }
217
            Class<?> theClass = Class.forName(this.fullClassName);
218
            Method method = theClass.getMethod(this.methodName, parameterTypes);
219
            Object value = method.invoke(null, args);
220
            return value;
221
        }
222
        
223
    }
224

  
129 225
}
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/DefaultInterpreter.java
166 166
        if( fn == null ) {
167 167
            return false;
168 168
        }
169
        if( fn instanceof AbstractSymbolTable.ScriptFunction ) {
170
            return false;
171
        }
169
//        if( fn instanceof AbstractSymbolTable.ScriptFunction ) {
170
//            return false;
171
//        }
172 172
        return true;
173 173
    }
174 174
    
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/DefaultStatement.java
100 100
        }
101 101
    }
102 102

  
103
    public class RuleRequireLiteralString implements Rule {
104

  
105
        private final String id;
106

  
107
        public RuleRequireLiteralString(String id) {
108
            this.id = id;
109
        }
110

  
111
        @Override
112
        public void parse(StatementContext context) {
113
            Token token = context.look_token();
114
            if (token.getType() != Token.STRING_LITERAL) {
115
                throw new ExpressionSyntaxException(
116
                        I18N.A_string_literal_was_expected(),
117
                        context.getLexicalAnalyzer()
118
                );
119
            }
120
            String identifier = (String) token.getLiteral();
121
            Code code = context.getCodeBuilder().constant(identifier);
122
            context.setCode(id, code);
123
            context.next_token();
124
        }
125

  
126
        @Override
127
        public String toString() {
128
            return "require_literal_string('" + this.id + "')";
129
        }
130
    }
131

  
103 132
    public class RuleRequireExpression implements Rule {
104 133

  
105 134
        private final String id;
......
180 209
        }
181 210
    }
182 211

  
212
    public abstract class AbstractConditionalRule implements ConditionalRule {
213

  
214
        protected final List<Rule> onTrueRules;
215
        protected final List<Rule> onFalseRules;
216

  
217
        public AbstractConditionalRule() {
218
            this.onTrueRules = new ArrayList<>();
219
            this.onFalseRules = new ArrayList<>();
220
        }
221

  
222
        protected void parseOnTrueRules(StatementContext context) {
223
            for (Rule rule : this.onTrueRules) {
224
                rule.parse(context);
225
            }
226
        }
227

  
228
        protected void parseOnFalseRules(StatementContext context) {
229
            for (Rule rule : this.onFalseRules) {
230
                rule.parse(context);
231
            }
232
        }
233

  
234
        @Override
235
        public ConditionalRule addRuleOnTrue(Rule rule) {
236
            this.onTrueRules.add(rule);
237
            return this;
238
        }
239

  
240
        @Override
241
        public ConditionalRule addRuleOnFalse(Rule rule) {
242
            this.onFalseRules.add(rule);
243
            return this;
244
        }
245

  
246
    }
247

  
183 248
    public class RuleOptionalAnyToken implements ConditionalRule {
184 249

  
185 250
        private final String[] optional_token;
......
275 340
        }
276 341
    }
277 342

  
278
    public class RuleOptionalIdentifiers implements Rule {
343
    public class RuleOptionalIdentifiers extends AbstractConditionalRule implements ConditionalRule {
279 344

  
280 345
        private final String id;
281 346
        private final String separator;
282 347

  
283 348
        public RuleOptionalIdentifiers(String id, String separator) {
349
            super();
284 350
            this.id = id;
285 351
            this.separator = separator;
286 352
        }
......
304 370
                context.next_token();
305 371
                token = context.look_token();
306 372
            }
307
            Code code = null;
308 373
            if (args.size() != 0) {
309
                code = context.getCodeBuilder().function(FUNCTION_LIST, args);
374
                Code code = context.getCodeBuilder().function(FUNCTION_LIST, args);
375
                context.setCode(id, code);
376
                this.parseOnTrueRules(context);
377
            } else {
378
                this.parseOnFalseRules(context);
310 379
            }
311
            context.setCode(id, code);
312 380
        }
313 381

  
314 382
        @Override
......
317 385
        }
318 386
    }
319 387

  
388
    public class RuleOptionalLiteralString extends AbstractConditionalRule implements ConditionalRule {
389

  
390
        private final String id;
391

  
392
        public RuleOptionalLiteralString(String id) {
393
            super();
394
            this.id = id;
395
        }
396

  
397
        @Override
398
        public void parse(StatementContext context) {
399
            Token token = context.look_token();
400
            if (token.getType() == Token.STRING_LITERAL) {
401
                String s = (String) token.getLiteral();
402
                Code code = context.getCodeBuilder().constant(s);
403
                context.setCode(id, code);
404
                context.next_token();
405
                this.parseOnTrueRules(context);
406
            } else {
407
                this.parseOnFalseRules(context);
408
            }
409
        }
410

  
411
        @Override
412
        public String toString() {
413
            return "optional_literal_string('" + id + "')";
414
        }
415
    }
416

  
320 417
    public static class ArgsBuilderFromNames implements ArgsBuilder {
321 418

  
322 419
        private final String[] argNames;
......
342 439
                    }
343 440
                } else {
344 441
                    Code code = context.getCode(argName);
345
                    if( code != null) {
346
                        args.add(code);
442
                    if( code == null) {
443
                        code = context.getCodeBuilder().constant(null);
347 444
                    }
445
                    args.add(code);
348 446
                }
349 447
            }
350 448
            return args;
......
447 545
    }
448 546

  
449 547
    @Override
548
    public Rule require_literal_string(String id) {
549
        return new RuleRequireLiteralString(id);
550
    }
551

  
552
    @Override
450 553
    public Rule set_expression(String id, Object value) {
451 554
        return new RuleSetExpression(id, value);
452 555
    }
......
467 570
    }
468 571

  
469 572
    @Override
470
    public Rule optional_identifiers(String id, String separator) {
573
    public ConditionalRule optional_identifiers(String id, String separator) {
471 574
        return new RuleOptionalIdentifiers(id, separator);
472 575
    }
473 576

  
474 577
    @Override
578
    public ConditionalRule optional_literal_string(String id) {
579
        return new RuleOptionalLiteralString(id);
580
    }
581

  
582
    @Override
475 583
    public CompoundRule repeat_until_any_tokens(String... tokens) {
476 584
        return new RuleRepeatUntilAnyTokens(tokens);
477 585
    }
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/ExpressionUtils.java
62 62
        return expression;
63 63
    }
64 64

  
65
    public static Expression createExpression(String phrase, String code, Script... scripts) {
66
        Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
67
        expression.setPhrase(phrase);
68
        expression.setUserScript(code);
69
        for (Script script : scripts) {
70
            expression.addScript(script);
71
        }
72
        return expression;
73
    }
65
//    public static Expression createExpression(String phrase, String code, Script... scripts) {
66
//        Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
67
//        expression.setPhrase(phrase);
68
//        expression.setUserScript(code);
69
//        for (Script script : scripts) {
70
//            expression.addScript(script);
71
//        }
72
//        return expression;
73
//    }
74
//
75
//    public static Expression createExpression(String phrase, String code, String languaje, Script... scripts) {
76
//        Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
77
//        expression.setPhrase(phrase);
78
//        expression.setUserScript(code, languaje);
79
//        for (Script script : scripts) {
80
//            expression.addScript(script);
81
//        }
82
//        return expression;
83
//    }
74 84

  
75
    public static Expression createExpression(String phrase, String code, String languaje, Script... scripts) {
76
        Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
77
        expression.setPhrase(phrase);
78
        expression.setUserScript(code, languaje);
79
        for (Script script : scripts) {
80
            expression.addScript(script);
81
        }
82
        return expression;
83
    }
84

  
85 85
    public static ExpressionBuilder createExpressionBuilder() {
86 86
        ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder();
87 87
        return builder;
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/MutableSymbolTable.java
10 10
    
11 11
    public void addFunction(Function function);
12 12
    
13
    public void addScript(Script script);
13
//    public void addScript(Script script);
14 14
    
15 15
    public void addFunctions(Collection<Function> functions);
16 16

  
17 17
    public void removeVar(String name);
18 18
    
19
    public void removeScript(Script script);
19
//    public void removeScript(Script script);
20 20
    
21 21
    public void removeFunction(String name);
22 22

  
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/Expression.java
25 25
    
26 26
    public String getPhrase();
27 27
    
28
    public Script getUserScript(); 
29
    
30
    public UnmodifiableBasicList<Script> getScripts();
28
//    public Script getUserScript(); 
29
//    
30
//    public UnmodifiableBasicList<Script> getScripts();
31 31

  
32 32
    public Expression setPhrase(String phrase);
33 33
    
34
    public Expression setUserScript(String code, String languaje);
34
//    public Expression setUserScript(String code, String languaje);
35
//    
36
//    public Expression setUserScript(String code);
37
//    
38
//    public Expression setUserScript(Script script);
39
//    
40
//    public void removeAllScripts();
41
//    
42
//    public Expression addScript(Script script);
35 43
    
36
    public Expression setUserScript(String code);
37
    
38
    public Expression setUserScript(Script script);
39
    
40
    public void removeAllScripts();
41
    
42
    public Expression addScript(Script script);
43
    
44 44
    public Object execute(SymbolTable symbolTable);
45 45
    
46 46
    public void link(SymbolTable symbolTable);    
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/Statement.java
76 76

  
77 77
    public Rule require_identifier(String id);
78 78

  
79
    public Rule require_literal_string(String id);
80

  
79 81
    public Rule set_expression(String id, Object value);
80 82

  
81 83
    public Rule require_expression(String id);
......
84 86

  
85 87
    public ConditionalRule optional_any_token(String... id);
86 88

  
87
    public Rule optional_identifiers(String id, String separator);
88

  
89
    public ConditionalRule optional_identifiers(String id, String separator);
90
    
91
    public ConditionalRule optional_literal_string(String id);
92
        
89 93
    public ArgsBuilder args_names(String... args);
90 94
    
91 95
    public ArgsBuilder args_expand(String... args);
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/spi/AbstractSymbolTable.java
9 9
import java.util.List;
10 10
import java.util.Map;
11 11
import java.util.Set;
12
import org.apache.commons.lang3.Range;
13 12
import org.apache.commons.lang3.StringUtils;
14 13
import org.gvsig.expressionevaluator.Function;
15
import org.gvsig.expressionevaluator.Interpreter;
16 14
import org.gvsig.expressionevaluator.SymbolTable;
17
import org.gvsig.tools.script.Script;
18 15

  
19 16
/**
20 17
 *
......
24 21

  
25 22
    private final String name;
26 23

  
27
    public class ScriptFunction extends AbstractFunction {
24
//    public class ScriptFunction extends AbstractFunction {
25
//
26
//        public ScriptFunction(String funcName) {
27
//            super("Script", funcName, Range.between(1, Integer.MAX_VALUE));
28
//        }
29
//
30
//        @Override
31
//        public Object call(Interpreter interpreter, Object[] args) throws Exception {
32
//            for (Script script : scripts()) {
33
//                try {
34
//                    return script.invokeFunction(this.name(), args);
35
//                } catch (NoSuchMethodException ex) {
36
//                    // Si la funcion no existe en el script pasamos al siguiente
37
//                    // script.
38
//                }
39
//            }
40
//            throw new NoSuchMethodException("Can't locate funcion '" + this.name() + "'.");
41
//        }
42
//
43
//    }
28 44

  
29
        public ScriptFunction(String funcName) {
30
            super("Script", funcName, Range.between(1, Integer.MAX_VALUE));
31
        }
32

  
33
        @Override
34
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
35
            for (Script script : scripts()) {
36
                try {
37
                    return script.invokeFunction(this.name(), args);
38
                } catch (NoSuchMethodException ex) {
39
                    // Si la funcion no existe en el script pasamos al siguiente
40
                    // script.
41
                }
42
            }
43
            throw new NoSuchMethodException("Can't locate funcion '" + this.name() + "'.");
44
        }
45

  
46
    }
47

  
48 45
    protected final List<SymbolTable> symbolTables;
49 46
    protected Map<String, Object> vars;
50 47
    protected Map<String, Function> functions;
51 48
    protected Map<String, Function> functionAlias;
52
    protected List<Script> scripts;
49
//    protected List<Script> scripts;
53 50

  
54 51
    public AbstractSymbolTable() {
55 52
        this(null);
......
61 58
        this.vars = null;
62 59
        this.functions = null;
63 60
        this.functionAlias = null;
64
        this.scripts = null;
61
//        this.scripts = null;
65 62
    }
66 63

  
67 64
    @Override
......
127 124
        return this.functionAlias;
128 125
    }
129 126

  
130
    protected List<Script> getScripts() {
131
        if (this.scripts == null) {
132
            this.scripts = new ArrayList<>();
133
        }
134
        return this.scripts;
135
    }
136

  
127
//    protected List<Script> getScripts() {
128
//        if (this.scripts == null) {
129
//            this.scripts = new ArrayList<>();
130
//        }
131
//        return this.scripts;
132
//    }
133
//
137 134
    @Override
138 135
    public boolean exists(String name) {
139 136
        if (StringUtils.isEmpty(name)) {
......
191 188
        for (SymbolTable other : this.symbolTables) {
192 189
            Function fn = other.function(name);
193 190
            if (fn != null) {
194
                if (fn instanceof ScriptFunction) {
195
                    continue;
196
                }
191
//                if (fn instanceof ScriptFunction) {
192
//                    continue;
193
//                }
197 194
                return fn;
198 195
            }
199 196
        }
200
        if (this.scripts == null || this.scripts.isEmpty()) {
201
            return null;
202
        }
203
        return new ScriptFunction(name);
197
//        if (this.scripts != null && ! this.scripts.isEmpty()) {
198
//            return new ScriptFunction(name);
199
//        }
200
        return null;
204 201
    }
205 202

  
206 203
    @Override
......
241 238
        return Collections.unmodifiableCollection(this.vars.keySet());
242 239
    }
243 240

  
241
//    @Override
242
//    public Collection<Script> scripts() {
243
//        Set<Script> theScripts = new HashSet<>();
244
//        for (SymbolTable symbolTable : this.symbolTables) {
245
//            theScripts.addAll(symbolTable.scripts());
246
//        }
247
//        if (this.scripts != null) {
248
//            theScripts.addAll(this.scripts);
249
//        }
250
//        return Collections.unmodifiableCollection(theScripts);
251
//    }
252
//
244 253
    @Override
245
    public Collection<Script> scripts() {
246
        Set<Script> theScripts = new HashSet<>();
247
        for (SymbolTable symbolTable : this.symbolTables) {
248
            theScripts.addAll(symbolTable.scripts());
249
        }
250
        if (this.scripts != null) {
251
            theScripts.addAll(this.scripts);
252
        }
253
        return Collections.unmodifiableCollection(theScripts);
254
    }
255

  
256
    @Override
257 254
    public Iterator<Function> iterator() {
258 255
        return this.functions().iterator();
259 256
    }
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/ExpressionEvaluatorManager.java
2 2
package org.gvsig.expressionevaluator;
3 3

  
4 4
import java.util.Collection;
5
import org.apache.commons.lang3.ArrayUtils;
6
import org.apache.commons.lang3.StringUtils;
7 5
import org.gvsig.tools.bookmarksandhistory.Bookmarks;
8 6
import org.gvsig.tools.bookmarksandhistory.History;
7
import org.gvsig.tools.resourcesstorage.ResourcesStorage;
9 8
import org.gvsig.tools.script.ScriptManager;
10 9

  
11 10

  
......
66 65
    public Bookmarks<Expression> getBookmarks();
67 66
    
68 67
    public History<Expression> getHistory();
68
    
69
    public ResourcesStorage getScriptsResourcesStorage();
70

  
71
    public void setScriptsResourcesStorage(ResourcesStorage scriptsResourcesStorage);
72
    
69 73
}
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/SymbolTable.java
3 3
import java.util.Collection;
4 4

  
5 5
import org.gvsig.tools.lang.Cloneable;
6
import org.gvsig.tools.script.Script;
7 6

  
8 7
public interface SymbolTable extends Iterable<Function>, Cloneable {
9 8

  
......
31 30
    
32 31
    public Collection<Function> localfunctions();
33 32

  
34
    public Collection<Script> scripts();
33
//    public Collection<Script> scripts();
35 34

  
36 35
    @Override
37 36
    public SymbolTable clone() throws CloneNotSupportedException;
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.swing/org.gvsig.expressionevaluator.swing.api/src/main/java/org/gvsig/expressionevaluator/swing/JExpressionBuilder.java
9 9
 */
10 10
public interface JExpressionBuilder extends Component, ExpressionBuilderConfig {
11 11

  
12
    public static final String CONFIGURABLE_PANEL_ID = "JExpressionBuilder";
13
    
12 14
    public Expression getExpressionWithOutHistory();
13 15

  
14 16
    public Expression getExpression();
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff