changeset 1216:f8b5c37f15e4

Fixes for the HYK parser flys-backend/trunk@2342 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 17 Jul 2011 10:00:13 +0000
parents 8aef353e54fb
children d5e39c16fd2a
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/PRFParser.java
diffstat 3 files changed, 69 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Fri Jul 15 16:57:13 2011 +0000
+++ b/flys-backend/ChangeLog	Sun Jul 17 10:00:13 2011 +0000
@@ -1,3 +1,12 @@
+2011-07-17	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/parsers/HYKParser.java:
+	  Added callback mechanism and recursive file search like in the PRF parser.
+	  All BfG-HYK files seem to parse correctly now. TODO: Build the data structures.
+
+	* src/main/java/de/intevation/flys/importer/parsers/PRFParser.java:
+	  Added x.canRead() before accepting files for parsing.
+
 2011-07-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/parsers/HYKParser.java:
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java	Fri Jul 15 16:57:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/HYKParser.java	Sun Jul 17 10:00:13 2011 +0000
@@ -15,10 +15,17 @@
 
 import org.apache.log4j.Logger;
 
+import de.intevation.flys.utils.FileTools;
+
 public class HYKParser
 {
     private static Logger log = Logger.getLogger(HYKParser.class);
 
+    public interface Callback {
+        boolean hykAccept(File file);
+        void    hykParsed(HYKParser parser);
+    } // interface Callback
+
     public static enum State {
         LINE_1, LINE_2, LINE_3, LINE_4, LINE_5, LINE_6
     };
@@ -33,6 +40,8 @@
 
     public boolean parse(File file) {
 
+        boolean debug = log.isDebugEnabled();
+
         log.info("Parsing HYK file '" + file + "'");
 
         LineNumberReader in = null;
@@ -64,18 +73,27 @@
             int                      coordPos = 0;
 
             while ((line = in.readLine()) != null) {
-                if ((line = line.trim()).length() == 0) {
+
+                if (line.startsWith("*") || line.startsWith("----")) {
                     continue;
                 }
-                if (line.startsWith("*")) {
+
+                line = line.trim();
+
+                if (state != State.LINE_5 && line.length() == 0) {
                     continue;
                 }
+
                 String [] parts = line.split("\\s+");
 
+                if (debug) {
+                    log.debug("'" + line + "': " + state);
+                }
+
                 switch (state) {
                     case LINE_1:
                         if (parts.length < 2) {
-                            log.error("not enough elements in line " +
+                            log.error("1: not enough elements in line " +
                                 in.getLineNumber());
                             return false;
                         }
@@ -113,7 +131,7 @@
 
                     case LINE_2:
                         if (parts.length < 3) {
-                            log.error("not enough elements in line " +
+                            log.error("2: not enough elements in line " +
                                 in.getLineNumber());
                             return false;
                         }
@@ -143,6 +161,7 @@
                         for (int i = 0; i < fzts.length; ++i) {
                             fzts[i] = getFlowZoneType(parts[i]);
                         }
+                        coords = new BigDecimal[numZones];
                         state = State.LINE_4;
                         break;
 
@@ -164,7 +183,7 @@
 
                     case LINE_5:
                         if (parts.length + coordPos < coords.length) {
-                            log.error("not enough elements in line " +
+                            log.error("5: not enough elements in line " +
                                 in.getLineNumber());
                             return false;
                         }
@@ -186,7 +205,7 @@
 
                     case LINE_6:
                         if (parts.length < 3) {
-                            log.error("not enough elements in line " +
+                            log.error("6: not enough elements in line " +
                                 in.getLineNumber());
                             return false;
                         }
@@ -232,10 +251,44 @@
         name = name.toUpperCase();
         ImportHYKFlowZoneType fzt = flowZoneTypes.get(name);
         if (fzt == null) {
+            log.info("New flow zone type: " + name);
             fzt = new ImportHYKFlowZoneType(name);
             flowZoneTypes.put(name, fzt);
         }
         return fzt;
     }
+
+    protected void reset() {
+        // TODO: reset per file data structures
+    }
+
+    public void parseHYKs(File root, final Callback callback) {
+
+        FileTools.walkTree(root, new FileTools.FileVisitor() {
+            @Override
+            public boolean visit(File file) {
+                if (file.isFile() && file.canRead()
+                && file.getName().toLowerCase().endsWith(".hyk")
+                && (callback == null || callback.hykAccept(file))) {
+                    reset();
+                    boolean success = parse(file);
+                    log.info("parsing " + (success ? "succeeded" : "failed"));
+                    if (success && callback != null) {
+                        callback.hykParsed(HYKParser.this);
+                    }
+                }
+                return true;
+            }
+        });
+    }
+
+    public static void main(String [] args) {
+
+        HYKParser parser = new HYKParser();
+
+        for (String arg: args) {
+            parser.parseHYKs(new File(arg), null);
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/PRFParser.java	Fri Jul 15 16:57:13 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/PRFParser.java	Sun Jul 17 10:00:13 2011 +0000
@@ -389,7 +389,7 @@
         FileTools.walkTree(root, new FileTools.FileVisitor() {
             @Override
             public boolean visit(File file) {
-                if (file.isFile()
+                if (file.isFile() && file.canRead()
                 && file.getName().toLowerCase().endsWith(".prf")
                 && (callback == null || callback.prfAccept(file))) {
                     reset();

http://dive4elements.wald.intevation.org