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 @ 44389
History | View | Annotate | Download (6.44 KB)
1 |
package org.gvsig.expressionevaluator; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import org.apache.commons.lang3.StringUtils; |
5 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
6 |
import org.gvsig.tools.script.Script; |
7 |
|
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 |
if( StringUtils.isBlank(phrase) ) {
|
57 |
return null; |
58 |
} |
59 |
Expression expression = ExpressionEvaluatorLocator.getManager().createExpression();
|
60 |
expression.setPhrase(phrase); |
61 |
return expression;
|
62 |
} |
63 |
|
64 |
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 |
|
84 |
public static ExpressionBuilder createExpressionBuilder() { |
85 |
ExpressionBuilder builder = ExpressionEvaluatorLocator.getManager().createExpressionBuilder(); |
86 |
return builder;
|
87 |
} |
88 |
|
89 |
public static Code compile(String expression) { |
90 |
if( StringUtils.isBlank(expression) ) {
|
91 |
return null; |
92 |
} |
93 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
94 |
Code code = manager.compile(expression); |
95 |
return code;
|
96 |
} |
97 |
|
98 |
public static Object evaluate(String expression) { |
99 |
return evaluate(null, expression); |
100 |
} |
101 |
|
102 |
public static Object evaluate(SymbolTable symbolTable, String expression) { |
103 |
if( StringUtils.isBlank(expression) ) {
|
104 |
return null; |
105 |
} |
106 |
if( symbolTable==null ) { |
107 |
symbolTable = createSymbolTable(); |
108 |
} |
109 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
110 |
Object x = manager.evaluate(symbolTable, expression);
|
111 |
return x;
|
112 |
} |
113 |
|
114 |
public static Object evaluate(SymbolTable symbolTable, Code code) { |
115 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
116 |
Object x = manager.evaluate(symbolTable, code);
|
117 |
return x;
|
118 |
} |
119 |
|
120 |
public static Code optimize(SymbolTable symbolTable, Code code) { |
121 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
122 |
code = manager.optimize(symbolTable, code); |
123 |
return code;
|
124 |
} |
125 |
|
126 |
public static String toString(Value value, Formatter formatter) { |
127 |
if( value == null ) { |
128 |
return null; |
129 |
} |
130 |
if( formatter==null ) { |
131 |
formatter = ExpressionBuilder.EMPTY_FORMATTER; |
132 |
} |
133 |
return value.toString(formatter);
|
134 |
} |
135 |
|
136 |
public static String toString(Value value) { |
137 |
if( value == null ) { |
138 |
return null; |
139 |
} |
140 |
return value.toString(ExpressionBuilder.EMPTY_FORMATTER);
|
141 |
} |
142 |
|
143 |
public static String toString(Code code, Formatter formatter) { |
144 |
if( code == null ) { |
145 |
return null; |
146 |
} |
147 |
if( formatter==null ) { |
148 |
formatter = Code.EMPTY_FORMATTER; |
149 |
} |
150 |
return code.toString(formatter);
|
151 |
} |
152 |
|
153 |
public static String toString(Code code) { |
154 |
if( code == null ) { |
155 |
return null; |
156 |
} |
157 |
return code.toString(Code.EMPTY_FORMATTER);
|
158 |
} |
159 |
|
160 |
public static Expression createExpressionFromJSON(String json) { |
161 |
Expression expression = ExpressionUtils.createExpression();
|
162 |
expression.fromJSON(json); |
163 |
return expression;
|
164 |
} |
165 |
|
166 |
public static MutableSymbolTable createSymbolTable() { |
167 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
168 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
169 |
return symbolTable;
|
170 |
} |
171 |
|
172 |
public String evaluatePage(String source) { |
173 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
174 |
return manager.evaluatePage(source);
|
175 |
} |
176 |
|
177 |
public String evaluatePage(SymbolTable symbolTable, String source) { |
178 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
179 |
return manager.evaluatePage(symbolTable, source);
|
180 |
} |
181 |
|
182 |
public File evaluateFilename(File source) { |
183 |
return evaluateFilename(null, source); |
184 |
} |
185 |
|
186 |
@SuppressWarnings("StringEquality") |
187 |
public File evaluateFilename(SymbolTable symbolTable, File source) { |
188 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
189 |
String src = source.getPath();
|
190 |
String r = manager.evaluatePage(symbolTable, src);
|
191 |
if( r == src ) { // !!! I compare that it is the same pointer, it is what I want. |
192 |
return source;
|
193 |
} |
194 |
File f = new File(r); |
195 |
return f;
|
196 |
} |
197 |
|
198 |
} |