[PATCH] Simple interpolation logic for diagram titles and subtitles

Wald Commits scm-commit at wald.intevation.org
Thu Sep 19 10:57:52 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1379581067 -7200
# Branch generator-refactoring
# Node ID a43137dfdcac931d7de6cc74273b1b409d76ac59
# Parent  6ab1464021ae6694911396dbb5859023506a1d11
Simple interpolation logic for diagram titles and subtitles.

diff -r 6ab1464021ae -r a43137dfdcac artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Wed Sep 18 17:13:17 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Thu Sep 19 10:57:47 2013 +0200
@@ -18,11 +18,16 @@
 
 import org.apache.log4j.Logger;
 
+import org.dive4elements.artifacts.CallContext;
+
+import org.dive4elements.river.artifacts.resources.Resources;
+import org.dive4elements.river.artifacts.D4EArtifact;
+
 public class DiagramAttributes
 {
     private static Logger log = Logger.getLogger(DiagramAttributes.class);
 
-    public class AxisAttributes {
+    public static class AxisAttributes {
         // TODO: More Attributes
         private String name;
 
@@ -38,7 +43,7 @@
         }
     } // class AxisAttributes
 
-    public class AxisProcessor {
+    public static class AxisProcessor {
         private Processor processor;
         private String axisName;
 
@@ -59,9 +64,92 @@
         }
     } // class AxisProcessor
 
+    public static class Argument {
+        private String expression;
+        private String type;
+
+        public Argument() {
+        }
+
+        public Argument(String expression, String type) {
+            this.expression = expression;
+            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);
+            }
+            if (expression.startsWith("context.")) {
+                return context.getContextValue(
+                    expression.substring("context.".length()));
+            }
+            return expression;
+        }
+
+        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
+            return value;
+        }
+    } // class Argument
+
+    public static class Title {
+
+        private String key;
+        private String def;
+        private List<Argument> arguments;
+
+        public Title() {
+            arguments = new ArrayList<Argument>(5);
+        }
+
+        public Title(String key) {
+            this(key, key);
+        }
+
+        public Title(String key, String def) {
+            this();
+            this.key = key;
+            this.def = def;
+        }
+
+        public String getKey() {
+            return key;
+        }
+
+        public void addArgument(Argument argument) {
+            arguments.add(argument);
+        }
+
+        public String evalute(D4EArtifact artifact, CallContext context) {
+            if (key == null || key.isEmpty()) {
+                return def;
+            }
+            Object [] args = new Object[arguments.size()];
+            for (int i = 0; i < args.length; ++i) {
+                args[i] = arguments.get(i).evaluate(artifact, context);
+            }
+            return Resources.getMsg(context.getMeta(), key, def, args);
+        }
+    } // class Title
+
     private List<AxisAttributes> axesAttrs;
     private List<AxisProcessor> axesProcessors;
 
+    private Title title;
+    private Title subtitle;
+
     public DiagramAttributes() {
         axesAttrs      = new ArrayList<AxisAttributes>(5);
         axesProcessors = new ArrayList<AxisProcessor>(5);
@@ -71,6 +159,7 @@
         this();
         parseAxis(config);
         parseProcessors(config);
+        parseTitle(config);
     }
 
     private void parseAxis(Element config) {
@@ -89,6 +178,14 @@
         return axesProcessors;
     }
 
+    public Title getTitle() {
+        return title;
+    }
+
+    public Title getSubtitle() {
+        return subtitle;
+    }
+
     private void parseProcessors(Element config) {
         NodeList processorNodes = config.getElementsByTagName("processor");
 
@@ -119,5 +216,32 @@
             }
         }
     }
+
+    private void parseTitle(Element config) {
+        title = extractTitle(config, "title");
+    }
+
+    private void parseSubtitle(Element config) {
+        subtitle = extractTitle(config, "subtitle");
+    }
+
+    private static Title extractTitle(Element config, String tagName) {
+        NodeList titleNodes = config.getElementsByTagName(tagName);
+        if (titleNodes.getLength() < 1) {
+            return null;
+        }
+        Element titleElement = (Element)titleNodes.item(0);
+        String key = titleElement.getAttribute("key");
+        String def = titleElement.getAttribute("default");
+        Title title = new Title(key, def);
+        NodeList argumentNodes = titleElement.getElementsByTagName("arg");
+        for (int i = 0, N = argumentNodes.getLength(); i < N; ++i) {
+            Element argumentElement = (Element)argumentNodes.item(i);
+            String expression = argumentElement.getAttribute("expr");
+            String type = argumentElement.getAttribute("type");
+            title.addArgument(new Argument(expression, type));
+        }
+        return title;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list