Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/FunctionResolver.java @ 5604:7e837c5c377b
Datacage: toValue/fromValue: Use constant instead of hard coded number.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Tue, 09 Apr 2013 08:28:07 +0200 |
parents | f8a4ee86d276 |
children | 62513c9183ba |
comparison
equal
deleted
inserted
replaced
5603:47e5bc64d3de | 5604:7e837c5c377b |
---|---|
21 /** Home logger. */ | 21 /** Home logger. */ |
22 private static Logger log = Logger.getLogger(FunctionResolver.class); | 22 private static Logger log = Logger.getLogger(FunctionResolver.class); |
23 | 23 |
24 public static final String FUNCTION_NAMESPACE_URI = "dc"; | 24 public static final String FUNCTION_NAMESPACE_URI = "dc"; |
25 | 25 |
26 public static final double FAR_AWAY = 99999d; | |
27 | |
26 public static final class Entry { | 28 public static final class Entry { |
27 | 29 |
28 String name; | 30 String name; |
29 XPathFunction function; | 31 XPathFunction function; |
30 int arity; | 32 int arity; |
160 Object mode = args.get(0); | 162 Object mode = args.get(0); |
161 Object locations = args.get(1); | 163 Object locations = args.get(1); |
162 Object from = args.get(2); | 164 Object from = args.get(2); |
163 | 165 |
164 if (!(mode instanceof String)){ | 166 if (!(mode instanceof String)){ |
165 return -99999d; | 167 return -FAR_AWAY; |
166 } | 168 } |
167 | 169 |
168 if (mode.equals("locations")) { | 170 if (mode.equals("locations")) { |
169 if (!(locations instanceof String)) { | 171 if (!(locations instanceof String)) { |
170 return -99999d; | 172 return -FAR_AWAY; |
171 } | 173 } |
172 String loc = ((String)locations).replace(" ", ""); | 174 String loc = ((String)locations).replace(" ", ""); |
173 String[] split = loc.split(","); | 175 String[] split = loc.split(","); |
174 if (split.length < 1) { | 176 if (split.length < 1) { |
175 return -99999d; | 177 return -FAR_AWAY; |
176 } | 178 } |
177 try { | 179 try { |
178 double min = Double.parseDouble(split[0]); | 180 double min = Double.parseDouble(split[0]); |
179 for (int i = 1; i < split.length; ++i) { | 181 for (int i = 1; i < split.length; ++i) { |
180 double v = Double.parseDouble(split[i]); | 182 double v = Double.parseDouble(split[i]); |
183 } | 185 } |
184 } | 186 } |
185 return min; | 187 return min; |
186 } | 188 } |
187 catch (NumberFormatException nfe) { | 189 catch (NumberFormatException nfe) { |
188 return -99999d; | 190 return -FAR_AWAY; |
189 } | 191 } |
190 } | 192 } |
191 else if (mode.equals("distance")) { | 193 else if (mode.equals("distance")) { |
192 if (!(from instanceof String)) { | 194 if (!(from instanceof String)) { |
193 return -99999d; | 195 return -FAR_AWAY; |
194 } | 196 } |
195 String f = (String)from; | 197 String f = (String)from; |
196 try { | 198 try { |
197 return Double.parseDouble(f); | 199 return Double.parseDouble(f); |
198 } | 200 } |
199 catch(NumberFormatException nfe) { | 201 catch(NumberFormatException nfe) { |
200 return -99999d; | 202 return -FAR_AWAY; |
201 } | 203 } |
202 } | 204 } |
203 else { | 205 else { |
204 return -99999d; | 206 return -FAR_AWAY; |
205 } | 207 } |
206 } | 208 } |
207 | 209 |
208 /** Implementation for getting the maximum value of location or distance | 210 /** Implementation for getting the maximum value of location or distance |
209 * dc:toValue. | 211 * dc:toValue. |
212 Object mode = args.get(0); | 214 Object mode = args.get(0); |
213 Object locations = args.get(1); | 215 Object locations = args.get(1); |
214 Object to = args.get(2); | 216 Object to = args.get(2); |
215 | 217 |
216 if (!(mode instanceof String)){ | 218 if (!(mode instanceof String)){ |
217 return 99999d; | 219 return FAR_AWAY; |
218 } | 220 } |
219 | 221 |
220 if (mode.equals("locations")) { | 222 if (mode.equals("locations")) { |
221 if (!(locations instanceof String)) { | 223 if (!(locations instanceof String)) { |
222 return 99999d; | 224 return FAR_AWAY; |
223 } | 225 } |
224 try { | 226 try { |
225 String loc = ((String)locations).replace(" ", ""); | 227 String loc = ((String)locations).replace(" ", ""); |
226 String[] split = loc.split(","); | 228 String[] split = loc.split(","); |
227 if (split.length < 1) { | 229 if (split.length < 1) { |
228 return 99999d; | 230 return FAR_AWAY; |
229 } | 231 } |
230 double max = Double.parseDouble(split[0]); | 232 double max = Double.parseDouble(split[0]); |
231 for (int i = 1; i < split.length; ++i) { | 233 for (int i = 1; i < split.length; ++i) { |
232 double v = Double.parseDouble(split[i]); | 234 double v = Double.parseDouble(split[i]); |
233 if (v > max) { | 235 if (v > max) { |
235 } | 237 } |
236 } | 238 } |
237 return max; | 239 return max; |
238 } | 240 } |
239 catch (NumberFormatException nfe) { | 241 catch (NumberFormatException nfe) { |
240 return 99999d; | 242 return FAR_AWAY; |
241 } | 243 } |
242 } | 244 } |
243 else if (mode.equals("distance")) { | 245 else if (mode.equals("distance")) { |
244 if (!(to instanceof String)) { | 246 if (!(to instanceof String)) { |
245 return 99999d; | 247 return FAR_AWAY; |
246 } | 248 } |
247 else { | 249 else { |
248 String t = (String)to; | 250 String t = (String)to; |
249 try { | 251 try { |
250 return Double.parseDouble(t); | 252 return Double.parseDouble(t); |
251 } | 253 } |
252 catch(NumberFormatException nfe) { | 254 catch(NumberFormatException nfe) { |
253 return 99999d; | 255 return FAR_AWAY; |
254 } | 256 } |
255 } | 257 } |
256 } | 258 } |
257 else { | 259 else { |
258 return 99999d; | 260 return FAR_AWAY; |
259 } | 261 } |
260 } | 262 } |
261 | 263 |
262 /** Implementation for doing a string replace | 264 /** Implementation for doing a string replace |
263 * dc:replace | 265 * dc:replace |