changeset 426:3a0c0ad113d9

Load palettes from configuration. gnv-artifacts/trunk@474 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 22 Dec 2009 10:34:15 +0000
parents 15b8e95fa8da
children a95aaeb206f7
files gnv-artifacts/ChangeLog gnv-artifacts/doc/conf/conf.xml gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java
diffstat 4 files changed, 167 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Mon Dec 21 19:58:17 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Tue Dec 22 10:34:15 2009 +0000
@@ -1,3 +1,17 @@
+2009-12-22	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* doc/conf/conf.xml: Added config section for palettes.
+
+	* src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java:
+	  load palettes at start up.
+	  They are store in a hash map under the key 'color.palettes'. The keys of
+	  the maps are the names from the configuration, the values are 
+	  de.intevation.gnv.raster.Palette objects.
+	  Put configuration of chart template into own method.
+
+	* src/main/java/de/intevation/gnv/raster/Palette.java: Added a palette
+	  description.
+
 2009-12-21	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/gnv/state/profile/horizontal/HorizontalProfileMeshCrossOutputState.java:
--- a/gnv-artifacts/doc/conf/conf.xml	Mon Dec 21 19:58:17 2009 +0000
+++ b/gnv-artifacts/doc/conf/conf.xml	Tue Dec 22 10:34:15 2009 +0000
@@ -385,6 +385,26 @@
         
         <artifact name="horizontalCrossSectionMesh" xlink:href="${artifacts.config.dir}/products/horizontalcrosssection/conf_mesh.xml" />
     </artifacts>
+    <charttemplate>
+        <!--    This section configures the path to the template used to create
+                charts. -->
+        <configuration>${artifacts.config.dir}/charttemplate.xml</configuration>
+    </charttemplate>
+    <palettes>
+        <!-- This section configures the palettes used in 2D diagrams. -->
+        <palette name="flow-velocity" 
+                 description="Palette for flow velocity"
+                 file="${artifacts.config.dir}/palette/flow-velocity.xml"/>
+        <palette name="salinity"
+                 description="Palette for salinity"
+                 file="${artifacts.config.dir}/palette/flow-velocity.xml"/>
+        <palette name="water-levels"
+                 description="Palette for water levels"
+                 file="${artifacts.config.dir}/palette/water-levels.xml"/>
+        <palette name="water-temperature"
+                 description="Palette for water temperature"
+                 file="${artifacts.config.dir}/palette/water-temperature.xml"/>
+    </palettes>
     <ehcache>
         <!--  In this Section the Path to Configurationfile for the EHCache,
               which is unsed to store the Results of the Artifacts ,
@@ -415,9 +435,4 @@
         <driver>org.h2.Driver</driver> 
         <sql></sql> -->
     </database>
-    <charttemplate>
-        <!--    This section configures the path to the template used to create
-                charts. -->
-        <configuration>${artifacts.config.dir}/charttemplate.xml</configuration>
-    </charttemplate>
 </artifact-database>
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java	Mon Dec 21 19:58:17 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/context/GNVArtifactContextFactory.java	Tue Dec 22 10:34:15 2009 +0000
@@ -10,18 +10,29 @@
 import java.io.File;
 
 import java.util.Properties;
+import java.util.HashMap;
 
 import org.apache.log4j.Logger;
+
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
+
+import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory;
+
+import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory;
+import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException;
+
+import de.intevation.gnv.chart.XMLChartTheme;
+
+import de.intevation.gnv.artifacts.cache.CacheFactory;
+
+import de.intevation.gnv.raster.Palette;
+
+import de.intevation.artifacts.ArtifactContextFactory;
 
 import de.intevation.artifactdatabase.Config;
-import de.intevation.artifacts.ArtifactContextFactory;
-import de.intevation.gnv.artifacts.cache.CacheFactory;
-import de.intevation.gnv.geobackend.base.connectionpool.ConnectionPoolFactory;
-import de.intevation.gnv.geobackend.base.query.container.QueryContainerFactory;
-import de.intevation.gnv.geobackend.base.query.container.exception.QueryContainerException;
-import de.intevation.gnv.chart.XMLChartTheme;
-
 import de.intevation.artifactdatabase.XMLUtils;
 
 /**
@@ -34,13 +45,26 @@
      */
     private static Logger log = Logger.getLogger(GNVArtifactContext.class);
 
-    public static final String XPATH_GEOBACKEND_CONFIGURATION = "artifact-database/geo-backend/backend-configuration";
+    public static final String XPATH_GEOBACKEND_CONFIGURATION = 
+        "artifact-database/geo-backend/backend-configuration";
 
-    public static final String XPATH_GEOBACKEND_QUERYCONFIGURATION = "artifact-database/geo-backend/query-configuration";
+    public static final String XPATH_GEOBACKEND_QUERYCONFIGURATION = 
+        "artifact-database/geo-backend/query-configuration";
 
-    private final static String CACHECONFIGNODEPATH = "/artifact-database/ehcache/configuration";
+    private final static String CACHECONFIGNODEPATH = 
+        "/artifact-database/ehcache/configuration";
 
-    private final static String CHARTCONFIGNODEPATH = "/artifact-database/charttemplate/configuration";
+    private final static String CHARTCONFIGNODEPATH = 
+        "/artifact-database/charttemplate/configuration";
+
+    public final static String PALETTES_PATH =
+        "/artifact-database/palettes";
+
+    public final static String PALETTE_ITEMS =
+        "palette";
+
+    public final static String PALETTES =
+        "color.palettes";
 
     public final static String CHARTTEMPLATE = "template";
 
@@ -62,7 +86,8 @@
             log.info("Initialisation of the Geo-BackendConnectionPool");
             String backendConfigurationFile = Config.getStringXPath(config,
                     XPATH_GEOBACKEND_CONFIGURATION);
-            backendConfigurationFile = Config.replaceConfigDir(backendConfigurationFile);
+            backendConfigurationFile = Config.replaceConfigDir(
+                backendConfigurationFile);
             
             Properties properties = getProperties(backendConfigurationFile);
             ConnectionPoolFactory cpf = ConnectionPoolFactory.getInstance();
@@ -71,7 +96,8 @@
             log.info("Initialisation of the QueryContainer");
             String queryConfigurationFile = Config.getStringXPath(config,
                     XPATH_GEOBACKEND_QUERYCONFIGURATION);
-            queryConfigurationFile = Config.replaceConfigDir(queryConfigurationFile);
+            queryConfigurationFile = Config.replaceConfigDir(
+                queryConfigurationFile);
             
             Properties queryProperties = getProperties(queryConfigurationFile);
             QueryContainerFactory qcf = QueryContainerFactory.getInstance();
@@ -80,31 +106,19 @@
             log.info("Initialisation of the Cache");
             String cacheConfigurationFile = Config.getStringXPath(config,
                     CACHECONFIGNODEPATH);
-            cacheConfigurationFile = Config.replaceConfigDir(cacheConfigurationFile);
+            cacheConfigurationFile = Config.replaceConfigDir(
+                cacheConfigurationFile);
+
             CacheFactory cf = CacheFactory.getInstance();
             cf.initializeCache(cacheConfigurationFile);
 
-            log.info("Initialisation of chart template");
-            String chartConfigFile = Config.getStringXPath(
-                config, CHARTCONFIGNODEPATH
-            );
-            chartConfigFile = Config.replaceConfigDir(chartConfigFile);
-            log.debug("Parse xml configuration of " + chartConfigFile);
-
-            Document tmpl = XMLUtils.parseDocument(new File(chartConfigFile));
-            XMLChartTheme theme = new XMLChartTheme("XMLChartTheme");
-            if (tmpl != null) {
-                theme.applyXMLConfiguration(tmpl);
-            }
-            else {
-                log.error(
-                    "Cannot load chart template from '" +
-                    chartConfigFile + "'");
-            }
-
             returnValue = new GNVArtifactContext(config);
 
-            returnValue.put(CHARTTEMPLATE, theme);
+            configurePalettes(config, returnValue);
+
+            configureChartTemplate(config, returnValue);
+
+
         } catch (FileNotFoundException e) {
             log.error(e, e);
         } catch (IOException e) {
@@ -115,6 +129,77 @@
         return returnValue;
     }
 
+    protected void configureChartTemplate(
+        Document           config,
+        GNVArtifactContext context
+    ) {
+        log.info("Initialisation of chart template");
+        String chartConfigFile = Config.getStringXPath(
+            config, CHARTCONFIGNODEPATH
+        );
+        chartConfigFile = Config.replaceConfigDir(chartConfigFile);
+        log.debug("Parse xml configuration of " + chartConfigFile);
+
+        Document tmpl = XMLUtils.parseDocument(new File(chartConfigFile));
+        XMLChartTheme theme = new XMLChartTheme("XMLChartTheme");
+        if (tmpl != null) {
+            theme.applyXMLConfiguration(tmpl);
+        }
+        else {
+            log.error(
+                "Cannot load chart template from '" +
+                chartConfigFile + "'");
+        }
+
+        context.put(CHARTTEMPLATE, theme);
+    }
+
+    protected void configurePalettes(
+        Document           config,
+        GNVArtifactContext context
+    ) {
+        log.info("configure palettes");
+
+        HashMap palettes = new HashMap();
+
+        Node node = Config.getNodeXPath(config, PALETTES_PATH);
+
+        if (node == null) {
+            log.error("No palettes found");
+        }
+        else {
+            NodeList pals = Config.getNodeSetXPath(PALETTE_ITEMS);
+            for (int i = 0, N = pals == null ? 0 : pals.getLength(); i < N; ++i) {
+                Element pal = (Element)pals.item(i);
+                String name        = pal.getAttribute("name");
+                String description = pal.getAttribute("description");
+                String filename    = pal.getAttribute("file");
+
+                if (name == null || name.length() == 0) {
+                    log.error("Palette has no 'name' attribute.");
+                }
+                else if (filename == null || filename.length() == 0) {
+                    log.error("Palette has no 'file' attribute.");
+                }
+                else {
+                    filename = Config.replaceConfigDir(filename);
+                    Document document = XMLUtils.parseDocument(
+                        new File(filename));
+                    if (document == null) {
+                        log.error("Cannot load palette file '" +
+                            filename + "'");
+                    }
+                    else {
+                        Palette p = new Palette(document, description);
+                        palettes.put(name, p);
+                    }
+                }
+            }
+        }
+
+        context.put(PALETTES, palettes);
+    }
+
     /**
      * @param filePath
      * @return
@@ -122,8 +207,8 @@
      * @throws IOException
      */
     private Properties getProperties(String filePath)
-                                                     throws FileNotFoundException,
-                                                     IOException {
+    throws FileNotFoundException, IOException
+    {
         InputStream inputStream = null;
         try {
             inputStream = new FileInputStream(filePath);
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Mon Dec 21 19:58:17 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java	Tue Dec 22 10:34:15 2009 +0000
@@ -97,6 +97,7 @@
     protected Entry [] entries;
     protected Entry    lookup;
     protected Color [] rgbs;
+    protected String   description;
 
     private Entry buildLookup(Entry [] entries, int lo, int hi) {
         if (lo > hi) {
@@ -113,6 +114,12 @@
     }
 
     public Palette(Document document) {
+        this(document, null);
+    }
+
+    public Palette(Document document, String description) {
+
+        this.description = description;
 
         NodeList rangeNodes = document.getElementsByTagName("range");
 
@@ -124,9 +131,9 @@
             double from  = doubleValue(rangeElement.getAttribute("from"));
             double to    = doubleValue(rangeElement.getAttribute("to"));
             Color  color = color(rangeElement.getAttribute("rgb"));
-            String description = rangeElement.getAttribute("description");
+            String desc = rangeElement.getAttribute("description");
             if (from > to) { double t = from; from = to; to = t; }
-            entries[i] = new Entry(i, from, to, color, description);
+            entries[i] = new Entry(i, from, to, color, desc);
             rgbs[i] = color;
         }
 
@@ -161,6 +168,10 @@
         lookup = buildLookup(entries, 0, entries.length-1);
     }
 
+    public String getDescription() {
+        return description;
+    }
+
     public int getSize() {
         return rgbs.length;
     }

http://dive4elements.wald.intevation.org