svn-gvsig-desktop / 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 @ 43989
History | View | Annotate | Download (9.05 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.net.URI; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.gvsig.expressionevaluator.Code; |
9 |
import org.gvsig.expressionevaluator.Expression; |
10 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
11 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
12 |
import org.gvsig.expressionevaluator.Interpreter; |
13 |
import org.gvsig.expressionevaluator.SymbolTable; |
14 |
import org.gvsig.tools.ToolsLocator; |
15 |
import org.gvsig.tools.dynobject.DynStruct; |
16 |
import org.gvsig.tools.persistence.PersistenceManager; |
17 |
import org.gvsig.tools.persistence.PersistentState; |
18 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
19 |
import org.gvsig.tools.script.Script; |
20 |
import org.gvsig.tools.script.ScriptManager; |
21 |
import org.gvsig.tools.util.UnmodifiableBasicList; |
22 |
import org.gvsig.tools.util.UnmodifiableBasicListAdapter; |
23 |
import org.json.JSONArray; |
24 |
import org.json.JSONObject; |
25 |
|
26 |
/**
|
27 |
*
|
28 |
* @author jjdelcerro
|
29 |
*/
|
30 |
public class DefaultExpression implements Expression { |
31 |
|
32 |
private String phrase = null; |
33 |
private Script userScript = null; |
34 |
private List<Script> scripts = null; |
35 |
private UnmodifiableBasicList<Script> unmodifiableScripts = null; |
36 |
|
37 |
private Code code = null; |
38 |
private Interpreter interpreter;
|
39 |
|
40 |
public DefaultExpression() {
|
41 |
|
42 |
} |
43 |
|
44 |
@Override
|
45 |
public String getPhrase() { |
46 |
return this.phrase; |
47 |
} |
48 |
|
49 |
@Override
|
50 |
public Script getUserScript() {
|
51 |
return this.userScript; |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public UnmodifiableBasicList<Script> getScripts() {
|
56 |
if (this.unmodifiableScripts == null) { |
57 |
if (this.scripts == null) { |
58 |
return null; |
59 |
} |
60 |
this.unmodifiableScripts = new UnmodifiableBasicListAdapter<>(this.scripts); |
61 |
} |
62 |
return this.unmodifiableScripts; |
63 |
} |
64 |
|
65 |
@Override
|
66 |
public void setPhrase(String phrase) { |
67 |
this.phrase = phrase;
|
68 |
this.code = null; |
69 |
} |
70 |
|
71 |
@Override
|
72 |
public void setUserScript(String code, String languaje) { |
73 |
if (this.userScript == null) { |
74 |
ScriptManager scriptMananger = ToolsLocator.getScriptManager(); |
75 |
this.userScript = scriptMananger.createScript("user", code, languaje); |
76 |
} else if (this.userScript.getTypeName().equalsIgnoreCase(languaje)) { |
77 |
this.userScript.setCode(code);
|
78 |
} else {
|
79 |
ScriptManager scriptMananger = ToolsLocator.getScriptManager(); |
80 |
this.userScript = scriptMananger.createScript("user", code, languaje); |
81 |
} |
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void setUserScript(Script script) { |
86 |
this.userScript = script;
|
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void setUserScript(String code) { |
91 |
this.setUserScript(code, "python"); |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public void removeAllScripts() { |
96 |
this.scripts = null; |
97 |
this.unmodifiableScripts = null; |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public void addScript(Script script) { |
102 |
if (this.scripts == null) { |
103 |
this.scripts = new ArrayList<>(); |
104 |
} |
105 |
this.scripts.add(script);
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public void clear() { |
110 |
this.phrase = null; |
111 |
this.userScript = null; |
112 |
this.unmodifiableScripts = null; |
113 |
this.scripts = null; |
114 |
this.code = null; |
115 |
this.interpreter = null; |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public Code getCode() {
|
120 |
if (this.code == null) { |
121 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
122 |
this.code = manager.compile(this.phrase); |
123 |
} |
124 |
return code;
|
125 |
} |
126 |
|
127 |
|
128 |
@Override
|
129 |
public Object execute(SymbolTable symbolTable) { |
130 |
if (this.interpreter == null) { |
131 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
132 |
this.interpreter = manager.createInterpreter();
|
133 |
} |
134 |
this.interpreter.setSymbolTable(symbolTable);
|
135 |
Object x = this.interpreter.run(this.getCode()); |
136 |
return x;
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public void saveToState(PersistentState state) throws PersistenceException { |
141 |
state.set("phrase", this.phrase); |
142 |
if (this.userScript == null) { |
143 |
state.setNull("userScript.code");
|
144 |
state.setNull("userScript.language");
|
145 |
} else {
|
146 |
state.set("userScript.code", this.userScript.getCode()); |
147 |
state.set("userScript.language", this.userScript.getTypeName()); |
148 |
} |
149 |
if (this.scripts != null && !this.scripts.isEmpty()) { |
150 |
List<URI> l = new ArrayList<>(); |
151 |
for (Script script : this.scripts) { |
152 |
URI location = script.getURI();
|
153 |
if (location != null) { |
154 |
l.add(location); |
155 |
} |
156 |
} |
157 |
if (l.isEmpty()) {
|
158 |
state.setNull("scripts");
|
159 |
} else {
|
160 |
state.set("scripts", l);
|
161 |
} |
162 |
} else {
|
163 |
state.setNull("scripts");
|
164 |
} |
165 |
} |
166 |
|
167 |
@Override
|
168 |
public void loadFromState(PersistentState state) throws PersistenceException { |
169 |
ScriptManager scriptManager = ToolsLocator.getScriptManager(); |
170 |
|
171 |
this.phrase = state.getString("phrase"); |
172 |
String userScript_code = state.getString("userScript.code"); |
173 |
String userScript_language = state.getString("userScript.language"); |
174 |
if (StringUtils.isEmpty(userScript_code)) {
|
175 |
this.userScript = null; |
176 |
} else {
|
177 |
if (StringUtils.isEmpty(userScript_language)) {
|
178 |
userScript_language = "python";
|
179 |
} |
180 |
this.userScript = scriptManager.createScript("user", userScript_code, userScript_language); |
181 |
} |
182 |
Iterator scriptsLocations = state.getIterator("scripts"); |
183 |
while (scriptsLocations.hasNext()) {
|
184 |
URI location = (URI) scriptsLocations.next(); |
185 |
Script script = scriptManager.loadScript(location); |
186 |
if (script != null) { |
187 |
if (this.scripts == null) { |
188 |
this.scripts = new ArrayList<>(); |
189 |
} |
190 |
this.scripts.add(script);
|
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
public static void registerPersistence() { |
196 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
197 |
if (manager.getDefinition("Expression") == null) { |
198 |
DynStruct definition = manager.addDefinition(DefaultExpression.class, |
199 |
"Expression", "Expression persistence definition", null, null); |
200 |
definition.addDynFieldString("phrase").setMandatory(false); |
201 |
definition.addDynFieldString("userScript.code").setMandatory(false); |
202 |
definition.addDynFieldString("userScript.language").setMandatory(false); |
203 |
definition.addDynFieldList("scripts").setMandatory(false); |
204 |
} |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public String toJSON() { |
209 |
JSONObject expressionJson = new JSONObject();
|
210 |
expressionJson.put("phrase", this.phrase); |
211 |
|
212 |
if( this.userScript!=null ) { |
213 |
JSONObject userScriptJson = new JSONObject();
|
214 |
userScriptJson.put("code", this.userScript.getCode()); |
215 |
userScriptJson.put("language", this.userScript.getTypeName()); |
216 |
expressionJson.put("userScript", userScriptJson);
|
217 |
} |
218 |
|
219 |
if( this.scripts!=null && !this.scripts.isEmpty() ) { |
220 |
JSONArray scriptsJson = new JSONArray();
|
221 |
for (Script script : this.scripts) { |
222 |
scriptsJson.put(script.getURI()); |
223 |
} |
224 |
expressionJson.put("scripts", scriptsJson);
|
225 |
} |
226 |
return expressionJson.toString();
|
227 |
} |
228 |
|
229 |
@Override
|
230 |
public void fromJSON(String json) { |
231 |
this.clear();
|
232 |
ScriptManager scriptMananger = ToolsLocator.getScriptManager(); |
233 |
|
234 |
JSONObject expressionJson = new JSONObject(json);
|
235 |
if( expressionJson.has("phrase") ) { |
236 |
this.phrase = expressionJson.getString("phrase"); |
237 |
} |
238 |
if( expressionJson.has("userScript") ) { |
239 |
String theCode = ""; |
240 |
String theLanguage = "python"; |
241 |
JSONObject userScriptJson = expressionJson.getJSONObject("userScript");
|
242 |
if( userScriptJson.has("code") ) { |
243 |
theCode = userScriptJson.getString("code");
|
244 |
} |
245 |
if( userScriptJson.has("language") ) { |
246 |
theCode = userScriptJson.getString("language");
|
247 |
} |
248 |
this.userScript = scriptMananger.createScript("user", theCode, theLanguage); |
249 |
} |
250 |
if( expressionJson.has("scripts") ) { |
251 |
this.scripts = new ArrayList<>(); |
252 |
JSONArray scriptsJson = expressionJson.getJSONArray("scripts");
|
253 |
for (Object object : scriptsJson) { |
254 |
URI location = (URI) object; |
255 |
Script script = scriptMananger.loadScript(location); |
256 |
this.scripts.add(script);
|
257 |
} |
258 |
} |
259 |
} |
260 |
|
261 |
@Override
|
262 |
public String toString() { |
263 |
return this.toJSON(); |
264 |
} |
265 |
|
266 |
@Override
|
267 |
public Expression clone() throws CloneNotSupportedException { |
268 |
Expression other = (Expression) super.clone(); |
269 |
other.fromJSON(this.toJSON());
|
270 |
return other;
|
271 |
} |
272 |
|
273 |
|
274 |
} |