Revision 44750 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/main/java/org/gvsig/expressionevaluator/impl/DefaultCodeBuilder.java
DefaultCodeBuilder.java | ||
---|---|---|
5 | 5 |
import java.util.Iterator; |
6 | 6 |
import java.util.List; |
7 | 7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
|
9 |
import org.apache.commons.lang3.tuple.Pair; |
|
10 | 8 |
import org.gvsig.expressionevaluator.Code; |
11 | 9 |
import static org.gvsig.expressionevaluator.Code.CALLER; |
12 | 10 |
import static org.gvsig.expressionevaluator.Code.CONSTANT; |
... | ... | |
22 | 20 |
import org.gvsig.expressionevaluator.CodeBuilder; |
23 | 21 |
import org.gvsig.expressionevaluator.Codes; |
24 | 22 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
23 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
|
25 | 24 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_ADD; |
26 | 25 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_AND; |
27 | 26 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.OPERATOR_CONCAT; |
... | ... | |
98 | 97 |
} |
99 | 98 |
} |
100 | 99 |
} |
100 |
|
|
101 |
@Override |
|
102 |
public void replace(Code target, Code replacement) { |
|
103 |
} |
|
104 |
|
|
105 |
|
|
101 | 106 |
} |
102 | 107 |
|
103 | 108 |
class BaseConstant extends BaseCode implements Constant { |
... | ... | |
239 | 244 |
|
240 | 245 |
public class BaseCodes implements Codes { |
241 | 246 |
|
242 |
private final List<Pair<String,Code>> codes; |
|
243 |
private boolean useNames; |
|
247 |
private final List<Code> codes; |
|
244 | 248 |
|
245 | 249 |
public BaseCodes() { |
246 | 250 |
this.codes = new ArrayList<>(); |
247 |
this.useNames = false; |
|
248 | 251 |
} |
249 | 252 |
|
250 | 253 |
@Override |
251 |
public boolean useArgNames() { |
|
252 |
return this.useNames; |
|
253 |
} |
|
254 |
|
|
255 |
@Override |
|
256 |
public boolean useArgNames(boolean useNames) { |
|
257 |
this.useNames = useNames; |
|
258 |
return this.useNames; |
|
259 |
} |
|
260 |
|
|
261 |
@Override |
|
262 | 254 |
public int code() { |
263 | 255 |
return CODES; |
264 | 256 |
} |
... | ... | |
271 | 263 |
return this.codes.size(); |
272 | 264 |
} |
273 | 265 |
|
274 |
public void add(String name, Code parameter) { |
|
275 |
if( !StringUtils.isBlank(name) ) { |
|
276 |
for (int i = 0; i < codes.size(); i++) { |
|
277 |
Pair<String, Code> arg = codes.get(i); |
|
278 |
if( StringUtils.equalsIgnoreCase(name, arg.getKey()) ) { |
|
279 |
codes.set(i, new ImmutablePair<>(name, parameter)); |
|
280 |
return; |
|
281 |
} |
|
282 |
} |
|
283 |
} |
|
284 |
this.codes.add(new ImmutablePair<>(name, parameter)); |
|
266 |
public void add(Code arg) { |
|
267 |
this.codes.add(arg); |
|
285 | 268 |
} |
286 | 269 |
|
287 |
public void add(Code arg) {
|
|
288 |
this.codes.add(new ImmutablePair<>(null, arg));
|
|
270 |
public void set(int pos, Code arg) {
|
|
271 |
this.codes.set(pos, arg);
|
|
289 | 272 |
} |
290 | 273 |
|
291 | 274 |
public void insert(int pos, Code arg) { |
292 |
this.codes.add(pos, new ImmutablePair<>(null, arg));
|
|
275 |
this.codes.add(pos, arg);
|
|
293 | 276 |
} |
294 | 277 |
|
295 | 278 |
@Override |
296 |
public boolean contains(String name) { |
|
297 |
for (Pair<String, Code> arg : this.codes) { |
|
298 |
if( StringUtils.equalsIgnoreCase(arg.getKey(), name) ) { |
|
299 |
return true; |
|
300 |
} |
|
301 |
} |
|
302 |
return false; |
|
303 |
} |
|
304 |
|
|
305 |
@Override |
|
306 |
public boolean contains(String name, int index) { |
|
307 |
String argNameX = name + String.valueOf(index).trim(); |
|
308 |
for (Pair<String, Code> arg : this.codes) { |
|
309 |
if( StringUtils.equalsIgnoreCase(arg.getKey(), argNameX) ) { |
|
310 |
return true; |
|
311 |
} |
|
312 |
} |
|
313 |
return false; |
|
314 |
} |
|
315 |
|
|
316 |
@Override |
|
317 | 279 |
public Iterator<Code> iterator() { |
318 |
final Iterator<Pair<String, Code>> it = this.codes.iterator(); |
|
319 |
return new Iterator<Code>() { |
|
320 |
@Override |
|
321 |
public boolean hasNext() { |
|
322 |
return it.hasNext(); |
|
323 |
} |
|
324 |
|
|
325 |
@Override |
|
326 |
public Code next() { |
|
327 |
Pair<String, Code> arg = it.next(); |
|
328 |
return arg.getValue(); |
|
329 |
} |
|
330 |
}; |
|
280 |
final Iterator<Code> it = this.codes.iterator(); |
|
281 |
return it; |
|
331 | 282 |
} |
332 | 283 |
|
333 | 284 |
@Override |
334 | 285 |
public Code get(int n) { |
335 |
return this.codes.get(n).getValue();
|
|
286 |
return this.codes.get(n); |
|
336 | 287 |
} |
337 | 288 |
|
338 | 289 |
@Override |
339 |
public Code get(String name) { |
|
340 |
for (Pair<String, Code> arg : this.codes) { |
|
341 |
if( StringUtils.equalsIgnoreCase(name, arg.getKey()) ) { |
|
342 |
return arg.getValue(); |
|
343 |
} |
|
344 |
} |
|
345 |
return null; |
|
346 |
} |
|
347 |
|
|
348 |
@Override |
|
349 |
public String getName(int n) { |
|
350 |
return this.codes.get(n).getKey(); |
|
351 |
} |
|
352 |
|
|
353 |
@Override |
|
354 | 290 |
public boolean isEmpty() { |
355 | 291 |
return this.codes.isEmpty(); |
356 | 292 |
} |
357 | 293 |
|
358 | 294 |
@Override |
359 | 295 |
public List<Code> toList() { |
360 |
List<Code> l = new ArrayList<>(); |
|
361 |
for (Pair<String, Code> arg : this.codes) { |
|
362 |
l.add(arg.getValue()); |
|
363 |
} |
|
364 |
return Collections.unmodifiableList(l); |
|
296 |
return Collections.unmodifiableList(this.codes); |
|
365 | 297 |
} |
366 | 298 |
|
367 | 299 |
@Override |
368 | 300 |
public void accept(Visitor visitor) throws BaseException { |
369 |
for( Pair<String,Code> arg : this.codes ) {
|
|
370 |
arg.getValue().accept(visitor);
|
|
301 |
for( Code arg : this.codes ) {
|
|
302 |
arg.accept(visitor); |
|
371 | 303 |
} |
372 | 304 |
} |
373 | 305 |
|
... | ... | |
394 | 326 |
if( codes != null ) { |
395 | 327 |
StringBuilder builder = new StringBuilder(); |
396 | 328 |
boolean skipcoma = true; |
397 |
for( Pair<String, Code> arg : codes ) {
|
|
329 |
for( Code arg : codes ) {
|
|
398 | 330 |
if( arg == null ) { |
399 | 331 |
continue; |
400 | 332 |
} |
401 |
String name = arg.getKey(); |
|
402 |
Code code = arg.getValue(); |
|
403 |
if( code == null ) { |
|
404 |
continue; |
|
405 |
} |
|
406 | 333 |
if( skipcoma ) { |
407 | 334 |
skipcoma = false; |
408 | 335 |
} else { |
409 | 336 |
builder.append(", "); |
410 | 337 |
} |
411 |
if( name==null || !useNames ) { |
|
412 |
builder.append(code.toString(formatter)); |
|
413 |
} else { |
|
414 |
builder.append(name); |
|
415 |
builder.append(":"); |
|
416 |
builder.append(code.toString(formatter)); |
|
417 |
} |
|
338 |
builder.append(arg.toString(formatter)); |
|
418 | 339 |
} |
419 | 340 |
return builder.toString(); |
420 | 341 |
} |
... | ... | |
423 | 344 |
|
424 | 345 |
@Override |
425 | 346 |
public void link(SymbolTable symbolTable) { |
426 |
for (Pair<String,Code> arg : this.codes) {
|
|
427 |
arg.getValue().link(symbolTable);
|
|
347 |
for (Code arg : this.codes) {
|
|
348 |
arg.link(symbolTable); |
|
428 | 349 |
} |
429 | 350 |
} |
430 | 351 |
|
352 |
@Override |
|
353 |
public void replace(Code target, Code replacement) { |
|
354 |
for (int i = 0; i < this.codes.size(); i++) { |
|
355 |
Code code = this.codes.get(i); |
|
356 |
if( code == target ) { |
|
357 |
codes.set(i, replacement); |
|
358 |
} else { |
|
359 |
code.replace(target, replacement); |
|
360 |
} |
|
361 |
} |
|
362 |
} |
|
363 |
|
|
431 | 364 |
} |
432 | 365 |
|
433 | 366 |
public class BaseCaller extends BaseCode implements Caller, RecursionControlSupport { |
... | ... | |
452 | 385 |
} |
453 | 386 |
|
454 | 387 |
@Override |
388 |
public void replace(Code target, Code replacement) { |
|
389 |
this.args.replace(target, replacement); |
|
390 |
} |
|
391 |
|
|
392 |
@Override |
|
455 | 393 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
456 | 394 |
return this.function.call(interpreter, args); |
457 | 395 |
} |
... | ... | |
469 | 407 |
@Override |
470 | 408 |
public Function function(Function function) { |
471 | 409 |
this.function = function; |
472 |
if( this.args!=null && this.function!=null ) { |
|
473 |
this.args.useArgNames(this.function.allowArgNames()); |
|
474 |
} |
|
475 | 410 |
return this.function; |
476 | 411 |
} |
477 | 412 |
|
... | ... | |
515 | 450 |
if( this.parameters()!=null ) { |
516 | 451 |
for (Code parameter : this.parameters()) { |
517 | 452 |
f.parameter(parameter.toValue(builder)); |
518 |
} |
|
519 |
|
|
453 |
} |
|
520 | 454 |
} |
521 | 455 |
return f; |
522 | 456 |
|
... | ... | |
570 | 504 |
if( StringUtils.equalsIgnoreCase(this.name(),ExpressionBuilder.FUNCTION_GETATTR) ) { |
571 | 505 |
Code arg0 = this.parameters().get(0); |
572 | 506 |
Code arg1 = this.parameters().get(1); |
573 |
if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant ) {
|
|
507 |
if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Identifier ) {
|
|
574 | 508 |
builder.append(arg0.toString(formatter)); |
575 |
builder.append(".\""); |
|
576 |
builder.append(((Code.Constant)arg1).value()); |
|
577 |
builder.append("\""); |
|
509 |
builder.append("."); |
|
510 |
builder.append(arg1.toString()); |
|
578 | 511 |
} else { |
579 | 512 |
builder.append(this.name()); |
580 | 513 |
builder.append("("); |
... | ... | |
613 | 546 |
|
614 | 547 |
public class BaseMethod extends BaseCode implements Method { |
615 | 548 |
|
616 |
private final Code instance;
|
|
549 |
private Code instance; |
|
617 | 550 |
private final String methodname; |
618 | 551 |
private final Codes args; |
619 | 552 |
|
... | ... | |
629 | 562 |
} |
630 | 563 |
|
631 | 564 |
@Override |
565 |
public void replace(Code target, Code replacement) { |
|
566 |
if( target == this.instance ) { |
|
567 |
this.instance = replacement; |
|
568 |
} |
|
569 |
this.args.replace(target, replacement); |
|
570 |
} |
|
571 |
|
|
572 |
@Override |
|
632 | 573 |
public String methodname() { |
633 | 574 |
return this.methodname; |
634 | 575 |
} |
... | ... | |
729 | 670 |
} |
730 | 671 |
|
731 | 672 |
@Override |
673 |
public Caller tuple() { |
|
674 |
BaseCodes args = this.args(); |
|
675 |
return function(FUNCTION_TUPLE, args); |
|
676 |
} |
|
677 |
|
|
678 |
@Override |
|
679 |
public Caller tuple(Codes args) { |
|
680 |
if( args == null ) { |
|
681 |
args = this.args(); |
|
682 |
} |
|
683 |
return function(FUNCTION_TUPLE, args); |
|
684 |
} |
|
685 |
|
|
686 |
@Override |
|
732 | 687 |
public Caller function(String name, int type, Codes args) { |
733 | 688 |
return new BaseCaller(name, type, args); |
734 | 689 |
} |
... | ... | |
863 | 818 |
public Code getattr(Code obj, String attrname) { |
864 | 819 |
BaseCodes args = args(); |
865 | 820 |
args.add(obj); |
866 |
args.add(constant(attrname));
|
|
821 |
args.add(identifier(attrname));
|
|
867 | 822 |
return function(ExpressionBuilder.FUNCTION_GETATTR, args); |
868 | 823 |
} |
869 | 824 |
|
Also available in: Unified diff