From 1fe86f0b8adf656ddce7e143565204b0f97562e0 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 20 Jun 2010 14:44:37 +0200 Subject: [PATCH] Add cardinal methods. --- src/fr/devinsy/util/FileIterator.java | 104 +++++++++++++++++++-- src/fr/devinsy/util/FileIteratorState.java | 11 +++ 2 files changed, 109 insertions(+), 6 deletions(-) diff --git a/src/fr/devinsy/util/FileIterator.java b/src/fr/devinsy/util/FileIterator.java index e79e2cf..50d0616 100644 --- a/src/fr/devinsy/util/FileIterator.java +++ b/src/fr/devinsy/util/FileIterator.java @@ -17,6 +17,7 @@ public class FileIterator extends Vector implements Iterator< protected int currentDepth; protected Pattern pattern; protected File previous; + protected boolean followLink; /** @@ -34,6 +35,7 @@ public class FileIterator extends Vector implements Iterator< this.pattern = null; this.previous = null; + this.followLink = false; } @@ -61,6 +63,7 @@ public class FileIterator extends Vector implements Iterator< } this.previous = null; + this.followLink = false; } @@ -75,10 +78,28 @@ public class FileIterator extends Vector implements Iterator< this.add(new FileIteratorState(pathnames, prefix)); this.pattern = null; this.previous = null; + this.followLink = false; } + /** + * + */ + public void reset () + { + this.currentDepth = 0; + this.previous = null; + if (this.size() > 0) + { + this.get(0).reset(); + FileIteratorState firstState = this.get(0); + this.removeAllElements(); + this.add(firstState); + } + } + + /** * */ @@ -171,14 +192,15 @@ public class FileIterator extends Vector implements Iterator< } else { - if ((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) + if (((this.pattern == null) || (this.pattern.matcher(next.getPath()).matches())) && (follow(next))) { ended = true; } else { this.currentState().next(); - if (next.isDirectory()) + + if (next.isDirectory() && (follow(next))) { this.push(next); } @@ -214,14 +236,16 @@ public class FileIterator extends Vector implements Iterator< result = this.currentState().next(); this.previous = result; - - if (result.isDirectory()) + if (result != null) { - this.push(result); + if (result.isDirectory()) + { + this.push(result); + } } shift(); - + // return(result); } @@ -241,6 +265,74 @@ public class FileIterator extends Vector implements Iterator< } + /** + * + */ + static public boolean isLink(File file) throws Exception + { + boolean result; + if (file.getCanonicalPath().equals(file.getAbsolutePath())) + { + result = false; + } + else + { + result = true; + } + + // + return(result); + } + + + /** + * + */ + public boolean follow(File file) + { + boolean result; + + result = false; + try + { + if ((this.followLink) || (!isLink(file))) + { + result = true; + } + else + { + result = false; + } + } + catch (Exception exception) + { + System.err.println("ERROR with file [" + this.next() + "]: " + exception.getMessage()); + result = false; + } + + // + return(result); + } + + + /** + * + */ + public int finalCountdown() + { + int result; + + result = 0; + while (this.next() != null) + { + result += 1; + } + + // + return(result); + } + + /** * */ diff --git a/src/fr/devinsy/util/FileIteratorState.java b/src/fr/devinsy/util/FileIteratorState.java index 570203c..7851373 100644 --- a/src/fr/devinsy/util/FileIteratorState.java +++ b/src/fr/devinsy/util/FileIteratorState.java @@ -17,6 +17,8 @@ public class FileIteratorState implements Iterator */ public FileIteratorState (String[] pathnames, String prefix) { + this.currentIndex = 0; + if (prefix == null) { prefix = ""; @@ -56,6 +58,15 @@ public class FileIteratorState implements Iterator } + /** + * + */ + public void reset() + { + currentIndex = 0; + } + + /** * */