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