Revision 43984 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 3
import java.net.URI;
4
import java.net.URISyntaxException;
5 4
import java.util.ArrayList;
6 5
import java.util.Iterator;
7 6
import java.util.List;
8 7
import java.util.logging.Level;
9 8
import java.util.logging.Logger;
10 9
import org.apache.commons.lang3.StringUtils;
11
import org.apache.http.client.utils.URIBuilder;
12 10
import org.gvsig.expressionevaluator.Code;
13 11
import org.gvsig.expressionevaluator.Expression;
14 12
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
......
24 22
import org.gvsig.tools.script.ScriptManager;
25 23
import org.gvsig.tools.util.UnmodifiableBasicList;
26 24
import org.gvsig.tools.util.UnmodifiableBasicListAdapter;
25
import org.json.JSONArray;
26
import org.json.JSONObject;
27 27

  
28 28
/**
29 29
 *
......
108 108
    }
109 109

  
110 110
    @Override
111
    public void clear() {
112
        this.phrase = null;
113
        this.userScript = null;
114
        this.unmodifiableScripts = null;
115
        this.scripts = null;
116
        this.code = null;
117
        this.interpreter = null;
118
    }
119

  
120
    @Override
111 121
    public Object execute(SymbolTable symbolTable) {
112 122
        if (this.code == null) {
113 123
            ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
......
191 201
    }
192 202

  
193 203
    @Override
194
    public URI toURI() {
195
        URIBuilder builder = new URIBuilder();
204
    public String toJSON() {
205
        JSONObject expressionJson = new JSONObject();
206
        expressionJson.put("phrase", this.phrase);
196 207
        
197
        builder.setScheme("expression");
198
        builder.addParameter("phrase", this.phrase);
199
        if( this.userScript==null ) {
200
            builder.addParameter("userScriptCode", null);
201
            builder.addParameter("userScriptLanguage", null);
202
        } else {
203
            builder.addParameter("userScriptCode", this.userScript.getCode());
204
            builder.addParameter("userScriptLanguage", this.userScript.getTypeName());
208
        if( this.userScript!=null ) {
209
            JSONObject userScriptJson = new JSONObject();
210
            userScriptJson.put("code", this.userScript.getCode());
211
            userScriptJson.put("language", this.userScript.getTypeName());
212
            expressionJson.put("userScript", userScriptJson);
205 213
        }
206
        if (this.scripts != null && !this.scripts.isEmpty()) {
207
            List<URI> l = new ArrayList<>();
214
        
215
        if( this.scripts!=null && !this.scripts.isEmpty() ) {
216
            JSONArray scriptsJson = new JSONArray();
208 217
            for (Script script : this.scripts) {
209
                URI location = script.getURI();
210
                if (location != null) {
211
                    l.add(location);
212
                }
218
                scriptsJson.put(script.getURI());
213 219
            }
214
            if (l.isEmpty()) {
215
                builder.addParameter("scriptsCount", "0");
216
                builder.addParameter("scripts", null);
217
            } else {
218
                builder.addParameter("scriptsCount", String.valueOf(l.size()));
219
                int n = 1;
220
                for (URI uri : l) {
221
                    builder.addParameter("scripts"+n, uri.toString());
222
                }
220
            expressionJson.put("scripts", scriptsJson);
221
        }
222
        return expressionJson.toString();
223
    }
224

  
225
    @Override
226
    public void fromJSON(String json) {
227
        this.clear();
228
        ScriptManager scriptMananger = ToolsLocator.getScriptManager();
229

  
230
        JSONObject expressionJson = new JSONObject(json);
231
        if( expressionJson.has("phrase") ) {
232
            this.phrase = expressionJson.getString("phrase");
233
        }
234
        if( expressionJson.has("userScript") ) {
235
            String theCode = "";
236
            String theLanguage = "python";
237
            JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
238
            if( userScriptJson.has("code") ) {
239
                theCode = userScriptJson.getString("code");
223 240
            }
224
        } else {
225
            builder.addParameter("scriptsCount", "0");
226
            builder.addParameter("scripts", null);
241
            if( userScriptJson.has("language") ) {
242
                theCode = userScriptJson.getString("language");
243
            }
244
            this.userScript = scriptMananger.createScript("user", theCode, theLanguage);
227 245
        }
228
        
229
        try {
230
            return builder.build();
231
        } catch (URISyntaxException ex) {
232
            throw new RuntimeException("Can't serialize expression", ex);
246
        if( expressionJson.has("scripts") ) {
247
            this.scripts = new ArrayList<>();
248
            JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
249
            for (Object object : scriptsJson) {
250
                URI location = (URI) object;
251
                Script script = scriptMananger.loadScript(location);
252
                this.scripts.add(script);
253
            }
233 254
        }
234 255
    }
235 256

  
257
    @Override
258
    public String toString() {
259
        return this.toJSON();
260
    }
261

  
262
    @Override
263
    public Expression clone() throws CloneNotSupportedException {
264
        Expression other = (Expression) super.clone();
265
        other.fromJSON(this.toJSON());
266
        return other;
267
    }
268

  
236 269
    
237 270
}

Also available in: Unified diff