Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java @ 1072:80aecb01d79a
Let MainValuesArtifact have a state, outputmode and facet.
flys-artifacts/trunk@2556 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 24 Aug 2011 12:07:33 +0000 |
parents | b81626b10cb7 |
children | 3ca999f507b7 |
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; public class FunctionResolver implements XPathFunctionResolver { 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 { FUNCTIONS.addFunction("contains", 2, new XPathFunction() { @Override public Object evaluate(List args) throws XPathFunctionException { Object haystack = args.get(0); Object needle = args.get(1); 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); } } }); } protected List<Entry> functions; public FunctionResolver() { functions = new ArrayList<Entry>(); } 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 :