diff artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java @ 8892:87a242425467

Introduced ChartExtender that allows to tweak the charts for very specific use cases.
author gernotbelger
date Thu, 15 Feb 2018 13:46:35 +0100
parents f87f435df856
children 55e2155ab52d
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Wed Feb 14 19:06:21 2018 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/DiagramAttributes.java	Thu Feb 15 13:46:35 2018 +0100
@@ -9,6 +9,8 @@
 package org.dive4elements.river.exports;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 import org.w3c.dom.Element;
@@ -82,6 +84,10 @@
         public List<Processor> getProcessors() {
             return processors;
         }
+        
+        public Collection<ChartExtender> getExtenders() {
+            return DiagramAttributes.this.getExtenders();
+        }
 
         public Title getTitle() {
             return DiagramAttributes.this.getTitle();
@@ -345,6 +351,7 @@
 
     private List<AxisAttributes> axesAttrs;
     private List<AxisProcessor>  axesProcessors;
+    private List<ChartExtender> extenders = new ArrayList<>();
 
     private Title title;
     private Title subtitle;
@@ -363,6 +370,7 @@
         parseTitle(config);
         parseSubtitle(config);
         parseDomainAxis(config);
+        parseExtenders(config);
         return this;
     }
 
@@ -459,6 +467,10 @@
     public List<AxisProcessor> getAxesProcessors() {
         return axesProcessors;
     }
+    
+    public Collection<ChartExtender> getExtenders() {
+        return Collections.unmodifiableCollection(extenders);
+    }
 
     public Title getTitle() {
         return title;
@@ -493,6 +505,33 @@
             }
         }
     }
+    
+    private void parseExtenders(final Element config) {
+        final NodeList processorNodes = config.getElementsByTagName("chartextender");
+
+        for (int i = 0, N = processorNodes.getLength(); i < N; ++i) {
+            final Element extenderElement = (Element)processorNodes.item(i);
+            final String className = extenderElement.getAttribute("class").trim();
+            if (className.isEmpty() ) {
+                log.error("chartextender missing 'class' attribute");
+                continue;
+            }
+
+            try {
+                final Class<?> protoclass = Class.forName(className);
+                if( !ChartExtender.class.isAssignableFrom(protoclass) ) {
+                    log.error(String.format( "Chart extender must implement interface ChartExtender: %s", className) );
+                    continue;
+                }
+                
+                final ChartExtender extender = (ChartExtender) protoclass.newInstance();
+                extenders.add(extender);
+            }
+            catch (ClassNotFoundException | InstantiationException | IllegalAccessException cnfe) {
+                log.error(String.format( "Failed to load or create chartextender class: %s", className ), cnfe);
+            }
+        }
+    }
 
     private void parseTitle(Element config) {
         title = extractTitle(config, "title");

http://dive4elements.wald.intevation.org