Revision 45425 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
2 2

  
3 3
import java.lang.ref.WeakReference;
4 4
import java.util.Objects;
5
import javax.json.JsonObject;
5 6
import org.apache.commons.lang3.StringUtils;
6 7
import org.gvsig.expressionevaluator.Code;
7 8
import org.gvsig.expressionevaluator.Compiler;
......
13 14
import org.gvsig.expressionevaluator.Interpreter;
14 15
import org.gvsig.expressionevaluator.Optimizer;
15 16
import org.gvsig.expressionevaluator.SymbolTable;
17
import org.gvsig.json.Json;
18
import org.gvsig.json.JsonObjectBuilder;
16 19
import org.gvsig.tools.ToolsLocator;
17 20
import org.gvsig.tools.dynobject.DynStruct;
18 21
import org.gvsig.tools.evaluator.Evaluator;
......
21 24
import org.gvsig.tools.persistence.exception.PersistenceException;
22 25
import org.gvsig.tools.script.ScriptManager;
23 26
import org.gvsig.tools.util.LabeledValue;
24
import org.json.JSONObject;
25 27

  
26 28
/**
27 29
 *
......
203 205
    @Override
204 206
    public void saveToState(PersistentState state) throws PersistenceException {
205 207
        state.set("phrase", this.phrase);
206
//        if (this.userScript == null) {
207
//            state.setNull("userScript_code");
208
//            state.setNull("userScript_language");
209
//        } else {
210
//            state.set("userScript_code", this.userScript.getCode());
211
//            state.set("userScript_language", this.userScript.getTypeName());
212
//        }
213
//        if (this.scripts != null && !this.scripts.isEmpty()) {
214
//            List<URL> l = new ArrayList<>();
215
//            for (Script script : this.scripts) {
216
//                URL location = script.getURL();
217
//                if (location != null) {
218
//                    l.add(location);
219
//                }
220
//            }
221
//            if (l.isEmpty()) {
222
//                state.setNull("scripts");
223
//            } else {
224
//                state.set("scripts", l);
225
//            }
226
//        } else {
227
//            state.setNull("scripts");
228
//        }
229 208
    }
230 209

  
231 210
    @Override
......
235 214
        this.clear();
236 215
        
237 216
        this.phrase = state.getString("phrase");
238
//        String userScript_code = state.getString("userScript_code");
239
//        String userScript_language = state.getString("userScript_language");
240
//        if (StringUtils.isEmpty(userScript_code)) {
241
//            this.userScript = null;
242
//        } else {
243
//            if (StringUtils.isEmpty(userScript_language)) {
244
//                userScript_language = "python";
245
//            }
246
//            this.userScript = scriptManager.createScript("user", userScript_code, userScript_language);
247
//        }
248
//        Iterator scriptsLocations = state.getIterator("scripts");
249
//        if (scriptsLocations != null) {
250
//            while (scriptsLocations.hasNext()) {
251
//                URI location = (URI) scriptsLocations.next();
252
//                Script script = scriptManager.loadScript(location);
253
//                if (script != null) {
254
//                    if (this.scripts == null) {
255
//                        this.scripts = new ArrayList<>();
256
//                    }
257
//                    this.scripts.add(script);
258
//                }
259
//            }
260
//        }
261 217
    }
262 218

  
263 219
    public static void registerPersistence() {
......
266 222
            DynStruct definition = manager.addDefinition(DefaultExpression.class,
267 223
                    "Expression", "Expression persistence definition", null, null);
268 224
            definition.addDynFieldString("phrase").setMandatory(false);
269
//            definition.addDynFieldString("userScript_code").setMandatory(false);
270
//            definition.addDynFieldString("userScript_language").setMandatory(false);
271
//            definition.addDynFieldList("scripts")
272
//                    .setClassOfItems(URL.class)
273
//                    .setMandatory(false);
274 225
        }
275 226
    }
276 227

  
277 228
    @Override
278
    public String toJSON() {
279
        JSONObject expressionJson = new JSONObject();
280
        expressionJson.put("phrase", this.phrase);
281

  
282
//        if (this.userScript != null) {
283
//            JSONObject userScriptJson = new JSONObject();
284
//            userScriptJson.put("code", this.userScript.getCode());
285
//            userScriptJson.put("language", this.userScript.getTypeName());
286
//            expressionJson.put("userScript", userScriptJson);
287
//        }
288
//
289
//        if (this.scripts != null && !this.scripts.isEmpty()) {
290
//            JSONArray scriptsJson = new JSONArray();
291
//            for (Script script : this.scripts) {
292
//                scriptsJson.put(script.getURL());
293
//            }
294
//            expressionJson.put("scripts", scriptsJson);
295
//        }
296
        return expressionJson.toString();
229
    public JsonObject toJson() {
230
        return this.toJsonBuilder().build();
297 231
    }
298 232

  
299 233
    @Override
300
    public void fromJSON(String json) {
234
    public JsonObjectBuilder toJsonBuilder() {
235
        JsonObjectBuilder builder = Json.createObjectBuilder();
236
        builder.add_class(this);
237
        builder.add("phrase", this.phrase);
238
        return builder;
239
    }
240
    
241
    @Override
242
    public void fromJson(JsonObject values) {
301 243
        this.clear();
302
        ScriptManager scriptMananger = ToolsLocator.getScriptManager();
303

  
304
        JSONObject expressionJson = new JSONObject(json);
305
        if (expressionJson.has("phrase")) {
306
            this.phrase = expressionJson.getString("phrase");
244
        if( values.containsKey("phrase") ) {
245
            this.phrase = values.getString("phrase");
307 246
        }
308
//        if (expressionJson.has("userScript")) {
309
//            String theCode = "";
310
//            String theLanguage = "python";
311
//            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
312
//            if (userScriptJson.has("code")) {
313
//                theCode = userScriptJson.getString("code");
314
//            }
315
//            if (userScriptJson.has("language")) {
316
//                theCode = userScriptJson.getString("language");
317
//            }
318
//            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
319
//        }
320
//        if (expressionJson.has("scripts")) {
321
//            this.scripts = new ArrayList<>();
322
//            JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
323
//            for (Object object : scriptsJson) {
324
//                URI location = (URI) object;
325
//                Script script = scriptMananger.loadScript(location);
326
//                this.scripts.add(script);
327
//            }
328
//        }
329 247
    }
248
    
249
    @Deprecated
250
    @Override
251
    public String toJSON() {
252
        return toJson().toString();
253
    }
330 254

  
255
    @Deprecated
331 256
    @Override
257
    public void fromJSON(String json) {
258
        this.fromJson(Json.createObject(json));
259
    }
260

  
261
    @Override
332 262
    public String toString() {
333
        return this.toJSON();
263
        return this.toJson().toString();
334 264
    }
335 265

  
336 266
    @Override
......
355 285
        ExpressionEvaluator evaluator = this.manager.createExpressionEvaluator(this);
356 286
        return evaluator;
357 287
    }
358
    
359 288
}

Also available in: Unified diff