diff --git a/src/fr/devinsy/util/FileIterator.java b/src/fr/devinsy/util/FileIterator.java index 50d0616..74192ef 100644 --- a/src/fr/devinsy/util/FileIterator.java +++ b/src/fr/devinsy/util/FileIterator.java @@ -17,7 +17,7 @@ public class FileIterator extends Vector implements Iterator< protected int currentDepth; protected Pattern pattern; protected File previous; - protected boolean followLink; + protected boolean followLinks; /** @@ -27,59 +27,89 @@ public class FileIterator extends Vector implements Iterator< { super(); - this.currentDepth = -1; // Note: push method increments this value. - if (root != null) + String[] pathnames; + if (root == null) { - this.push(root); - } - - this.pattern = null; - this.previous = null; - this.followLink = false; - } - - - /** - * - */ - public FileIterator (File root, String filter) - { - super(); - - this.currentDepth = -1; // Note: push method increments this value. - if (root != null) - { - this.push(root); - } - - if (filter == null) - { - this.pattern = null; + pathnames = null; } else { - this.pattern = Pattern.compile(filter); - shift(); + pathnames = new String[1]; + pathnames[0] = root.getPath(); } - - this.previous = null; - this.followLink = false; + + init (pathnames, null, false); } /** * */ - public FileIterator (String[] pathnames, String prefix) + public FileIterator (File root, String filter, boolean followLinks) { super(); - - this.currentDepth = 0; - this.add(new FileIteratorState(pathnames, prefix)); - this.pattern = null; + + String[] pathnames; + if (root == null) + { + pathnames = null; + } + else + { + pathnames = new String[1]; + pathnames[0] = root.getPath(); + } + + init (pathnames, filter, followLinks); + } + + + /** + * + */ + public FileIterator (String pathname, String filter, boolean followLinks) + { + super(); + + String[] pathnames; + if (pathname == null) + { + pathnames = null; + } + else + { + pathnames = new String[1]; + pathnames[0] = pathname; + } + + init (pathnames, filter, followLinks); + } + + + /** + * + */ + public FileIterator (String[] pathnames, String filter, boolean followLinks) + { + super(); + + init(pathnames, filter, followLinks); + } + + + /** + * + */ + protected void init (String[] pathnames, String filter, boolean followLinks) + { + setFilter(filter); + this.followLinks = followLinks; this.previous = null; - this.followLink = false; - + + this.currentDepth = 0; + this.add(new FileIteratorState(pathnames)); + + shift(); } @@ -121,6 +151,36 @@ public class FileIterator extends Vector implements Iterator< } + /** + * + */ + public Pattern pattern() + { + Pattern result; + + result = this.pattern; + + // + return(result); + } + + + /** + * + */ + protected void setFilter(String filter) + { + if (filter == null) + { + this.pattern = null; + } + else + { + this.pattern = Pattern.compile(filter); + } + } + + /** * */ @@ -152,10 +212,13 @@ public class FileIterator extends Vector implements Iterator< /** * */ - public void push(File directory) + public void push(File file) { - this.add(new FileIteratorState(directory)); - this.currentDepth += 1; + if ((file != null) && (file.isDirectory())) + { + this.add(new FileIteratorState(file.listFiles())); + this.currentDepth += 1; + } } @@ -169,6 +232,56 @@ 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.followLinks) || (!isLink(file))) + { + result = true; + } + else + { + result = false; + } + } + catch (Exception exception) + { + System.err.println("ERROR with file [" + this.next() + "]: " + exception.getMessage()); + result = false; + } + + // + return(result); + } + + /** * Set indexes to the good next item. */ @@ -207,8 +320,7 @@ public class FileIterator extends Vector implements Iterator< } } } - } - + } /** * @@ -232,10 +344,9 @@ public class FileIterator extends Vector implements Iterator< public File next() { File result; - + result = this.currentState().next(); this.previous = result; - if (result != null) { if (result.isDirectory()) @@ -265,56 +376,6 @@ 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); - } - - /** * */ diff --git a/src/fr/devinsy/util/FileIteratorState.java b/src/fr/devinsy/util/FileIteratorState.java index 7851373..b3457b1 100644 --- a/src/fr/devinsy/util/FileIteratorState.java +++ b/src/fr/devinsy/util/FileIteratorState.java @@ -2,6 +2,7 @@ package fr.devinsy.util; import java.io.File; import java.util.Iterator; +import java.util.regex.Pattern; /** @@ -12,28 +13,19 @@ public class FileIteratorState implements Iterator protected File[] files; protected int currentIndex; + /** - * + * Useful for the depth zero, otherwise parent path is lost. */ - public FileIteratorState (String[] pathnames, String prefix) + public FileIteratorState (String[] pathnames) { + // Initialize the state. this.currentIndex = 0; - if (prefix == null) - { - prefix = ""; - } - else if (!prefix.endsWith(File.separator)) - { - prefix += File.separator; - } - this.files = new File[pathnames.length]; for (int pathnameIndex = 0; pathnameIndex < pathnames.length; pathnameIndex++) { - String pathname = pathnames[pathnameIndex]; - - this.files[pathnameIndex] = new File(prefix + pathname); + this.files[pathnameIndex] = new File(pathnames[pathnameIndex]); } } @@ -41,19 +33,18 @@ public class FileIteratorState implements Iterator /** * */ - public FileIteratorState (File file) + public FileIteratorState (File[] files) { - if (file.isDirectory()) + // Initialize the state. + this.currentIndex = 0; + + if (files == null) { - this.files = file.listFiles(); - this.currentIndex = 0; + this.files = new File[0]; } else { - // File case or not exist file. - this.files = new File[1]; - this.files[0] = file; - this.currentIndex = 0; + this.files = files; } } @@ -125,7 +116,7 @@ public class FileIteratorState implements Iterator boolean result; - if (this.currentIndex >= this.files.length) + if (this.currentFile() == null) { result = false; } @@ -149,7 +140,7 @@ public class FileIteratorState implements Iterator result = this.currentFile(); this.currentIndex += 1; - + // return(result); } diff --git a/tests/FileIteratorTester.java b/tests/FileIteratorTester.java new file mode 100644 index 0000000..517da6c --- /dev/null +++ b/tests/FileIteratorTester.java @@ -0,0 +1,87 @@ + + + +import java.io.File; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; + +import fr.devinsy.util.FileIterator; + + +/** + * + */ +public class FileIteratorTester +{ + static private org.apache.log4j.Logger logger; + + static + { + // Initialize logger. + + BasicConfigurator.configure (); + Logger defaultLogger = org.apache.log4j.Logger.getRootLogger (); + defaultLogger.setLevel (org.apache.log4j.Level.DEBUG); + + defaultLogger.removeAllAppenders(); + defaultLogger.addAppender(new ConsoleAppender(new PatternLayout("%d{ISO8601} - FIT [%-5p] %34.34c.%-25M - %m%n"))); + + defaultLogger.debug ("Log initialized."); + + logger = org.apache.log4j.Logger.getLogger (FileIteratorTester.class.getName ()); + } + + /** + * + */ + protected static void test() + { + System.out.println("user.dir=" + System.getProperty("user.dir")); + + try + { + File f = new File("TestTree/DirectoryOne/titi2"); + System.out.println("exists=" + f.exists()); + System.out.println("canonical path = " + f.getCanonicalPath()); + System.out.println("absolute path = " + f.getAbsolutePath()); + System.out.println("name = " + f.getName()); + System.out.println("parent = " + f.getParent()); + System.out.println("path = " + f.getPath()); + System.out.println("path = " + f.lastModified()); + System.out.println("path = " + f.length()); + System.out.println("path = " + f.isFile()); + System.out.println("path = " + f.isDirectory()); + System.out.println("list = " + f.list()); + + System.out.println("----"); + //FileIterator i = new FileIterator(new File("tests/TestTree")); + //FileIterator i = new FileIterator(new File("tests/TestTree"), ".*dsc.*", false); + //FileIterator i = new FileIterator(new File("/home/cpm/Images/Photos/")); + // FileIterator i = new FileIterator(new File("/home/cpm/Images/Photos/"), ".*\\.(JPG|jpg)", false); + FileIterator i = new FileIterator(new File("/home/cpm/Images/Photos/"), ".*anni_moi.*", false); + while (i.hasNext()) + { + //System.out.println(i.toString()); + System.out.println("File=[" + i.next().getPath() + "]"); + } + i.reset(); + System.out.println("Cardinal=" + i.finalCountdown()); + } + catch(Exception exception) + { + System.out.println("ERROR:" + exception.getMessage()); + } + } + + + /** + * + */ + public static void main (String[] args) + { + test(); + } +} diff --git a/tests/Test.java b/tests/Test.java deleted file mode 100644 index 57e35ce..0000000 --- a/tests/Test.java +++ /dev/null @@ -1,242 +0,0 @@ -import java.util.regex.Pattern; - -import fr.devinsy.xid.Data; -import fr.devinsy.xid.IdData; -import fr.devinsy.xid.StringPresenter; - -/** - * Devinsy-utils tests. - */ - - - -/** - * - */ -class XidTest -{ - static private org.apache.log4j.Logger logger; - - static - { - // Initialize logger. - org.apache.log4j.Logger log = null; - - org.apache.log4j.BasicConfigurator.configure (); - - - log = org.apache.log4j.Logger.getRootLogger (); - //logger.setLevel (org.apache.log4j.Level.INFO); - logger.setLevel (org.apache.log4j.Level.INFO); - - logger.info ("Enter"); - - // - logger.info ("Set the log file format..."); - - - // log = org.apache.log4j.Category.getInstance(Application.class.getName()); - logger.info ("... done."); - - logger.debug ("Exit"); - log = org.apache.log4j.Logger.getLogger (XidTest.class.getName ()); - } - - - - /** - * - */ - public static String check (String title, StringBuffer source, String model) - { - String result; - - if (source.indexOf (model) == -1) - { - result = String.format ("%-40s -> KO <-", title) + "\nGet:\n" + source + "\nWaiting:\n" + model; - - } - else - { - result = String.format ("%-40s [ OK ] ", title); - } - - - // - return (result); - } - - public enum MONTHS {JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBRE, DECEMBRE}; - - /** - * - */ - public static void main(String[] args) - { - System.out.println("Automatic test action for Xid!"); - - Data datas; - IdData tag; - - - String htmlSource; - StringBuffer html; - StringBuffer errorMessage; - - // Populate attributes of Test 03. - System.out.println ("----------------------------"); - datas = new Data (); - datas.setContent ("name", "Superman"); - errorMessage = new StringBuffer (); - - html = StringPresenter.doXid ("
a name
", datas, errorMessage); - - System.out.println (check ("only content change", html, "
Superman
")); - - - // Populate attributes of Test 03. - System.out.println ("----------------------------"); - datas = new Data (); - datas.setContent ("lastname", "Spiderman"); - datas.appendAttribute ("lastname", "style", "background: blue;"); - datas.appendAttribute ("lastname", "style", "foreground: red;"); - datas.setAttribute ("lastname", "class", "nameClass"); - - errorMessage = new StringBuffer (); - html = StringPresenter.doXid ("
a last name
", datas, errorMessage); - System.out.println (check ("content and attributes", html, "
Spiderman
")); - - // Populate attributes of Test 03. - System.out.println ("----------------------------"); - datas = new Data (); - datas.setContent ("words", 0, "alpha"); - datas.setContent ("words", 1, "bravo"); - datas.setContent ("words", 2, "charlie"); - datas.setContent ("words", 3, "delta"); - datas.setContent ("words", 4, "echo"); - datas.setContent ("words", 5, "fox"); - - - errorMessage = new StringBuffer (); - html = StringPresenter.doXid ("
    \n
  • a word
  • \n
", datas, errorMessage); - - System.out.println (check ("list assertion 1", html, "
  • alpha
  • ")); - System.out.println (check ("list assertion 2", html, "
  • bravo
  • ")); - System.out.println (check ("list assertion 3", html, "
  • charlie
  • ")); - System.out.println (check ("list assertion 4", html, "
  • delta
  • ")); - System.out.println (check ("list assertion 5", html, "
  • echo
  • ")); - System.out.println (check ("list assertion 6", html, "
  • fox
  • ")); - - // Populate attributes of Test 03. - System.out.println ("----------------------------"); - datas = new Data (); - datas.setContent ("identity", 0, "nom", "Jemba"); - datas.setContent ("identity", 0, "prenom", "Epo"); - datas.setContent ("identity", 1, "nom", "Momon"); - datas.setContent ("identity", 1, "prenom", "Christian"); - datas.setContent ("identity", 2, "nom", "Tronche"); - datas.setContent ("identity", 2, "prenom", "Christophe"); - - - errorMessage = new StringBuffer (); - StringBuffer source = new StringBuffer (); - source.append ("\n"); - source.append (" \n"); - source.append ("
    noidun nomun prenom
    "); - htmlSource = source.toString (); - html = StringPresenter.doXid (htmlSource, datas, errorMessage); - - System.out.println (check ("table 1 assertion 1", html, "noidJembaEpo")); - System.out.println (check ("table 1 assertion 2", html, "noidMomonChristian")); - System.out.println (check ("table 1 assertion 3", html, "noidTroncheChristophe")); - - /* - // Populate attributes of Test 03. - System.out.println ("----------------------------"); - datas = new Data (); - datas.setContent ("identity", 0, "nom", "Jemba"); - datas.setContent ("identity", 0, "prenom", "Epo"); - datas.setContent ("identity", 1, "nom", "Momon"); - datas.setContent ("identity", 1, "prenom", "Christian"); - datas.setContent ("identity", 2, "nom", "Tronche"); - datas.setContent ("identity", 2, "prenom", "Christophe"); - datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ONLY_FIRST_ROW); - //datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ONLY_ROWS_WITH_ID); - //datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ONLY_ROWS_WITHOUT_ID); - //datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ALL_ROWS); - - - errorMessage = new StringBuffer (); - source = new StringBuffer (); - source.append ("\n"); - source.append (" \n"); - source.append (" \n"); - source.append (" \n"); - source.append ("
    noidun nomun prenom
    noidun nomun prenom
    noidun nomun prenom
    "); - htmlSource = source.toString (); - - System.out.println ("datas = new Data ();"); - System.out.println ("datas.setContent (\"identity\", 0, \"nom\", \"Jemba\");"); - System.out.println ("datas.setContent (\"identity\", 0, \"prenom\", \"Epo\");"); - System.out.println ("datas.setContent (\"identity\", 1, \"nom\", \"Momon\");"); - System.out.println ("datas.setContent (\"identity\", 1, \"prenom\", \"Christian\");"); - System.out.println ("datas.setContent (\"identity\", 2, \"nom\", \"Tronche\");"); - System.out.println ("datas.setContent (\"identity\", 2, \"prenom\", \"Christophe\");"); - - System.out.println ("+"); - System.out.println (htmlSource); - System.out.println ("=>"); - - - datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ONLY_FIRST_ROW); - System.out.println ("ONLY_FIRST_ROW:"); - html = Presenter.doXid (htmlSource, datas, "", errorMessage); - System.out.println (html); - - datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ONLY_ROWS_WITH_ID); - System.out.println ("ONLY_ROWS_WITH_ID:"); - html = Presenter.doXid (htmlSource, datas, "", errorMessage); - System.out.println (html); - - datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ONLY_ROWS_WITHOUT_ID); - System.out.println ("ONLY_ROWS_WITHOUT_ID:"); - html = Presenter.doXid (htmlSource, datas, "", errorMessage); - System.out.println (html); - - datas.setIterationStrategy ("identity", IdsDataByIndex.IterationStrategy.ALL_ROWS); - System.out.println ("ALL_ROWS:"); - html = Presenter.doXid (htmlSource, datas, "", errorMessage); - System.out.println (html); - - - - // Populate attributes of Test 03. - System.out.println ("----------------------------"); - datas = new Data (); - datas.setAttribute ("
    ", "class", "aDivClass"); - datas.setAttribute ("
    ", "style", "background-color: #000000;"); - datas.setAttribute ("number", "style", "background-color: #0000FF;"); - - errorMessage = new StringBuffer (); - source = new StringBuffer (); - source.append ("
    \n"); - source.append ("

    one

    \n"); - source.append ("
    \n"); - source.append ("
    \n"); - source.append ("

    three

    \n"); - source.append ("
    "); - htmlSource = source.toString (); - html = Presenter.doXid (htmlSource, datas, "", errorMessage); - - System.out.println (htmlSource); - System.out.println ("+"); - System.out.println ("datas = new Data ();"); - System.out.println ("datas.setAttribute (\"
    \", \"class\", \"aDivClass\");"); - System.out.println ("datas.setAttribute (\"
    \", \"style\", \"background-color: #000000;\");"); - System.out.println ("datas.setAttribute (\"number\", \"style\", \"background-color: #0000FF;\");"); - - System.out.println ("=>"); - System.out.println (html); - */ - } -} diff --git a/tests/TestTree/.test/arf b/tests/TestTree/.test/arf new file mode 100644 index 0000000..613a3f5 --- /dev/null +++ b/tests/TestTree/.test/arf @@ -0,0 +1 @@ +test sioux diff --git a/tests/TestTree/20081111-nouvelle_voiture/dsc01469.jpg b/tests/TestTree/20081111-nouvelle_voiture/dsc01469.jpg new file mode 100644 index 0000000..49b1665 Binary files /dev/null and b/tests/TestTree/20081111-nouvelle_voiture/dsc01469.jpg differ diff --git a/tests/TestTree/20081111-nouvelle_voiture/dsc01470.jpg b/tests/TestTree/20081111-nouvelle_voiture/dsc01470.jpg new file mode 100644 index 0000000..88a4027 Binary files /dev/null and b/tests/TestTree/20081111-nouvelle_voiture/dsc01470.jpg differ diff --git a/tests/TestTree/20081111-nouvelle_voiture/dsc01472.jpg b/tests/TestTree/20081111-nouvelle_voiture/dsc01472.jpg new file mode 100644 index 0000000..8fff0e5 Binary files /dev/null and b/tests/TestTree/20081111-nouvelle_voiture/dsc01472.jpg differ diff --git a/tests/TestTree/20081111-nouvelle_voiture/dsc01474.jpg b/tests/TestTree/20081111-nouvelle_voiture/dsc01474.jpg new file mode 100644 index 0000000..0e662f1 Binary files /dev/null and b/tests/TestTree/20081111-nouvelle_voiture/dsc01474.jpg differ diff --git a/tests/TestTree/DirectoryOne/titi b/tests/TestTree/DirectoryOne/titi new file mode 100644 index 0000000..cbd38df --- /dev/null +++ b/tests/TestTree/DirectoryOne/titi @@ -0,0 +1 @@ +azertyuiop diff --git a/tests/TestTree/DirectoryOne/toto b/tests/TestTree/DirectoryOne/toto new file mode 100644 index 0000000..388a530 --- /dev/null +++ b/tests/TestTree/DirectoryOne/toto @@ -0,0 +1 @@ +good weather today diff --git a/tests/TestTree/DirectoryOne/tu tu b/tests/TestTree/DirectoryOne/tu tu new file mode 100644 index 0000000..20b7025 --- /dev/null +++ b/tests/TestTree/DirectoryOne/tu tu @@ -0,0 +1 @@ +Ceci est un test. diff --git a/tests/TestTree/P/dsc01469.jpg b/tests/TestTree/P/dsc01469.jpg new file mode 100644 index 0000000..49b1665 Binary files /dev/null and b/tests/TestTree/P/dsc01469.jpg differ diff --git a/tests/TestTree/P/dsc01470.jpg b/tests/TestTree/P/dsc01470.jpg new file mode 100644 index 0000000..88a4027 Binary files /dev/null and b/tests/TestTree/P/dsc01470.jpg differ diff --git a/tests/TestTree/P/dsc01472.jpg b/tests/TestTree/P/dsc01472.jpg new file mode 100644 index 0000000..8fff0e5 Binary files /dev/null and b/tests/TestTree/P/dsc01472.jpg differ diff --git a/tests/TestTree/P/dsc01474.jpg b/tests/TestTree/P/dsc01474.jpg new file mode 100644 index 0000000..0e662f1 Binary files /dev/null and b/tests/TestTree/P/dsc01474.jpg differ diff --git a/tests/TestTree/xine.jpg b/tests/TestTree/xine.jpg new file mode 100644 index 0000000..a89443b Binary files /dev/null and b/tests/TestTree/xine.jpg differ diff --git a/tests/TestTree/xine_snapshot-4.jpg b/tests/TestTree/xine_snapshot-4.jpg new file mode 100644 index 0000000..a89443b Binary files /dev/null and b/tests/TestTree/xine_snapshot-4.jpg differ diff --git a/tests/TestTree/xine_snapshot-9.jpg b/tests/TestTree/xine_snapshot-9.jpg new file mode 100644 index 0000000..5ef999b Binary files /dev/null and b/tests/TestTree/xine_snapshot-9.jpg differ