# HG changeset patch # User Sascha L. Teichmann # Date 1364310972 -3600 # Node ID ba489a16f4d8fc2bb836dfe8dca41ba609656bdd # Parent 13596605e81fe44ff6516312a704317871cb580f Datacage Function Resolver: Lift from static to object space. diff -r 13596605e81f -r ba489a16f4d8 flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java Tue Mar 26 14:02:58 2013 +0100 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java Tue Mar 26 16:16:12 2013 +0100 @@ -39,168 +39,35 @@ } } // class Entry - public static final FunctionResolver FUNCTIONS = new FunctionResolver(); - - static { - /** Implementation of case-ignoring dc:contains. */ - FUNCTIONS.addFunction("contains", 2, new XPathFunction() { - @Override - public Object evaluate(List args) throws XPathFunctionException { - Object haystack = args.get(0); - Object needle = args.get(1); - - if (needle instanceof String) { - needle = ((String)needle).toUpperCase(); - } - - try { - if (haystack instanceof Collection) { - return Boolean.valueOf( - ((Collection)haystack).contains(needle)); - } - - if (haystack instanceof Map) { - return Boolean.valueOf( - ((Map)haystack).containsKey(needle)); - } - - if (haystack instanceof Object []) { - for (Object straw: (Object [])haystack) { - if (straw.equals(needle)) { - return Boolean.TRUE; - } - } - } - - return Boolean.FALSE; - } - catch (Exception e) { - log.error(e); - throw new XPathFunctionException(e); - } - } - }); - } - - static { - /** Implementation for getting the minimum value of location or distance - * dc:fromValue. */ - FUNCTIONS.addFunction("fromValue", 3, new XPathFunction() { - @Override - public Object evaluate(List args) throws XPathFunctionException { - Object mode = args.get(0); - Object locations = args.get(1); - Object from = args.get(2); - - if (!(mode instanceof String)){ - return -99999d; - } - - if (mode.equals("locations")) { - if (!(locations instanceof String)) { - return -99999d; - } - String loc = ((String)locations).replace(" ", ""); - String[] split = loc.split(","); - if (split.length < 1) { - return -99999d; - } - try { - double min = Double.parseDouble(split[0]); - for (int i = 1; i < split.length; ++i) { - double v = Double.parseDouble(split[i]); - if (v < min) { - min = v; - } - } - return min; - } - catch (NumberFormatException nfe) { - return -99999d; - } - } - else if (mode.equals("distance")) { - if (!(from instanceof String)) { - return -99999d; - } - String f = (String)from; - try { - return Double.parseDouble(f); - } - catch(NumberFormatException nfe) { - return -99999d; - } - } - else { - return -99999d; - } - } - }); - } - - static { - /** Implementation for getting the maximum value of location or distance - * dc:toValue. */ - FUNCTIONS.addFunction("toValue", 3, new XPathFunction() { - @Override - public Object evaluate(List args) throws XPathFunctionException { - Object mode = args.get(0); - Object locations = args.get(1); - Object to = args.get(2); - - if (!(mode instanceof String)){ - return 99999d; - } - - if (mode.equals("locations")) { - if (!(locations instanceof String)) { - return 99999d; - } - try { - String loc = ((String)locations).replace(" ", ""); - String[] split = loc.split(","); - if (split.length < 1) { - return 99999d; - } - double max = Double.parseDouble(split[0]); - for (int i = 1; i < split.length; ++i) { - double v = Double.parseDouble(split[i]); - if (v > max) { - max = v; - } - } - return max; - } - catch (NumberFormatException nfe) { - return 99999d; - } - } - else if (mode.equals("distance")) { - if (!(to instanceof String)) { - return 99999d; - } - else { - String t = (String)to; - try { - return Double.parseDouble(t); - } - catch(NumberFormatException nfe) { - return 99999d; - } - } - } - else { - return 99999d; - } - } - }); - } - /** List of functions. */ protected List functions; + public static final FunctionResolver FUNCTIONS = new FunctionResolver(); + + public FunctionResolver() { functions = new ArrayList(); + + addFunction("contains", 2, new XPathFunction() { + @Override + public Object evaluate(List args) throws XPathFunctionException { + return contains(args); + } + }); + + addFunction("fromValue", 3, new XPathFunction() { + @Override + public Object evaluate(List args) throws XPathFunctionException { + return fromValue(args); + } + }); + + addFunction("toValue", 3, new XPathFunction() { + @Override + public Object evaluate(List args) throws XPathFunctionException { + return toValue(args); + } + }); } /** @@ -229,5 +96,147 @@ return null; } + + /** Implementation of case-ignoring dc:contains. */ + public Object contains(List args) throws XPathFunctionException { + Object haystack = args.get(0); + Object needle = args.get(1); + + if (needle instanceof String) { + needle = ((String)needle).toUpperCase(); + } + + try { + if (haystack instanceof Collection) { + return Boolean.valueOf( + ((Collection)haystack).contains(needle)); + } + + if (haystack instanceof Map) { + return Boolean.valueOf( + ((Map)haystack).containsKey(needle)); + } + + if (haystack instanceof Object []) { + for (Object straw: (Object [])haystack) { + if (straw.equals(needle)) { + return Boolean.TRUE; + } + } + } + + return Boolean.FALSE; + } + catch (Exception e) { + log.error(e); + throw new XPathFunctionException(e); + } + } + + /** Implementation for getting the minimum value of location or distance + * dc:fromValue. + */ + public Object fromValue(List args) throws XPathFunctionException { + Object mode = args.get(0); + Object locations = args.get(1); + Object from = args.get(2); + + if (!(mode instanceof String)){ + return -99999d; + } + + if (mode.equals("locations")) { + if (!(locations instanceof String)) { + return -99999d; + } + String loc = ((String)locations).replace(" ", ""); + String[] split = loc.split(","); + if (split.length < 1) { + return -99999d; + } + try { + double min = Double.parseDouble(split[0]); + for (int i = 1; i < split.length; ++i) { + double v = Double.parseDouble(split[i]); + if (v < min) { + min = v; + } + } + return min; + } + catch (NumberFormatException nfe) { + return -99999d; + } + } + else if (mode.equals("distance")) { + if (!(from instanceof String)) { + return -99999d; + } + String f = (String)from; + try { + return Double.parseDouble(f); + } + catch(NumberFormatException nfe) { + return -99999d; + } + } + else { + return -99999d; + } + } + + /** Implementation for getting the maximum value of location or distance + * dc:toValue. + */ + public Object toValue(List args) throws XPathFunctionException { + Object mode = args.get(0); + Object locations = args.get(1); + Object to = args.get(2); + + if (!(mode instanceof String)){ + return 99999d; + } + + if (mode.equals("locations")) { + if (!(locations instanceof String)) { + return 99999d; + } + try { + String loc = ((String)locations).replace(" ", ""); + String[] split = loc.split(","); + if (split.length < 1) { + return 99999d; + } + double max = Double.parseDouble(split[0]); + for (int i = 1; i < split.length; ++i) { + double v = Double.parseDouble(split[i]); + if (v > max) { + max = v; + } + } + return max; + } + catch (NumberFormatException nfe) { + return 99999d; + } + } + else if (mode.equals("distance")) { + if (!(to instanceof String)) { + return 99999d; + } + else { + String t = (String)to; + try { + return Double.parseDouble(t); + } + catch(NumberFormatException nfe) { + return 99999d; + } + } + } + else { + return 99999d; + } + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :