changeset 4083:44dc38ca8492

Added an optional XSL transformation to fix the raw DIPS files. flys-aft/trunk@3513 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 21 Dec 2011 14:56:29 +0000
parents d13011e53022
children 9178beeb7b05
files flys-aft/ChangeLog flys-aft/doc/conf.xml flys-aft/doc/pegelstationen.xml flys-aft/doc/repair.xsl flys-aft/src/main/java/de/intevation/aft/River.java flys-aft/src/main/java/de/intevation/aft/Rivers.java flys-aft/src/main/java/de/intevation/aft/Sync.java flys-aft/src/main/java/de/intevation/utils/XML.java
diffstat 8 files changed, 159 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/flys-aft/ChangeLog	Tue Dec 20 14:36:36 2011 +0000
+++ b/flys-aft/ChangeLog	Wed Dec 21 14:56:29 2011 +0000
@@ -1,3 +1,25 @@
+2011-12-20	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
+
+	* doc/repair.xsl: Repair XSL transform which brings the 
+	  DIPS gauge numbers of the 15 FLYS gauges to the same
+	  numbers as they are used in "Pegel Online".
+
+	  !!! The purpose of this script is to do more repairing !!!
+
+	* doc/pegelstationen.xml: Sub document of repair. Used
+	  for lookup the correct pegel numbers.
+
+	* doc/conf.xml: Changed to optionally load the repair XSLT.
+
+	* src/main/java/de/intevation/aft/Sync.java: Load the
+	  repair XSL transformation if configured.
+
+	* src/main/java/de/intevation/utils/XML.java: Added code
+	  to make XSL transforms possible.
+
+	* src/main/java/de/intevation/aft/River.java,
+	  src/main/java/de/intevation/aft/Rivers.java: Fixed logging.
+
 2011-12-20	Sascha L. Teichmann	<sascha.teichmann@inteavtion.de>
 
 	* src/main/java/de/intevation/aft/River.java,
--- a/flys-aft/doc/conf.xml	Tue Dec 20 14:36:36 2011 +0000
+++ b/flys-aft/doc/conf.xml	Wed Dec 21 14:56:29 2011 +0000
@@ -1,7 +1,10 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <sync>
   <!-- The path to the DiPs file -->
-  <dips file="/the/path/to/the/dips/file"/>
+  <dips>
+    <file>/the/path/to/the/dips/file</file>
+    <repair>/the/path/to/the/xslt/to/repair/dips</repair>
+  </dips>
   <!-- The FLYS side -->
   <side name="flys">
     <db>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/doc/pegelstationen.xml	Wed Dec 21 14:56:29 2011 +0000
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<STATIONEN>
+    <STATION NAME="FREMERSDORF"   NUMMER="0026400550" />
+    <STATION NAME="ST.ARNUAL"     NUMMER="0026400220" />
+    <STATION NAME="COCHEM"        NUMMER="0002690010" />
+    <STATION NAME="TRIER"         NUMMER="0002650010" />
+    <STATION NAME="PERL"          NUMMER="0002610010" />
+    <STATION NAME="SCHOENA"       NUMMER="501010"/>
+    <STATION NAME="DRESDEN"       NUMMER="501060"/>
+    <STATION NAME="TORGAU"        NUMMER="501261"/>
+    <STATION NAME="WITTENBERG"    NUMMER="501420"/>
+    <STATION NAME="AKEN"          NUMMER="502010"/>
+    <STATION NAME="BARBY"         NUMMER="502070"/>
+    <STATION NAME="MAGDEBURG-STR" NUMMER="502180"/>
+    <STATION NAME="TANGERMUENDE"  NUMMER="502350"/>
+    <STATION NAME="WITTENBERGE"   NUMMER="503050"/>
+    <STATION NAME="NEU DARCHAU"   NUMMER="5930010"/>
+</STATIONEN>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-aft/doc/repair.xsl	Wed Dec 21 14:56:29 2011 +0000
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+    <xsl:output method="xml"/>
+
+    <xsl:key name="gauge-name" match="/STATIONEN/STATION" use="@NAME"/>
+
+    <xsl:template name="lookup-gauge-number">
+        <xsl:param name="name"/>
+        <xsl:param name="number"/>
+        <xsl:variable name="fixed-number">
+            <xsl:for-each select="document('pegelstationen.xml')">
+                <xsl:value-of select="key('gauge-name', $name)/@NUMMER"/>
+            </xsl:for-each>
+        </xsl:variable>
+        <xsl:choose>
+            <xsl:when test="$fixed-number != ''">
+                <xsl:value-of select="$fixed-number"/>
+            </xsl:when>
+            <xsl:otherwise>
+                <xsl:value-of select="$number"/>
+            </xsl:otherwise>
+        </xsl:choose>
+    </xsl:template>
+
+    <xsl:template match="/DIPSFLYS/STATIONEN/PEGELSTATION">
+        <PEGELSTATION>
+        <xsl:attribute name="NUMMER">
+            <xsl:call-template name="lookup-gauge-number">
+                <xsl:with-param name="name" select="@NAME"/>
+                <xsl:with-param name="number" select="@NUMMER"/>
+            </xsl:call-template>
+        </xsl:attribute>
+        <xsl:apply-templates select="@*[local-name() != 'NUMMER']"/>
+        <xsl:apply-templates select="node()"/>
+        </PEGELSTATION>
+    </xsl:template>
+
+    <xsl:template match="@*|node()">
+       <xsl:copy>
+          <xsl:apply-templates select="@*|node()"/>
+       </xsl:copy>
+    </xsl:template>
+
+</xsl:stylesheet>
--- a/flys-aft/src/main/java/de/intevation/aft/River.java	Tue Dec 20 14:36:36 2011 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/River.java	Wed Dec 21 14:56:29 2011 +0000
@@ -104,7 +104,6 @@
         while (gaugesRs.next()) {
             int gaugeId = gaugesRs.getInt("id");
             String name = gaugesRs.getString("name");
-            System.err.println(name + ": " + gaugeId);
         }
 
         gaugesRs.close();
--- a/flys-aft/src/main/java/de/intevation/aft/Rivers.java	Tue Dec 20 14:36:36 2011 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/Rivers.java	Wed Dec 21 14:56:29 2011 +0000
@@ -51,7 +51,6 @@
             if (id1 != null) {
                 int id2 = aftRs.getInt("GEWAESSER_NR");
                 River river = new River(id1, id2, name);
-                System.err.println(river);
                 commonRivers.add(river);
             }
         }
--- a/flys-aft/src/main/java/de/intevation/aft/Sync.java	Tue Dec 20 14:36:36 2011 +0000
+++ b/flys-aft/src/main/java/de/intevation/aft/Sync.java	Wed Dec 21 14:56:29 2011 +0000
@@ -6,6 +6,8 @@
 
 import org.w3c.dom.Document;
 
+import org.apache.log4j.Logger;
+
 import javax.xml.xpath.XPathConstants;
 
 import de.intevation.utils.XML;
@@ -14,10 +16,13 @@
 
 public class Sync
 {
+    private static Logger log = Logger.getLogger(Sync.class);
+
     public static final String FLYS = "flys";
     public static final String AFT  = "aft";
 
-    public static final String XPATH_DIPS = "/sync/dips/@file";
+    public static final String XPATH_DIPS   = "/sync/dips/file/text()";
+    public static final String XPATH_REPAIR = "/sync/dips/repair/text()";
 
     public static final String CONFIG_FILE =
         System.getProperty("config.file", "config.xml");
@@ -27,14 +32,14 @@
         File configFile = new File(CONFIG_FILE);
 
         if (!configFile.isFile() || !configFile.canRead()) {
-            System.err.println("cannot read config file");
+            log.error("cannot read config file");
             System.exit(1);
         }
 
         Document config = XML.parseDocument(configFile, Boolean.FALSE);
 
         if (config == null) {
-            System.err.println("cannot load config");
+            log.error("Cannot load config file.");
             System.exit(1);
         }
 
@@ -42,24 +47,44 @@
             config, XPATH_DIPS, XPathConstants.STRING, null, null);
 
         if (dipsF == null || dipsF.length() == 0) {
-            System.err.println("Cannot find path to DiPS xml in config.");
+            log.error("Cannot find path to DIPS XML in config.");
             System.exit(1);
         }
 
         File dipsFile = new File(dipsF);
 
         if (!dipsFile.isFile() || !dipsFile.canRead()) {
-            System.err.println("Cannot find '" + dipsF + "'");
+            log.error("Cannot find '" + dipsF + "'");
             System.exit(1);
         }
 
         Document dips = XML.parseDocument(dipsFile, Boolean.FALSE);
 
         if (dips == null) {
-            System.err.println("Cannot load DiPs document.");
+            log.error("Cannot load DIPS document.");
             System.exit(1);
         }
 
+        String repairF = (String)XML.xpath(
+            config, XPATH_REPAIR, XPathConstants.STRING, null, null);
+
+        if (repairF != null && repairF.length() > 0) {
+            File repairFile = new File(repairF);
+            if (!repairFile.isFile() || !repairFile.canRead()) {
+                log.warn("Cannot open DIPS repair XSLT file.");
+            }
+            else {
+                Document fixed = XML.transform(dips, repairFile);
+                if (fixed == null) {
+                    log.warn("Fixing DIPS failed");
+                }
+                else {
+                    dips = fixed;
+                }
+            }
+        }
+ 
+
         ConnectionBuilder aftConnectionBuilder =
             new ConnectionBuilder(AFT, config);
 
@@ -76,8 +101,7 @@
             rivers.sync(syncContext);
         }
         catch (SQLException sqle) {
-            sqle.printStackTrace();
-            System.err.println("syncing failed: " + sqle.getMessage());
+            log.error("syncing failed", sqle);
         }
         finally {
             if (syncContext != null) {
--- a/flys-aft/src/main/java/de/intevation/utils/XML.java	Tue Dec 20 14:36:36 2011 +0000
+++ b/flys-aft/src/main/java/de/intevation/utils/XML.java	Wed Dec 21 14:56:29 2011 +0000
@@ -26,6 +26,16 @@
 import javax.xml.xpath.XPathFactory;
 import javax.xml.xpath.XPathVariableResolver;
 
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import javax.xml.transform.stream.StreamSource;
+
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.dom.DOMResult;
+
 public final class XML
 {
     /** Logger for this class. */
@@ -217,5 +227,32 @@
 
         return null;
     }
+
+    public static Document transform(
+        Document           document,
+        File               xformFile
+    ) {
+        try {
+            Transformer transformer =
+                TransformerFactory
+                    .newInstance()
+                    .newTransformer(
+                        new StreamSource(xformFile));
+
+            DOMResult result = new DOMResult();
+
+            transformer.transform(new DOMSource(document), result);
+
+            return (Document)result.getNode();
+        }
+        catch (TransformerConfigurationException tce) {
+            log.error(tce, tce);
+        }
+        catch (TransformerException te) {
+            log.error(te, te);
+        }
+
+        return null;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org