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 @ 44446
History | View | Annotate | Download (10.2 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 | 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 | 44215 | jjdelcerro | if( StringUtils.isBlank(phrase) ) {
|
58 | return null; |
||
59 | } |
||
60 | 44163 | jjdelcerro | 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 | 44198 | jjdelcerro | |
85 | public static ExpressionBuilder createExpressionBuilder() { |
||
86 | ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder(); |
||
87 | return builder;
|
||
88 | } |
||
89 | |||
90 | public static Code compile(String expression) { |
||
91 | 44215 | jjdelcerro | if( StringUtils.isBlank(expression) ) {
|
92 | return null; |
||
93 | } |
||
94 | 44198 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
95 | Code code = manager.compile(expression); |
||
96 | return code;
|
||
97 | } |
||
98 | |||
99 | 44346 | jjdelcerro | public static Object evaluate(String expression) { |
100 | return evaluate(null, expression); |
||
101 | } |
||
102 | |||
103 | 44198 | jjdelcerro | public static Object evaluate(SymbolTable symbolTable, String expression) { |
104 | 44215 | jjdelcerro | if( StringUtils.isBlank(expression) ) {
|
105 | return null; |
||
106 | } |
||
107 | 44198 | jjdelcerro | 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 | 44215 | jjdelcerro | 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 | 44397 | jjdelcerro | |
170 | 44408 | jjdelcerro | 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 | 44397 | jjdelcerro | 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 | 44215 | jjdelcerro | |
192 | 44397 | jjdelcerro | public static String evaluateDynamicText(String source) { |
193 | 44389 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
194 | 44397 | jjdelcerro | return manager.evaluateDynamicText(source);
|
195 | 44389 | jjdelcerro | } |
196 | |||
197 | 44397 | jjdelcerro | public static String evaluateDynamicText(SymbolTable symbolTable, String source) { |
198 | 44389 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
199 | 44397 | jjdelcerro | return manager.evaluateDynamicText(symbolTable, source);
|
200 | 44389 | jjdelcerro | } |
201 | |||
202 | 44390 | jjdelcerro | public static File evaluateFilename(File source) { |
203 | 44389 | jjdelcerro | return evaluateFilename(null, source); |
204 | } |
||
205 | 44397 | jjdelcerro | |
206 | public static boolean isDynamicFilename(File source) { |
||
207 | if( source == null ) { |
||
208 | return false; |
||
209 | } |
||
210 | return isDynamicText(source.getPath());
|
||
211 | } |
||
212 | 44389 | jjdelcerro | |
213 | @SuppressWarnings("StringEquality") |
||
214 | 44390 | jjdelcerro | public static File evaluateFilename(SymbolTable symbolTable, File source) { |
215 | 44389 | jjdelcerro | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
216 | String src = source.getPath();
|
||
217 | 44397 | jjdelcerro | String r = manager.evaluateDynamicText(symbolTable, src);
|
218 | 44389 | jjdelcerro | 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 | 44444 | jjdelcerro | |
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 | 44446 | jjdelcerro | SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
231 | 44444 | jjdelcerro | 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 | 44446 | jjdelcerro | SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
254 | 44444 | jjdelcerro | 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 | 44446 | jjdelcerro | SymbolTable symbolTable = null; //manager.getInmutableSymbolTable(); |
277 | 44444 | jjdelcerro | 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 | 44163 | jjdelcerro | } |