comparison artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java @ 7160:f2ffa631c2ed

Diagram attributes: Use evaluators for title arguments, too. This makes it more consistent, powerful and a little bit faster.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 26 Sep 2013 12:56:37 +0200
parents 053e39436ba3
children 0e1191e34b5f
comparison
equal deleted inserted replaced
7159:1508ee33f85f 7160:f2ffa631c2ed
108 } 108 }
109 } // class Instance 109 } // class Instance
110 110
111 public static class AxisAttributes { 111 public static class AxisAttributes {
112 private String name; 112 private String name;
113 private boolean isLeftAlign; 113 private boolean isLeftAlign; // TODO: Remove!
114 private boolean forceAlign; 114 private boolean forceAlign; // TODO: Remove!
115 private boolean includeZero; 115 private boolean includeZero; // TODO: Use Evaluator
116 116
117 private Evaluator isInverted; 117 private Evaluator isInverted;
118 118
119 public AxisAttributes() { 119 public AxisAttributes() {
120 } 120 }
203 return null; 203 return null;
204 } 204 }
205 205
206 } // class AxisProcessor 206 } // class AxisProcessor
207 207
208 public static class Argument { 208 public abstract static class ConvertEvaluator
209 private String expression; 209 implements Evaluator
210 private String type; 210 {
211 211 protected String key;
212 public Argument() { 212 protected String type;
213 } 213
214 214 public ConvertEvaluator() {
215 public Argument(String expression, String type) { 215 }
216 this.expression = expression; 216
217 public ConvertEvaluator(String key, String type) {
218 this.key = key;
217 this.type = type; 219 this.type = type;
218 } 220 }
219 221
222 protected Object convert(Object value) {
223 if (value == null || type == null || type.isEmpty()) {
224 return value;
225 }
226 if (value instanceof String) {
227 String v = (String)value;
228 if ("double".equals(type)) {
229 return Double.valueOf(v);
230 }
231 if ("int".equals(type)) {
232 return Integer.valueOf(v);
233 }
234 if ("string".equals(type)) {
235 return v;
236 }
237 }
238 // TODO: Support more types
239 return value;
240 }
241 } // class ConvertEvaluator
242
243 public static class ContextEvaluator extends ConvertEvaluator {
244
245 public ContextEvaluator() {
246 }
247
248 public ContextEvaluator(String key, String type) {
249 super(key, type);
250 }
251
252 @Override
220 public Object evaluate(D4EArtifact artifact, CallContext context) { 253 public Object evaluate(D4EArtifact artifact, CallContext context) {
221 if (expression.startsWith("artifact.")) { 254 return convert(context.getContextValue(key));
222 String value = artifact.getDataAsString( 255 }
223 expression.substring("artifact.".length())); 256 } // class ContextEvaluator
224 return convert(value); 257
225 } 258 public static class ArtifactEvaluator extends ConvertEvaluator {
226 if (expression.startsWith("context.")) { 259
227 return context.getContextValue( 260 public ArtifactEvaluator() {
228 expression.substring("context.".length())); 261 }
229 } 262
230 return expression; 263 public ArtifactEvaluator(String key, String type) {
231 } 264 super(key, type);
232 265 }
233 private Object convert(String value) { 266
234 if (value == null || type == null) { 267 @Override
235 return value; 268 public Object evaluate(D4EArtifact artifact, CallContext context) {
236 } 269 return convert(artifact.getDataAsString(key));
237 if ("double".equals(type)) { 270 }
238 return Double.valueOf(value); 271 } // class ContextEvaluator
239 } 272
240 if ("int".equals(type)) { 273 public static class StringEvaluator implements Evaluator {
241 return Integer.valueOf(value); 274
242 } 275 private String value;
243 // TODO: more types 276
277 public StringEvaluator() {
278 }
279 public StringEvaluator(String value) {
280 this.value = value;
281 }
282
283 @Override
284 public Object evaluate(D4EArtifact artifact, CallContext context) {
244 return value; 285 return value;
245 } 286 }
246 } // class Argument 287 } // class StringEvaluator
247 288
248 public static class Title { 289 public static class Title {
249 290
250 private String key; 291 private String key;
251 private String def; 292 private String def;
252 private List<Argument> arguments; 293 private List<Evaluator> arguments;
253 294
254 public Title() { 295 public Title() {
255 arguments = new ArrayList<Argument>(5); 296 arguments = new ArrayList<Evaluator>(5);
256 } 297 }
257 298
258 public Title(String key) { 299 public Title(String key) {
259 this(key, key); 300 this(key, key);
260 } 301 }
267 308
268 public String getKey() { 309 public String getKey() {
269 return key; 310 return key;
270 } 311 }
271 312
272 public void addArgument(Argument argument) { 313 public void addArgument(Evaluator argument) {
273 arguments.add(argument); 314 arguments.add(argument);
274 } 315 }
275 316
276 public String evaluate(D4EArtifact artifact, CallContext context) { 317 public String evaluate(D4EArtifact artifact, CallContext context) {
277 if (key == null || key.isEmpty()) { 318 if (key == null || key.isEmpty()) {
343 includeZero.equals("true"), 384 includeZero.equals("true"),
344 isInvertedE)); 385 isInvertedE));
345 } 386 }
346 } 387 }
347 388
348 private Evaluator parseEvaluator(String s, Evaluator def) { 389 private static Evaluator createDynamicEvaluator(
390 String className,
391 Evaluator def
392 ) {
393 try {
394 Class<Evaluator> clazz = (Class<Evaluator>)Class.forName(className);
395 return clazz.newInstance();
396 }
397 catch (ClassNotFoundException cnfe) {
398 log.error(cnfe, cnfe);
399 }
400 catch (InstantiationException ie) {
401 log.error(ie, ie);
402 }
403 catch (IllegalAccessException iae) {
404 log.error(iae, iae);
405 }
406 return def;
407 }
408
409 private static Evaluator parseEvaluator(String s, Evaluator def) {
349 if ((s = s.trim()).isEmpty()) return def; 410 if ((s = s.trim()).isEmpty()) return def;
350 if ("true".equals(s)) return TRUE; 411 if ("true".equals(s)) return TRUE;
351 if ("false".equals(s)) return FALSE; 412 if ("false".equals(s)) return FALSE;
352 if (s.endsWith("()")) { 413 if (s.endsWith("()")) {
353 s = s.substring(0, s.length()-2).trim(); 414 return createDynamicEvaluator(
354 try { 415 s.substring(0, s.length()-2).trim(),
355 Class<Evaluator> clazz = (Class<Evaluator>)Class.forName(s); 416 def);
356 return clazz.newInstance();
357 }
358 catch (ClassNotFoundException cnfe) {
359 log.error(cnfe, cnfe);
360 }
361 catch (InstantiationException ie) {
362 log.error(ie, ie);
363 }
364 catch (IllegalAccessException iae) {
365 log.error(iae, iae);
366 }
367 } 417 }
368 return def; 418 return def;
369 } 419 }
370 420
371 public List<AxisProcessor> getAxesProcessors() { 421 public List<AxisProcessor> getAxesProcessors() {
421 471
422 domainAxis = new DomainAxisAttributes( 472 domainAxis = new DomainAxisAttributes(
423 "X", 473 "X",
424 false, 474 false,
425 false, 475 false,
426 false, 476 includeZero.equals("true"),
427 parseEvaluator(isInverted, FALSE), 477 parseEvaluator(isInverted, FALSE),
428 title); 478 title);
429 } 479 }
430 480
431 private static Title extractTitle(Element config, String tagName) { 481 private static Title extractTitle(Element config, String tagName) {
440 NodeList argumentNodes = titleElement.getElementsByTagName("arg"); 490 NodeList argumentNodes = titleElement.getElementsByTagName("arg");
441 for (int i = 0, N = argumentNodes.getLength(); i < N; ++i) { 491 for (int i = 0, N = argumentNodes.getLength(); i < N; ++i) {
442 Element argumentElement = (Element)argumentNodes.item(i); 492 Element argumentElement = (Element)argumentNodes.item(i);
443 String expression = argumentElement.getAttribute("expr"); 493 String expression = argumentElement.getAttribute("expr");
444 String type = argumentElement.getAttribute("type"); 494 String type = argumentElement.getAttribute("type");
445 title.addArgument(new Argument(expression, type)); 495
496 Evaluator ev = new StringEvaluator(expression);
497
498 if (expression.endsWith("()")) {
499 ev = createDynamicEvaluator(
500 expression.substring(0, expression.length()-2).trim(),
501 ev);
502 }
503 else if (expression.startsWith("artifact.")) {
504 ev = new ArtifactEvaluator(
505 expression.substring("artifact.".length()), type);
506 }
507 else if (expression.startsWith("context.")) {
508 ev = new ContextEvaluator(
509 expression.substring("context.".length()), type);
510 }
511
512 title.addArgument(ev);
446 } 513 }
447 return title; 514 return title;
448 } 515 }
449 516
450 public int getAxisIndex(String axisName) { 517 public int getAxisIndex(String axisName) {

http://dive4elements.wald.intevation.org