Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/meta/FunctionResolver.java @ 959:a2b20ed3d3b4
Datacage: Added custom provider for XPath functions.
flys-artifacts/trunk@2378 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 20 Jul 2011 16:28:49 +0000 |
parents | |
children | 30c85cb33a50 |
comparison
equal
deleted
inserted
replaced
958:ae198bef4ba0 | 959:a2b20ed3d3b4 |
---|---|
1 package de.intevation.flys.artifacts.services.meta; | |
2 | |
3 import java.util.List; | |
4 import java.util.ArrayList; | |
5 | |
6 import javax.xml.xpath.XPathFunctionResolver; | |
7 import javax.xml.xpath.XPathFunction; | |
8 import javax.xml.xpath.XPathFunctionException; | |
9 | |
10 import javax.xml.namespace.QName; | |
11 | |
12 public class FunctionResolver | |
13 implements XPathFunctionResolver | |
14 { | |
15 public static final String FUNCTION_NAMESPACE_URI = "dc"; | |
16 | |
17 public static final class Entry { | |
18 | |
19 String name; | |
20 XPathFunction function; | |
21 int arity; | |
22 | |
23 public Entry() { | |
24 } | |
25 | |
26 public Entry(String name, XPathFunction function, int arity) { | |
27 this.name = name; | |
28 this.function = function; | |
29 this.arity = arity; | |
30 } | |
31 } // class Entry | |
32 | |
33 public static final FunctionResolver FUNCTIONS = new FunctionResolver(); | |
34 | |
35 static { | |
36 FUNCTIONS.addFunction("contains", 2, new XPathFunction() { | |
37 @Override | |
38 public Object evaluate(List args) throws XPathFunctionException { | |
39 try { | |
40 Object [] haystack = (Object [])args.get(0); | |
41 Object needle = args.get(1); | |
42 for (Object straw: haystack) { | |
43 if (straw.equals(needle)) { | |
44 return Boolean.TRUE; | |
45 } | |
46 } | |
47 return Boolean.FALSE; | |
48 } | |
49 catch (Exception e) { | |
50 throw new XPathFunctionException(e); | |
51 } | |
52 } | |
53 }); | |
54 } | |
55 | |
56 protected List<Entry> functions; | |
57 | |
58 public FunctionResolver() { | |
59 functions = new ArrayList<Entry>(); | |
60 } | |
61 | |
62 public void addFunction(String name, int arity, XPathFunction function) { | |
63 functions.add(new Entry(name, function, arity)); | |
64 } | |
65 | |
66 @Override | |
67 public XPathFunction resolveFunction(QName functionName, int arity) { | |
68 | |
69 if (!functionName.getNamespaceURI().equals(FUNCTION_NAMESPACE_URI)) { | |
70 return null; | |
71 } | |
72 | |
73 String name = functionName.getLocalPart(); | |
74 for (Entry entry: functions) { | |
75 if (entry.arity == arity && entry.name.equals(name)) { | |
76 return entry.function; | |
77 } | |
78 } | |
79 | |
80 return null; | |
81 } | |
82 } | |
83 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |