comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java @ 5430:ba489a16f4d8

Datacage Function Resolver: Lift from static to object space.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 26 Mar 2013 16:16:12 +0100
parents 8c65acf01adc
children d0f5bc8064f1
comparison
equal deleted inserted replaced
5429:13596605e81f 5430:ba489a16f4d8
37 this.function = function; 37 this.function = function;
38 this.arity = arity; 38 this.arity = arity;
39 } 39 }
40 } // class Entry 40 } // class Entry
41 41
42 /** List of functions. */
43 protected List<Entry> functions;
44
42 public static final FunctionResolver FUNCTIONS = new FunctionResolver(); 45 public static final FunctionResolver FUNCTIONS = new FunctionResolver();
43 46
44 static { 47
45 /** Implementation of case-ignoring dc:contains. */ 48 public FunctionResolver() {
46 FUNCTIONS.addFunction("contains", 2, new XPathFunction() { 49 functions = new ArrayList<Entry>();
50
51 addFunction("contains", 2, new XPathFunction() {
47 @Override 52 @Override
48 public Object evaluate(List args) throws XPathFunctionException { 53 public Object evaluate(List args) throws XPathFunctionException {
49 Object haystack = args.get(0); 54 return contains(args);
50 Object needle = args.get(1);
51
52 if (needle instanceof String) {
53 needle = ((String)needle).toUpperCase();
54 }
55
56 try {
57 if (haystack instanceof Collection) {
58 return Boolean.valueOf(
59 ((Collection)haystack).contains(needle));
60 }
61
62 if (haystack instanceof Map) {
63 return Boolean.valueOf(
64 ((Map)haystack).containsKey(needle));
65 }
66
67 if (haystack instanceof Object []) {
68 for (Object straw: (Object [])haystack) {
69 if (straw.equals(needle)) {
70 return Boolean.TRUE;
71 }
72 }
73 }
74
75 return Boolean.FALSE;
76 }
77 catch (Exception e) {
78 log.error(e);
79 throw new XPathFunctionException(e);
80 }
81 } 55 }
82 }); 56 });
83 } 57
84 58 addFunction("fromValue", 3, new XPathFunction() {
85 static {
86 /** Implementation for getting the minimum value of location or distance
87 * dc:fromValue. */
88 FUNCTIONS.addFunction("fromValue", 3, new XPathFunction() {
89 @Override 59 @Override
90 public Object evaluate(List args) throws XPathFunctionException { 60 public Object evaluate(List args) throws XPathFunctionException {
91 Object mode = args.get(0); 61 return fromValue(args);
92 Object locations = args.get(1);
93 Object from = args.get(2);
94
95 if (!(mode instanceof String)){
96 return -99999d;
97 }
98
99 if (mode.equals("locations")) {
100 if (!(locations instanceof String)) {
101 return -99999d;
102 }
103 String loc = ((String)locations).replace(" ", "");
104 String[] split = loc.split(",");
105 if (split.length < 1) {
106 return -99999d;
107 }
108 try {
109 double min = Double.parseDouble(split[0]);
110 for (int i = 1; i < split.length; ++i) {
111 double v = Double.parseDouble(split[i]);
112 if (v < min) {
113 min = v;
114 }
115 }
116 return min;
117 }
118 catch (NumberFormatException nfe) {
119 return -99999d;
120 }
121 }
122 else if (mode.equals("distance")) {
123 if (!(from instanceof String)) {
124 return -99999d;
125 }
126 String f = (String)from;
127 try {
128 return Double.parseDouble(f);
129 }
130 catch(NumberFormatException nfe) {
131 return -99999d;
132 }
133 }
134 else {
135 return -99999d;
136 }
137 } 62 }
138 }); 63 });
139 } 64
140 65 addFunction("toValue", 3, new XPathFunction() {
141 static {
142 /** Implementation for getting the maximum value of location or distance
143 * dc:toValue. */
144 FUNCTIONS.addFunction("toValue", 3, new XPathFunction() {
145 @Override 66 @Override
146 public Object evaluate(List args) throws XPathFunctionException { 67 public Object evaluate(List args) throws XPathFunctionException {
147 Object mode = args.get(0); 68 return toValue(args);
148 Object locations = args.get(1);
149 Object to = args.get(2);
150
151 if (!(mode instanceof String)){
152 return 99999d;
153 }
154
155 if (mode.equals("locations")) {
156 if (!(locations instanceof String)) {
157 return 99999d;
158 }
159 try {
160 String loc = ((String)locations).replace(" ", "");
161 String[] split = loc.split(",");
162 if (split.length < 1) {
163 return 99999d;
164 }
165 double max = Double.parseDouble(split[0]);
166 for (int i = 1; i < split.length; ++i) {
167 double v = Double.parseDouble(split[i]);
168 if (v > max) {
169 max = v;
170 }
171 }
172 return max;
173 }
174 catch (NumberFormatException nfe) {
175 return 99999d;
176 }
177 }
178 else if (mode.equals("distance")) {
179 if (!(to instanceof String)) {
180 return 99999d;
181 }
182 else {
183 String t = (String)to;
184 try {
185 return Double.parseDouble(t);
186 }
187 catch(NumberFormatException nfe) {
188 return 99999d;
189 }
190 }
191 }
192 else {
193 return 99999d;
194 }
195 } 69 }
196 }); 70 });
197 }
198
199 /** List of functions. */
200 protected List<Entry> functions;
201
202 public FunctionResolver() {
203 functions = new ArrayList<Entry>();
204 } 71 }
205 72
206 /** 73 /**
207 * Create a new function. 74 * Create a new function.
208 * @param name Name of the function. 75 * @param name Name of the function.
227 } 94 }
228 } 95 }
229 96
230 return null; 97 return null;
231 } 98 }
99
100 /** Implementation of case-ignoring dc:contains. */
101 public Object contains(List args) throws XPathFunctionException {
102 Object haystack = args.get(0);
103 Object needle = args.get(1);
104
105 if (needle instanceof String) {
106 needle = ((String)needle).toUpperCase();
107 }
108
109 try {
110 if (haystack instanceof Collection) {
111 return Boolean.valueOf(
112 ((Collection)haystack).contains(needle));
113 }
114
115 if (haystack instanceof Map) {
116 return Boolean.valueOf(
117 ((Map)haystack).containsKey(needle));
118 }
119
120 if (haystack instanceof Object []) {
121 for (Object straw: (Object [])haystack) {
122 if (straw.equals(needle)) {
123 return Boolean.TRUE;
124 }
125 }
126 }
127
128 return Boolean.FALSE;
129 }
130 catch (Exception e) {
131 log.error(e);
132 throw new XPathFunctionException(e);
133 }
134 }
135
136 /** Implementation for getting the minimum value of location or distance
137 * dc:fromValue.
138 */
139 public Object fromValue(List args) throws XPathFunctionException {
140 Object mode = args.get(0);
141 Object locations = args.get(1);
142 Object from = args.get(2);
143
144 if (!(mode instanceof String)){
145 return -99999d;
146 }
147
148 if (mode.equals("locations")) {
149 if (!(locations instanceof String)) {
150 return -99999d;
151 }
152 String loc = ((String)locations).replace(" ", "");
153 String[] split = loc.split(",");
154 if (split.length < 1) {
155 return -99999d;
156 }
157 try {
158 double min = Double.parseDouble(split[0]);
159 for (int i = 1; i < split.length; ++i) {
160 double v = Double.parseDouble(split[i]);
161 if (v < min) {
162 min = v;
163 }
164 }
165 return min;
166 }
167 catch (NumberFormatException nfe) {
168 return -99999d;
169 }
170 }
171 else if (mode.equals("distance")) {
172 if (!(from instanceof String)) {
173 return -99999d;
174 }
175 String f = (String)from;
176 try {
177 return Double.parseDouble(f);
178 }
179 catch(NumberFormatException nfe) {
180 return -99999d;
181 }
182 }
183 else {
184 return -99999d;
185 }
186 }
187
188 /** Implementation for getting the maximum value of location or distance
189 * dc:toValue.
190 */
191 public Object toValue(List args) throws XPathFunctionException {
192 Object mode = args.get(0);
193 Object locations = args.get(1);
194 Object to = args.get(2);
195
196 if (!(mode instanceof String)){
197 return 99999d;
198 }
199
200 if (mode.equals("locations")) {
201 if (!(locations instanceof String)) {
202 return 99999d;
203 }
204 try {
205 String loc = ((String)locations).replace(" ", "");
206 String[] split = loc.split(",");
207 if (split.length < 1) {
208 return 99999d;
209 }
210 double max = Double.parseDouble(split[0]);
211 for (int i = 1; i < split.length; ++i) {
212 double v = Double.parseDouble(split[i]);
213 if (v > max) {
214 max = v;
215 }
216 }
217 return max;
218 }
219 catch (NumberFormatException nfe) {
220 return 99999d;
221 }
222 }
223 else if (mode.equals("distance")) {
224 if (!(to instanceof String)) {
225 return 99999d;
226 }
227 else {
228 String t = (String)to;
229 try {
230 return Double.parseDouble(t);
231 }
232 catch(NumberFormatException nfe) {
233 return 99999d;
234 }
235 }
236 }
237 else {
238 return 99999d;
239 }
240 }
232 } 241 }
233 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 242 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org