comparison artifacts/src/main/java/org/dive4elements/river/artifacts/datacage/templating/FunctionResolver.java @ 7968:09c9920e6f24

Added dc:coalesce(a, b). Returns first element which is not empty or not zero.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 26 Jun 2014 12:54:31 +0200
parents 6d5cbd69511a
children 912cf4ec09d1
comparison
equal deleted inserted replaced
7967:6d5cbd69511a 7968:09c9920e6f24
84 public FunctionResolver(Builder.BuildHelper buildHelper) { 84 public FunctionResolver(Builder.BuildHelper buildHelper) {
85 this.buildHelper = buildHelper; 85 this.buildHelper = buildHelper;
86 86
87 functions = new HashMap<String, Entry>(); 87 functions = new HashMap<String, Entry>();
88 88
89 addFunction("coalesce", 2, new XPathFunction() {
90 @Override
91 public Object evaluate(List args) throws XPathFunctionException {
92 return coalesce(args);
93 }
94 });
95
89 addFunction("lowercase", 1, new XPathFunction() { 96 addFunction("lowercase", 1, new XPathFunction() {
90 @Override 97 @Override
91 public Object evaluate(List args) throws XPathFunctionException { 98 public Object evaluate(List args) throws XPathFunctionException {
92 return args.get(0).toString().toLowerCase(); 99 return args.get(0).toString().toLowerCase();
93 } 100 }
544 551
545 return list instanceof Number 552 return list instanceof Number
546 ? (Number)list 553 ? (Number)list
547 : Double.valueOf(-Double.MAX_VALUE); 554 : Double.valueOf(-Double.MAX_VALUE);
548 } 555 }
556
557 public static Object coalesce(List list) {
558 for (Object x: list) {
559 if (x instanceof String && ((String)x).length() != 0) {
560 return x;
561 }
562 if (x instanceof Number && ((Number)x).doubleValue() != 0.0) {
563 return x;
564 }
565 }
566 return StackFrames.NULL;
567 }
549 } 568 }
550 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 569 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org