[PATCH] Diagram attributes: Use evaluators for title arguments, too. This makes it more consistent, powerful and a little bit faster
Wald Commits
scm-commit at wald.intevation.org
Thu Sep 26 12:56:40 CEST 2013
# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1380192997 -7200
# Node ID f2ffa631c2ed0caab4640bdee9626bb2145b918d
# Parent 1508ee33f85f14ed595af5c27db723470c38e452
Diagram attributes: Use evaluators for title arguments, too. This makes it more consistent, powerful and a little bit faster.
diff -r 1508ee33f85f -r f2ffa631c2ed artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java Thu Sep 26 11:41:02 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java Thu Sep 26 12:56:37 2013 +0200
@@ -110,9 +110,9 @@
public static class AxisAttributes {
private String name;
- private boolean isLeftAlign;
- private boolean forceAlign;
- private boolean includeZero;
+ private boolean isLeftAlign; // TODO: Remove!
+ private boolean forceAlign; // TODO: Remove!
+ private boolean includeZero; // TODO: Use Evaluator
private Evaluator isInverted;
@@ -205,54 +205,95 @@
} // class AxisProcessor
- public static class Argument {
- private String expression;
- private String type;
+ public abstract static class ConvertEvaluator
+ implements Evaluator
+ {
+ protected String key;
+ protected String type;
- public Argument() {
+ public ConvertEvaluator() {
}
- public Argument(String expression, String type) {
- this.expression = expression;
+ public ConvertEvaluator(String key, String type) {
+ this.key = key;
this.type = type;
}
- public Object evaluate(D4EArtifact artifact, CallContext context) {
- if (expression.startsWith("artifact.")) {
- String value = artifact.getDataAsString(
- expression.substring("artifact.".length()));
- return convert(value);
+ protected Object convert(Object value) {
+ if (value == null || type == null || type.isEmpty()) {
+ return value;
}
- if (expression.startsWith("context.")) {
- return context.getContextValue(
- expression.substring("context.".length()));
+ if (value instanceof String) {
+ String v = (String)value;
+ if ("double".equals(type)) {
+ return Double.valueOf(v);
+ }
+ if ("int".equals(type)) {
+ return Integer.valueOf(v);
+ }
+ if ("string".equals(type)) {
+ return v;
+ }
}
- return expression;
+ // TODO: Support more types
+ return value;
+ }
+ } // class ConvertEvaluator
+
+ public static class ContextEvaluator extends ConvertEvaluator {
+
+ public ContextEvaluator() {
}
- private Object convert(String value) {
- if (value == null || type == null) {
- return value;
- }
- if ("double".equals(type)) {
- return Double.valueOf(value);
- }
- if ("int".equals(type)) {
- return Integer.valueOf(value);
- }
- // TODO: more types
+ public ContextEvaluator(String key, String type) {
+ super(key, type);
+ }
+
+ @Override
+ public Object evaluate(D4EArtifact artifact, CallContext context) {
+ return convert(context.getContextValue(key));
+ }
+ } // class ContextEvaluator
+
+ public static class ArtifactEvaluator extends ConvertEvaluator {
+
+ public ArtifactEvaluator() {
+ }
+
+ public ArtifactEvaluator(String key, String type) {
+ super(key, type);
+ }
+
+ @Override
+ public Object evaluate(D4EArtifact artifact, CallContext context) {
+ return convert(artifact.getDataAsString(key));
+ }
+ } // class ContextEvaluator
+
+ public static class StringEvaluator implements Evaluator {
+
+ private String value;
+
+ public StringEvaluator() {
+ }
+ public StringEvaluator(String value) {
+ this.value = value;
+ }
+
+ @Override
+ public Object evaluate(D4EArtifact artifact, CallContext context) {
return value;
}
- } // class Argument
+ } // class StringEvaluator
public static class Title {
private String key;
private String def;
- private List<Argument> arguments;
+ private List<Evaluator> arguments;
public Title() {
- arguments = new ArrayList<Argument>(5);
+ arguments = new ArrayList<Evaluator>(5);
}
public Title(String key) {
@@ -269,7 +310,7 @@
return key;
}
- public void addArgument(Argument argument) {
+ public void addArgument(Evaluator argument) {
arguments.add(argument);
}
@@ -345,25 +386,34 @@
}
}
- private Evaluator parseEvaluator(String s, Evaluator def) {
+ private static Evaluator createDynamicEvaluator(
+ String className,
+ Evaluator def
+ ) {
+ try {
+ Class<Evaluator> clazz = (Class<Evaluator>)Class.forName(className);
+ return clazz.newInstance();
+ }
+ catch (ClassNotFoundException cnfe) {
+ log.error(cnfe, cnfe);
+ }
+ catch (InstantiationException ie) {
+ log.error(ie, ie);
+ }
+ catch (IllegalAccessException iae) {
+ log.error(iae, iae);
+ }
+ return def;
+ }
+
+ private static Evaluator parseEvaluator(String s, Evaluator def) {
if ((s = s.trim()).isEmpty()) return def;
if ("true".equals(s)) return TRUE;
if ("false".equals(s)) return FALSE;
if (s.endsWith("()")) {
- s = s.substring(0, s.length()-2).trim();
- try {
- Class<Evaluator> clazz = (Class<Evaluator>)Class.forName(s);
- return clazz.newInstance();
- }
- catch (ClassNotFoundException cnfe) {
- log.error(cnfe, cnfe);
- }
- catch (InstantiationException ie) {
- log.error(ie, ie);
- }
- catch (IllegalAccessException iae) {
- log.error(iae, iae);
- }
+ return createDynamicEvaluator(
+ s.substring(0, s.length()-2).trim(),
+ def);
}
return def;
}
@@ -423,7 +473,7 @@
"X",
false,
false,
- false,
+ includeZero.equals("true"),
parseEvaluator(isInverted, FALSE),
title);
}
@@ -442,7 +492,24 @@
Element argumentElement = (Element)argumentNodes.item(i);
String expression = argumentElement.getAttribute("expr");
String type = argumentElement.getAttribute("type");
- title.addArgument(new Argument(expression, type));
+
+ Evaluator ev = new StringEvaluator(expression);
+
+ if (expression.endsWith("()")) {
+ ev = createDynamicEvaluator(
+ expression.substring(0, expression.length()-2).trim(),
+ ev);
+ }
+ else if (expression.startsWith("artifact.")) {
+ ev = new ArtifactEvaluator(
+ expression.substring("artifact.".length()), type);
+ }
+ else if (expression.startsWith("context.")) {
+ ev = new ContextEvaluator(
+ expression.substring("context.".length()), type);
+ }
+
+ title.addArgument(ev);
}
return title;
}
More information about the Dive4elements-commits
mailing list