changeset 7099:24f153ad1f40 generator-refactoring

Use pre-converted diagram attributes now.
author Sascha L. Teichmann <teichmann@intevation.de>
date Mon, 23 Sep 2013 12:28:22 +0200 (2013-09-23)
parents 7dc31a235232
children c15f251772bb
files artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java
diffstat 4 files changed, 194 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java	Mon Sep 23 12:21:59 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContext.java	Mon Sep 23 12:28:22 2013 +0200
@@ -13,7 +13,6 @@
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
-import org.w3c.dom.Element;
 
 import org.dive4elements.artifactdatabase.DefaultArtifactContext;
 import org.dive4elements.artifacts.CallContext;
@@ -101,15 +100,15 @@
             ? (RiverContext) context
             : (RiverContext) context.globalContext();
 
-        Map<String, Pair<Class<OutGenerator>, Element>> generators =
-            (Map<String, Pair<Class<OutGenerator>, Element>>)flysContext
+        Map<String, Pair<Class<OutGenerator>, Object>> generators =
+            (Map<String, Pair<Class<OutGenerator>, Object>>)flysContext
                 .get(RiverContext.OUTGENERATORS_KEY);
 
         if (generators == null) {
             return null;
         }
 
-        Pair<Class<OutGenerator>, Element> pair = generators.get(name);
+        Pair<Class<OutGenerator>, Object> pair = generators.get(name);
 
         if (pair == null) {
             logger.warn("No generator class found for " + name);
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Mon Sep 23 12:21:59 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/context/RiverContextFactory.java	Mon Sep 23 12:28:22 2013 +0200
@@ -8,26 +8,8 @@
 
 package org.dive4elements.river.artifacts.context;
 
-import org.dive4elements.artifactdatabase.state.State;
-import org.dive4elements.artifactdatabase.state.StateEngine;
-import org.dive4elements.artifactdatabase.transition.Transition;
-import org.dive4elements.artifactdatabase.transition.TransitionEngine;
-import org.dive4elements.artifacts.ArtifactContextFactory;
-import org.dive4elements.artifacts.GlobalContext;
-import org.dive4elements.artifacts.common.utils.Config;
-import org.dive4elements.artifacts.common.utils.XMLUtils;
-import org.dive4elements.river.artifacts.model.Module;
-import org.dive4elements.river.artifacts.model.ZoomScale;
-import org.dive4elements.river.artifacts.states.StateFactory;
-import org.dive4elements.river.artifacts.transitions.TransitionFactory;
-import org.dive4elements.river.themes.Theme;
-import org.dive4elements.river.themes.ThemeFactory;
-import org.dive4elements.river.themes.ThemeGroup;
-import org.dive4elements.river.themes.ThemeMapping;
-import org.dive4elements.river.utils.Pair;
-import org.dive4elements.river.exports.OutGenerator;
+import java.io.File;
 
-import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -36,12 +18,41 @@
 import javax.xml.xpath.XPathConstants;
 
 import org.apache.log4j.Logger;
+
+import org.dive4elements.artifactdatabase.state.State;
+import org.dive4elements.artifactdatabase.state.StateEngine;
+
+import org.dive4elements.artifactdatabase.transition.Transition;
+import org.dive4elements.artifactdatabase.transition.TransitionEngine;
+
+import org.dive4elements.artifacts.ArtifactContextFactory;
+import org.dive4elements.artifacts.GlobalContext;
+
+import org.dive4elements.artifacts.common.utils.Config;
+import org.dive4elements.artifacts.common.utils.ElementConverter;
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+
+import org.dive4elements.river.artifacts.model.Module;
+import org.dive4elements.river.artifacts.model.ZoomScale;
+
+import org.dive4elements.river.artifacts.states.StateFactory;
+
+import org.dive4elements.river.artifacts.transitions.TransitionFactory;
+
+import org.dive4elements.river.exports.OutGenerator;
+
+import org.dive4elements.river.themes.Theme;
+import org.dive4elements.river.themes.ThemeFactory;
+import org.dive4elements.river.themes.ThemeGroup;
+import org.dive4elements.river.themes.ThemeMapping;
+
+import org.dive4elements.river.utils.Pair;
+
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
-
 /**
  * The ArtifactContextFactory is used to initialize basic components and put
  * them into the global context of the application.
@@ -324,8 +335,8 @@
 
         logger.info("Found " + num + " configured output generators.");
 
-        Map<String, Pair<Class<OutGenerator>, Element>> generators =
-            new HashMap<String, Pair<Class<OutGenerator>, Element>>();
+        Map<String, Pair<Class<OutGenerator>, Object>> generators =
+            new HashMap<String, Pair<Class<OutGenerator>, Object>>();
 
         int idx = 0;
 
@@ -334,6 +345,7 @@
 
             String names = item.getAttribute("names").trim();
             String clazz = item.getAttribute("class").trim();
+            String converter = item.getAttribute("converter").trim();
 
             if (names.isEmpty() || clazz.isEmpty()) {
                 continue;
@@ -345,12 +357,32 @@
                 generatorClass = (Class<OutGenerator>)Class.forName(clazz);
             }
             catch (ClassNotFoundException cnfe) {
-                logger.warn(cnfe, cnfe);
+                logger.error(cnfe, cnfe);
                 continue;
             }
 
-            Pair<Class<OutGenerator>, Element> pair =
-                new Pair<Class<OutGenerator>, Element>(generatorClass, item);
+            Object cfg = null;
+
+            if (!converter.isEmpty()) {
+                try {
+                    ElementConverter ec =
+                        (ElementConverter)Class.forName(converter)
+                            .newInstance();
+                    cfg = ec.convert(item);
+                }
+                catch (ClassNotFoundException cnfe) {
+                    logger.error(cnfe, cnfe);
+                }
+                catch (InstantiationException ie) {
+                    logger.error(ie);
+                }
+                catch (IllegalAccessException iae) {
+                    logger.error(iae);
+                }
+            }
+
+            Pair<Class<OutGenerator>, Object> pair =
+                new Pair<Class<OutGenerator>, Object>(generatorClass, cfg);
 
             for (String key: names.split("[\\s,]")) {
                 if (!(key = key.trim()).isEmpty()) {
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Mon Sep 23 12:21:59 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Mon Sep 23 12:28:22 2013 +0200
@@ -23,10 +23,72 @@
 import org.dive4elements.river.artifacts.resources.Resources;
 import org.dive4elements.river.artifacts.D4EArtifact;
 
+import org.dive4elements.artifacts.common.utils.ElementConverter;
+
 public class DiagramAttributes
+implements   ElementConverter
 {
     private static Logger log = Logger.getLogger(DiagramAttributes.class);
 
+    public class Instance {
+
+        private List<Processor> processors;
+
+        public Instance() {
+            processors = createProcessors();
+        }
+
+        private List<Processor> createProcessors() {
+            List<Processor> processors =
+                new ArrayList<Processor>(axesProcessors.size());
+            for (AxisProcessor ap: axesProcessors) {
+                Processor pr = ap.createProcessor();
+                if (pr != null) {
+                    processors.add(pr);
+                }
+            }
+            return processors;
+        }
+
+        public Processor getProcessorForAxisName(String axisName) {
+            for (Processor pr: processors) {
+                String aName = pr.getAxisName();
+                if (aName != null && axisName.equals(aName)) {
+                    return pr;
+                }
+            }
+            return null;
+        }
+
+        public List<Processor> getProcessors() {
+            return processors;
+        }
+
+        public Title getTitle() {
+            return DiagramAttributes.this.getTitle();
+        }
+
+        public Title getSubtitle() {
+            return DiagramAttributes.this.getSubtitle();
+        }
+
+        public Title getDomainAxisTitle() {
+            return DiagramAttributes.this.getDomainAxisTitle();
+        }
+
+        public int getAxisIndex(String axisName) {
+            return DiagramAttributes.this.getAxisIndex(axisName);
+        }
+
+        public String getAxisName(int index) {
+            return DiagramAttributes.this.getAxisName(index);
+        }
+
+        public List<AxisAttributes> getAxesAttributes() {
+            return DiagramAttributes.this.getAxesAttributes();
+        }
+    } // class Instance
+
     public static class AxisAttributes {
         private String  name;
         private boolean isLeftAlign;
@@ -65,6 +127,33 @@
         }
     } // class AxisAttributes
 
+    public static class AxisProcessor {
+
+        private Class<Processor> processorClass;
+        private String axisName;
+
+        public AxisProcessor(Class<Processor> processorClass, String axisName) {
+            this.processorClass = processorClass;
+            this.axisName = axisName;
+        }
+
+        public Processor createProcessor() {
+            try {
+                Processor pr = processorClass.newInstance();
+                pr.setAxisName(axisName);
+                return pr;
+            }
+            catch (InstantiationException ie) {
+                log.error(ie, ie);
+            }
+            catch (IllegalAccessException iae) {
+                log.error(iae, iae);
+            }
+            return null;
+        }
+
+    } // class AxisProcessor
+
     public static class Argument {
         private String expression;
         private String type;
@@ -146,7 +235,7 @@
     } // class Title
 
     private List<AxisAttributes> axesAttrs;
-    private List<Processor> processors;
+    private List<AxisProcessor>  axesProcessors;
 
     private Title title;
     private Title subtitle;
@@ -154,16 +243,17 @@
 
     public DiagramAttributes() {
         axesAttrs      = new ArrayList<AxisAttributes>(5);
-        processors     = new ArrayList<Processor>(5);
+        axesProcessors = new ArrayList<AxisProcessor>(5);
     }
 
-    public DiagramAttributes(Element config) {
-        this();
+    @Override
+    public Object convert(Element config) {
         parseAxis(config);
         parseProcessors(config);
         parseTitle(config);
         parseSubtitle(config);
         parseDomainAxisTitle(config);
+        return this;
     }
 
     public List<AxisAttributes> getAxesAttributes() {
@@ -197,8 +287,8 @@
         }
     }
 
-    public List<Processor> getProcessors() {
-        return processors;
+    public List<AxisProcessor> getAxesProcessors() {
+        return axesProcessors;
     }
 
     public Title getTitle() {
@@ -225,23 +315,13 @@
             }
 
             try {
-                Processor processor =
-                    (Processor)Class.forName(className).newInstance();
-                processor.setAxisName(axisName);
-                processors.add(processor);
+                Class<Processor> processorClass =
+                    (Class<Processor>)Class.forName(className);
+                axesProcessors.add(new AxisProcessor(processorClass, axisName));
             }
             catch (ClassNotFoundException cnfe) {
                 log.error(cnfe, cnfe);
             }
-            catch (InstantiationException ie) {
-                log.error(ie, ie);
-            }
-            catch (IllegalAccessException ia) {
-                log.error(ia, ia);
-            }
-            catch (ClassCastException cce) {
-                log.error(cce, cce);
-            }
         }
     }
 
@@ -285,21 +365,10 @@
         return -1;
     }
 
-    public Processor getProcessorForAxisName(String axisName) {
-        for (Processor pr: processors) {
-            if (pr.getAxisName().equals(axisName)) {
-                return pr;
-            }
-        }
-        return null;
-    }
-
     public String getAxisName(int index) {
-        AxisAttributes att = axesAttrs.get(index);
-        if (att == null) {
-            return ""; /* null? */
-        }
-        return att.getName();
+        return index < 0 || index >= axesAttrs.size()
+            ? "" // null?
+            : axesAttrs.get(index).getName();
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Mon Sep 23 12:21:59 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramGenerator.java	Mon Sep 23 12:28:22 2013 +0200
@@ -10,7 +10,9 @@
 
 import java.awt.Color;
 import java.awt.Font;
+
 import java.text.NumberFormat;
+
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -19,39 +21,45 @@
 import javax.swing.ImageIcon;
 
 import org.apache.log4j.Logger;
+
+import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
+import org.dive4elements.artifactdatabase.state.Facet;
+
+import org.dive4elements.river.artifacts.D4EArtifact;
+
+import org.dive4elements.river.exports.process.Processor;
+
+import org.dive4elements.river.jfree.AnnotationHelper;
+import org.dive4elements.river.jfree.AxisDataset;
+import org.dive4elements.river.jfree.Bounds;
+import org.dive4elements.river.jfree.DoubleBounds;
+import org.dive4elements.river.jfree.StyledAreaSeriesCollection;
+
+import org.dive4elements.river.themes.ThemeDocument;
+
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.LegendItem;
+
 import org.jfree.chart.annotations.XYAnnotation;
 import org.jfree.chart.annotations.XYImageAnnotation;
-import org.jfree.chart.annotations.XYTextAnnotation;
+
+import org.jfree.chart.axis.LogarithmicAxis;
 import org.jfree.chart.axis.NumberAxis;
 import org.jfree.chart.axis.ValueAxis;
-import org.jfree.chart.axis.LogarithmicAxis;
+
 import org.jfree.chart.plot.Marker;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.plot.XYPlot;
+
 import org.jfree.data.Range;
+
 import org.jfree.data.general.Series;
+
 import org.jfree.data.xy.XYDataset;
 import org.jfree.data.xy.XYSeries;
 import org.jfree.data.xy.XYSeriesCollection;
 
-import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
-import org.dive4elements.artifactdatabase.state.Facet;
-import org.dive4elements.river.artifacts.D4EArtifact;
-import org.dive4elements.river.exports.process.Processor;
-import org.dive4elements.river.jfree.AxisDataset;
-import org.dive4elements.river.jfree.AnnotationHelper;
-import org.dive4elements.river.jfree.Bounds;
-import org.dive4elements.river.jfree.CollisionFreeXYTextAnnotation;
-import org.dive4elements.river.jfree.DoubleBounds;
-import org.dive4elements.river.jfree.RiverAnnotation;
-import org.dive4elements.river.jfree.StyledAreaSeriesCollection;
-import org.dive4elements.river.jfree.StyledXYSeries;
-import org.dive4elements.river.themes.ThemeDocument;
-
-import org.w3c.dom.Element;
 
 /**
  * The main diagram creation class.
@@ -90,7 +98,7 @@
     /** Whether or not the plot is inverted (left-right). */
     private boolean inverted;
 
-    protected DiagramAttributes diagramAttributes;
+    protected DiagramAttributes.Instance diagramAttributes;
 
     public DiagramGenerator() {
         super();
@@ -101,12 +109,13 @@
 
     @Override
     public void setup(Object config) {
-        Element cfg = (Element)config;
-        logger.debug("DiagramGenerator.setup");
-        // TODO: XML DOM is not thread safe! We have to re-factor this.
-        synchronized (cfg.getOwnerDocument()) {
-            diagramAttributes = new DiagramAttributes(cfg);
+
+        if (!(config instanceof DiagramAttributes)) {
+            logger.error("invalid config type");
+            return;
         }
+        DiagramAttributes da = (DiagramAttributes)config;
+        diagramAttributes = da.new Instance();
     }
 
     /**

http://dive4elements.wald.intevation.org