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