Statistics
| Revision:

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 @ 44644

History | View | Annotate | Download (11.1 KB)

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