Revision 44533 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

View differences:

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

Also available in: Unified diff