Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java @ 4902:e1566938d04c
Added new functions to datacage templating language.
* get minimum of location/distance:
dc:fromValue($ld_mode, $ld_locations, $ld_from)
- returns -Double.MAX_VALUE if no min exists.
* get maximum of location/distance:
dc:toValue($ld_mode, $ld_locations, $ld_to)
- returns Double.MAX_VALUE if no max exists.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Tue, 29 Jan 2013 17:11:26 +0100 |
parents | 2970046fcdca |
children | 5ab87837622f |
line wrap: on
line source
package de.intevation.flys.artifacts.datacage.templating; import java.util.List; import java.util.Collection; import java.util.Map; import java.util.ArrayList; import javax.xml.xpath.XPathFunctionResolver; import javax.xml.xpath.XPathFunction; import javax.xml.xpath.XPathFunctionException; import javax.xml.namespace.QName; import org.apache.log4j.Logger; /** Resolves functions (e.g. dc:contains) in Datacage/Meta-Data system. */ public class FunctionResolver implements XPathFunctionResolver { /** Home logger. */ private static Logger log = Logger.getLogger(FunctionResolver.class); public static final String FUNCTION_NAMESPACE_URI = "dc"; public static final class Entry { String name; XPathFunction function; int arity; public Entry() { } public Entry(String name, XPathFunction function, int arity) { this.name = name; this.function = function; this.arity = arity; } } // 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 -Double.MAX_VALUE; } if (mode.equals("locations")) { if (!(locations instanceof String)) { return -Double.MAX_VALUE; } else { String loc = ((String)locations).replace(" ", ""); String[] split = loc.split(","); return split[0]; } } else if (mode.equals("distance")) { if (!(from instanceof String)) { return -Double.MAX_VALUE; } String f = (String)from; try { return Double.parseDouble(f); } catch(NumberFormatException nfe) { return -Double.MAX_VALUE; } } else { return -Double.MAX_VALUE; } } }); } 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 Double.MAX_VALUE; } if (mode.equals("locations")) { if (!(locations instanceof String)) { return Double.MAX_VALUE; } else { String loc = ((String)locations).replace(" ", ""); String[] split = loc.split(","); return split[split.length - 1]; } } else if (mode.equals("distance")) { if (!(to instanceof String)) { return Double.MAX_VALUE; } else { String t = (String)to; try { return Double.parseDouble(t); } catch(NumberFormatException nfe) { return Double.MAX_VALUE; } } } else { return Double.MAX_VALUE; } } }); } /** List of functions. */ protected List<Entry> functions; public FunctionResolver() { functions = new ArrayList<Entry>(); } /** * Create a new function. * @param name Name of the function. * @param arity Number of arguments for function. * @param function the function itself. */ public void addFunction(String name, int arity, XPathFunction function) { functions.add(new Entry(name, function, arity)); } @Override public XPathFunction resolveFunction(QName functionName, int arity) { if (!functionName.getNamespaceURI().equals(FUNCTION_NAMESPACE_URI)) { return null; } String name = functionName.getLocalPart(); for (Entry entry: functions) { if (entry.arity == arity && entry.name.equals(name)) { return entry.function; } } return null; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :