svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.api / src / main / java / org / gvsig / expressionevaluator / ExpressionUtils.java @ 45163
History | View | Annotate | Download (11.9 KB)
1 |
package org.gvsig.expressionevaluator; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import org.apache.commons.lang3.ArrayUtils; |
5 |
import org.apache.commons.lang3.StringUtils; |
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
7 |
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_ENDTAG; |
8 |
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_STARTTAG; |
9 |
|
10 |
/**
|
11 |
*
|
12 |
* @author jjdelcerro
|
13 |
*/
|
14 |
public class ExpressionUtils { |
15 |
|
16 |
public static boolean isEmpty(Expression expression) { |
17 |
return expression == null || expression.isEmpty(); |
18 |
} |
19 |
|
20 |
public static boolean isPhraseEmpty(Expression expression) { |
21 |
return expression == null || expression.isPhraseEmpty(); |
22 |
} |
23 |
|
24 |
public static Expression defaultIfEmpty(Expression expression, Expression defaultValue) { |
25 |
if( expression==null || expression.isEmpty() ) { |
26 |
return defaultValue;
|
27 |
} |
28 |
return expression;
|
29 |
} |
30 |
|
31 |
public static Expression defaultNullIfEmpty(Expression expression) { |
32 |
if( expression==null || expression.isEmpty() ) { |
33 |
return null; |
34 |
} |
35 |
return expression;
|
36 |
} |
37 |
|
38 |
public static Expression defaultIfPhraseEmpty(Expression expression, Expression defaultValue) { |
39 |
if( expression==null || expression.isPhraseEmpty() ) { |
40 |
return defaultValue;
|
41 |
} |
42 |
return expression;
|
43 |
} |
44 |
|
45 |
public static Expression defaultNullIfPhraseEmpty(Expression expression) { |
46 |
if( expression==null || expression.isPhraseEmpty() ) { |
47 |
return null; |
48 |
} |
49 |
return expression;
|
50 |
} |
51 |
|
52 |
public static Expression createExpression() { |
53 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
54 |
return expression;
|
55 |
} |
56 |
|
57 |
public static Expression createExpression(String phrase) { |
58 |
if( StringUtils.isBlank(phrase) ) {
|
59 |
return null; |
60 |
} |
61 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
62 |
expression.setPhrase(phrase); |
63 |
return expression;
|
64 |
} |
65 |
|
66 |
// public static Expression createExpression(String phrase, String code, Script... scripts) {
|
67 |
// Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
68 |
// expression.setPhrase(phrase);
|
69 |
// expression.setUserScript(code);
|
70 |
// for (Script script : scripts) {
|
71 |
// expression.addScript(script);
|
72 |
// }
|
73 |
// return expression;
|
74 |
// }
|
75 |
//
|
76 |
// public static Expression createExpression(String phrase, String code, String languaje, Script... scripts) {
|
77 |
// Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
78 |
// expression.setPhrase(phrase);
|
79 |
// expression.setUserScript(code, languaje);
|
80 |
// for (Script script : scripts) {
|
81 |
// expression.addScript(script);
|
82 |
// }
|
83 |
// return expression;
|
84 |
// }
|
85 |
|
86 |
public static ExpressionBuilder createExpressionBuilder() { |
87 |
ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder(); |
88 |
return builder;
|
89 |
} |
90 |
|
91 |
public static Code compile(String expression) { |
92 |
if( StringUtils.isBlank(expression) ) {
|
93 |
return null; |
94 |
} |
95 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
96 |
Code code = manager.compile(expression); |
97 |
return code;
|
98 |
} |
99 |
|
100 |
public static Object evaluate(String expression) { |
101 |
return evaluate(null, expression); |
102 |
} |
103 |
|
104 |
public static Object evaluate(SymbolTable symbolTable, String expression) { |
105 |
if( StringUtils.isBlank(expression) ) {
|
106 |
return null; |
107 |
} |
108 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
109 |
Object x = manager.evaluate(symbolTable, expression);
|
110 |
return x;
|
111 |
} |
112 |
|
113 |
public static Object evaluate(SymbolTable symbolTable, Code code) { |
114 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
115 |
Object x = manager.evaluate(symbolTable, code);
|
116 |
return x;
|
117 |
} |
118 |
|
119 |
public static Code optimize(SymbolTable symbolTable, Code code) { |
120 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
121 |
code = manager.optimize(symbolTable, code); |
122 |
return code;
|
123 |
} |
124 |
|
125 |
public static String toString(Value value, Formatter formatter) { |
126 |
if( value == null ) { |
127 |
return null; |
128 |
} |
129 |
if( formatter==null ) { |
130 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
131 |
formatter = manager.getExpressionBuilderFormatter(); |
132 |
} |
133 |
return value.toString(formatter);
|
134 |
} |
135 |
|
136 |
public static String toString(Value value) { |
137 |
return toString(value, null); |
138 |
} |
139 |
|
140 |
public static String toString(Code code, Formatter formatter) { |
141 |
if( code == null ) { |
142 |
return null; |
143 |
} |
144 |
if( formatter==null ) { |
145 |
formatter = Code.EMPTY_FORMATTER; |
146 |
} |
147 |
return code.toString(formatter);
|
148 |
} |
149 |
|
150 |
public static String toString(Code code) { |
151 |
return toString(code, null); |
152 |
} |
153 |
|
154 |
public static Expression createExpressionFromJSON(String json) { |
155 |
Expression expression = ExpressionUtils.createExpression();
|
156 |
expression.fromJSON(json); |
157 |
return expression;
|
158 |
} |
159 |
|
160 |
public static MutableSymbolTable createSymbolTable() { |
161 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
162 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
163 |
return symbolTable;
|
164 |
} |
165 |
|
166 |
public static String surroundByDynamicTextTag(String source) { |
167 |
return surroundByDynamicTextTag(source, true); |
168 |
} |
169 |
|
170 |
public static String surroundByDynamicTextTag(String source, boolean insert) { |
171 |
if( source==null ) { |
172 |
return null; |
173 |
} |
174 |
if( insert ) {
|
175 |
return DYNAMICTEXT_STARTTAG+ "=" + source + DYNAMICTEXT_ENDTAG; |
176 |
} |
177 |
return DYNAMICTEXT_STARTTAG + source + DYNAMICTEXT_ENDTAG;
|
178 |
} |
179 |
|
180 |
public static boolean isDynamicText(String source) { |
181 |
if( !source.contains(DYNAMICTEXT_STARTTAG) ) {
|
182 |
return false; |
183 |
} |
184 |
String[] sources = StringUtils.substringsBetween(source, DYNAMICTEXT_STARTTAG, DYNAMICTEXT_ENDTAG); |
185 |
if( ArrayUtils.isEmpty(sources) ) {
|
186 |
return false; |
187 |
} |
188 |
return true; |
189 |
} |
190 |
|
191 |
public static String evaluateDynamicText(String source) { |
192 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
193 |
return manager.evaluateDynamicText(source);
|
194 |
} |
195 |
|
196 |
public static String evaluateDynamicText(SymbolTable symbolTable, String source) { |
197 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
198 |
return manager.evaluateDynamicText(symbolTable, source);
|
199 |
} |
200 |
|
201 |
public static File evaluateFilename(File source) { |
202 |
return evaluateFilename(null, source); |
203 |
} |
204 |
|
205 |
public static boolean isDynamicFilename(File source) { |
206 |
if( source == null ) { |
207 |
return false; |
208 |
} |
209 |
return isDynamicText(source.getPath());
|
210 |
} |
211 |
|
212 |
public static File createDynamicFile(Value builder) { |
213 |
File f = new File(surroundByDynamicTextTag(builder.toString())); |
214 |
return f;
|
215 |
} |
216 |
|
217 |
public static File createDynamicFile(String expression) { |
218 |
File f = new File(surroundByDynamicTextTag(expression)); |
219 |
return f;
|
220 |
} |
221 |
|
222 |
@SuppressWarnings("StringEquality") |
223 |
public static File evaluateFilename(SymbolTable symbolTable, File source) { |
224 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
225 |
String src = source.getPath();
|
226 |
String r = manager.evaluateDynamicText(symbolTable, src);
|
227 |
if( r == src ) { // !!! I compare that it is the same pointer, it is what I want. |
228 |
return source;
|
229 |
} |
230 |
File f = new File(r); |
231 |
return f;
|
232 |
} |
233 |
|
234 |
public static int parseInt(String s) throws NumberFormatException { |
235 |
if( StringUtils.isBlank(s) ) {
|
236 |
throw new NumberFormatException("Can't get integer from a blank string."); |
237 |
} |
238 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
239 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
240 |
Object x;
|
241 |
try {
|
242 |
x = manager.evaluate(symbolTable, s); |
243 |
if( x instanceof Number ) { |
244 |
return ((Number) x).intValue(); |
245 |
} |
246 |
} catch(Exception ex) { |
247 |
NumberFormatException ex1 = new NumberFormatException("Can't get integer from '"+s+"'."); |
248 |
ex1.initCause(ex); |
249 |
throw ex;
|
250 |
} |
251 |
if( x == null ) { |
252 |
throw new NumberFormatException("Can't get integer from '"+s+"' value is null."); |
253 |
} |
254 |
throw new NumberFormatException("Can't get integer from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
255 |
} |
256 |
|
257 |
public static long parseLong(String s) throws NumberFormatException { |
258 |
if( StringUtils.isBlank(s) ) {
|
259 |
throw new NumberFormatException("Can't get long from a blank string."); |
260 |
} |
261 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
262 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
263 |
Object x;
|
264 |
try {
|
265 |
x = manager.evaluate(symbolTable, s); |
266 |
if( x instanceof Number ) { |
267 |
return ((Number) x).longValue(); |
268 |
} |
269 |
} catch(Exception ex) { |
270 |
NumberFormatException ex1 = new NumberFormatException("Can't get long from '"+s+"'."); |
271 |
ex1.initCause(ex); |
272 |
throw ex;
|
273 |
} |
274 |
if( x == null ) { |
275 |
throw new NumberFormatException("Can't get long from '"+s+"' value is null."); |
276 |
} |
277 |
throw new NumberFormatException("Can't get long from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
278 |
} |
279 |
|
280 |
public static double parseDouble(String s) throws NumberFormatException { |
281 |
if( StringUtils.isBlank(s) ) {
|
282 |
throw new NumberFormatException("Can't get double from a blank string."); |
283 |
} |
284 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
285 |
SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
286 |
Object x;
|
287 |
try {
|
288 |
x = manager.evaluate(symbolTable, s); |
289 |
if( x instanceof Number ) { |
290 |
return ((Number) x).doubleValue(); |
291 |
} |
292 |
} catch(Exception ex) { |
293 |
NumberFormatException ex1 = new NumberFormatException("Can't get double from '"+s+"'."); |
294 |
ex1.initCause(ex); |
295 |
throw ex;
|
296 |
} |
297 |
if( x == null ) { |
298 |
throw new NumberFormatException("Can't get double from '"+s+"' value is null."); |
299 |
} |
300 |
throw new NumberFormatException("Can't get double from '"+s+"' value is a "+x.getClass().getSimpleName()+"."); |
301 |
} |
302 |
|
303 |
public static Compiler createCompiler() { |
304 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
305 |
Compiler compiler = manager.createCompiler();
|
306 |
return compiler;
|
307 |
} |
308 |
|
309 |
public static Interpreter createInterpreter() { |
310 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
311 |
Interpreter interpreter = manager.createInterpreter(); |
312 |
return interpreter;
|
313 |
} |
314 |
|
315 |
public static Optimizer createOptimizer() { |
316 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
317 |
Optimizer optimizer = manager.createOptimizer(); |
318 |
return optimizer;
|
319 |
} |
320 |
|
321 |
public static String repr(Object value) { |
322 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
323 |
ReprMethod method = manager.getReprMethod(value); |
324 |
return method.repr(value);
|
325 |
} |
326 |
|
327 |
public static CodeBuilder createCodeBuilder() { |
328 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
329 |
return manager.createCodeBuilder();
|
330 |
} |
331 |
} |