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 @ 43983
History | View | Annotate | Download (8.05 KB)
1 |
package org.gvsig.expressionevaluator.impl; |
---|---|
2 |
|
3 |
import java.net.URI; |
4 |
import java.net.URISyntaxException; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
import java.util.logging.Level; |
9 |
import java.util.logging.Logger; |
10 |
import org.apache.commons.lang3.StringUtils; |
11 |
import org.apache.http.client.utils.URIBuilder; |
12 |
import org.gvsig.expressionevaluator.Code; |
13 |
import org.gvsig.expressionevaluator.Expression; |
14 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
15 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
16 |
import org.gvsig.expressionevaluator.Interpreter; |
17 |
import org.gvsig.expressionevaluator.SymbolTable; |
18 |
import org.gvsig.tools.ToolsLocator; |
19 |
import org.gvsig.tools.dynobject.DynStruct; |
20 |
import org.gvsig.tools.persistence.PersistenceManager; |
21 |
import org.gvsig.tools.persistence.PersistentState; |
22 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
23 |
import org.gvsig.tools.script.Script; |
24 |
import org.gvsig.tools.script.ScriptManager; |
25 |
import org.gvsig.tools.util.UnmodifiableBasicList; |
26 |
import org.gvsig.tools.util.UnmodifiableBasicListAdapter; |
27 |
|
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 |
public Object execute(SymbolTable symbolTable) { |
112 |
if (this.code == null) { |
113 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
114 |
this.code = manager.compile(this.phrase); |
115 |
} |
116 |
if (this.interpreter == null) { |
117 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
118 |
this.interpreter = manager.createInterpreter();
|
119 |
} |
120 |
this.interpreter.setSymbolTable(symbolTable);
|
121 |
Object x = this.interpreter.run(code); |
122 |
return x;
|
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void saveToState(PersistentState state) throws PersistenceException { |
127 |
state.set("phrase", this.phrase); |
128 |
if (this.userScript == null) { |
129 |
state.setNull("userScript.code");
|
130 |
state.setNull("userScript.language");
|
131 |
} else {
|
132 |
state.set("userScript.code", this.userScript.getCode()); |
133 |
state.set("userScript.language", this.userScript.getTypeName()); |
134 |
} |
135 |
if (this.scripts != null && !this.scripts.isEmpty()) { |
136 |
List<URI> l = new ArrayList<>(); |
137 |
for (Script script : this.scripts) { |
138 |
URI location = script.getURI();
|
139 |
if (location != null) { |
140 |
l.add(location); |
141 |
} |
142 |
} |
143 |
if (l.isEmpty()) {
|
144 |
state.setNull("scripts");
|
145 |
} else {
|
146 |
state.set("scripts", l);
|
147 |
} |
148 |
} else {
|
149 |
state.setNull("scripts");
|
150 |
} |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public void loadFromState(PersistentState state) throws PersistenceException { |
155 |
ScriptManager scriptManager = ToolsLocator.getScriptManager(); |
156 |
|
157 |
this.phrase = state.getString("phrase"); |
158 |
String userScript_code = state.getString("userScript.code"); |
159 |
String userScript_language = state.getString("userScript.language"); |
160 |
if (StringUtils.isEmpty(userScript_code)) {
|
161 |
this.userScript = null; |
162 |
} else {
|
163 |
if (StringUtils.isEmpty(userScript_language)) {
|
164 |
userScript_language = "python";
|
165 |
} |
166 |
this.userScript = scriptManager.createScript("user", userScript_code, userScript_language); |
167 |
} |
168 |
Iterator scriptsLocations = state.getIterator("scripts"); |
169 |
while (scriptsLocations.hasNext()) {
|
170 |
URI location = (URI) scriptsLocations.next(); |
171 |
Script script = scriptManager.loadScript(location); |
172 |
if (script != null) { |
173 |
if (this.scripts == null) { |
174 |
this.scripts = new ArrayList<>(); |
175 |
} |
176 |
this.scripts.add(script);
|
177 |
} |
178 |
} |
179 |
} |
180 |
|
181 |
public static void registerPersistence() { |
182 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
183 |
if (manager.getDefinition("Expression") == null) { |
184 |
DynStruct definition = manager.addDefinition(DefaultExpression.class, |
185 |
"Expression", "Expression persistence definition", null, null); |
186 |
definition.addDynFieldString("phrase").setMandatory(false); |
187 |
definition.addDynFieldString("userScript.code").setMandatory(false); |
188 |
definition.addDynFieldString("userScript.language").setMandatory(false); |
189 |
definition.addDynFieldList("scripts").setMandatory(false); |
190 |
} |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public URI toURI() { |
195 |
URIBuilder builder = new URIBuilder();
|
196 |
|
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()); |
205 |
} |
206 |
if (this.scripts != null && !this.scripts.isEmpty()) { |
207 |
List<URI> l = new ArrayList<>(); |
208 |
for (Script script : this.scripts) { |
209 |
URI location = script.getURI();
|
210 |
if (location != null) { |
211 |
l.add(location); |
212 |
} |
213 |
} |
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 |
} |
223 |
} |
224 |
} else {
|
225 |
builder.addParameter("scriptsCount", "0"); |
226 |
builder.addParameter("scripts", null); |
227 |
} |
228 |
|
229 |
try {
|
230 |
return builder.build();
|
231 |
} catch (URISyntaxException ex) { |
232 |
throw new RuntimeException("Can't serialize expression", ex); |
233 |
} |
234 |
} |
235 |
|
236 |
|
237 |
} |