changeset 1213:cc88db4a5b34

Added method walkTree() to traverse a directory tree. To be reused in HYK parser. flys-backend/trunk@2339 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Jul 2011 15:36:59 +0000
parents dd948530fd57
children 32ee9babe42c
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/parsers/PRFParser.java flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java
diffstat 3 files changed, 49 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Fri Jul 15 14:33:12 2011 +0000
+++ b/flys-backend/ChangeLog	Fri Jul 15 15:36:59 2011 +0000
@@ -1,3 +1,11 @@
+2011-07-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/FileTools.java: Added method
+	  walkTree() to traverse a directory tree. To be reused in HYK parser.
+
+	* src/main/java/de/intevation/flys/importer/parsers/PRFParser.java:
+	  Uses the FileTools.walkTree() method now to find all PRF file.
+
 2011-07-15	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* doc/schema/postgresql.sql: Added missing columns.
@@ -8,6 +16,7 @@
 	    ALTER TABLE hyk_entries ADD COLUMN distance_hf NUMERIC NOT NULL;
 	    ALTER TABLE hyk_entries ADD COLUMN distance_vr NUMERIC NOT NULL;
 	  COMMIT;
+
 	* src/main/java/de/intevation/flys/model/HYKEntry.java,
 	  src/main/java/de/intevation/flys/model/HYK.java:
 	  Adjusted Hibernate models.
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/PRFParser.java	Fri Jul 15 14:33:12 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/PRFParser.java	Fri Jul 15 15:36:59 2011 +0000
@@ -1,7 +1,6 @@
 package de.intevation.flys.importer.parsers;
 
 import java.util.Map;
-import java.util.Stack;
 import java.util.TreeMap;
 import java.util.List;
 import java.util.ArrayList;
@@ -20,6 +19,8 @@
 
 import de.intevation.flys.importer.XY;
 
+import de.intevation.flys.utils.FileTools;
+
 public class PRFParser
 {
     private static Logger log = Logger.getLogger(PRFParser.class);
@@ -383,33 +384,24 @@
         description = null;
     }
 
-    public void parsePRFs(File root, Callback callback) {
-
-        Stack<File> stack = new Stack<File>();
-        stack.push(root);
+    public void parsePRFs(File root, final Callback callback) {
 
-        while (!stack.empty()) {
-            File file = stack.pop();
-            if (file.isDirectory()) {
-                File [] files = file.listFiles();
-                if (files != null) {
-                    for (File f: files) {
-                        stack.push(f);
+        FileTools.walkTree(root, new FileTools.FileVisitor() {
+            @Override
+            public boolean visit(File file) {
+                if (file.isFile()
+                && file.getName().toLowerCase().endsWith(".prf")
+                && (callback == null || callback.prfAccept(file))) {
+                    reset();
+                    boolean success = parse(file);
+                    log.info("parsing " + (success ? "succeeded" : "failed"));
+                    if (success && callback != null) {
+                        callback.prfParsed(PRFParser.this);
                     }
                 }
+                return true;
             }
-            else if (file.isFile()
-                && file.getName().toLowerCase().endsWith(".prf")
-                && (callback == null || callback.prfAccept(file))
-            ) {
-                reset();
-                boolean success = parse(file);
-                log.info("parsing " + (success ? "succeeded" : "failed"));
-                if (success && callback != null) {
-                    callback.prfParsed(this);
-                }
-            }
-        }
+        });
     }
 
     public static void main(String [] args) {
--- a/flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java	Fri Jul 15 14:33:12 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java	Fri Jul 15 15:36:59 2011 +0000
@@ -192,5 +192,29 @@
 
         return out;
     }
+
+    public interface FileVisitor {
+        boolean visit(File file);
+    } // Visitor
+
+    public static void walkTree(File root, FileVisitor visitor) {
+
+        Stack<File> stack = new Stack<File>();
+
+        stack.push(root);
+
+        while (!stack.isEmpty()) {
+            File current = stack.pop();
+            if (!visitor.visit(current)) break;
+            if (current.isDirectory()) {
+                File [] subs = current.listFiles();
+                if (subs != null) {
+                    for (File f: subs) {
+                        stack.push(f);
+                    }
+                }
+            }
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org