FileIterator fixed and test files.

This commit is contained in:
Christian P. MOMON 2010-06-21 03:15:31 +02:00
parent 36f42f9342
commit 4e522c4576
19 changed files with 264 additions and 363 deletions

View file

@ -17,7 +17,7 @@ public class FileIterator extends Vector<FileIteratorState> 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<FileIteratorState> 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<FileIteratorState> 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<FileIteratorState> 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<FileIteratorState> 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<FileIteratorState> implements Iterator<
}
}
}
}
}
/**
*
@ -232,10 +344,9 @@ public class FileIterator extends Vector<FileIteratorState> 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<FileIteratorState> 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);
}
/**
*
*/

View file

@ -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<File>
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<File>
/**
*
*/
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<File>
boolean result;
if (this.currentIndex >= this.files.length)
if (this.currentFile() == null)
{
result = false;
}
@ -149,7 +140,7 @@ public class FileIteratorState implements Iterator<File>
result = this.currentFile();
this.currentIndex += 1;
//
return(result);
}

View file

@ -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();
}
}

View file

@ -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 ("<div id='name'>a name</div >", datas, errorMessage);
System.out.println (check ("only content change", html, "<div id=\"name\">Superman</div>"));
// 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 ("<div id='lastname'>a last name</div >", datas, errorMessage);
System.out.println (check ("content and attributes", html, "<div id=\"lastname\" style=\"background: blue;foreground: red;\" class=\"nameClass\">Spiderman</div>"));
// 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 ("<ul>\n <li id='words'>a word</li>\n</ul>", datas, errorMessage);
System.out.println (check ("list assertion 1", html, "<li id=\"words_0\">alpha</li>"));
System.out.println (check ("list assertion 2", html, "<li id=\"words_1\">bravo</li>"));
System.out.println (check ("list assertion 3", html, "<li id=\"words_2\">charlie</li>"));
System.out.println (check ("list assertion 4", html, "<li id=\"words_3\">delta</li>"));
System.out.println (check ("list assertion 5", html, "<li id=\"words_4\">echo</li>"));
System.out.println (check ("list assertion 6", html, "<li id=\"words_5\">fox</li>"));
// 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 ("<table>\n");
source.append (" <tr id='identity'><td>noid</td><td id='nom'>un nom</td><td id='prenom'>un prenom</td></tr>\n");
source.append ("</table>");
htmlSource = source.toString ();
html = StringPresenter.doXid (htmlSource, datas, errorMessage);
System.out.println (check ("table 1 assertion 1", html, "<tr id=\"identity_0\"><td>noid</td><td id=\"nom_0\">Jemba</td><td id=\"prenom_0\">Epo</td></tr>"));
System.out.println (check ("table 1 assertion 2", html, "<tr id=\"identity_1\"><td>noid</td><td id=\"nom_1\">Momon</td><td id=\"prenom_1\">Christian</td></tr>"));
System.out.println (check ("table 1 assertion 3", html, "<tr id=\"identity_2\"><td>noid</td><td id=\"nom_2\">Tronche</td><td id=\"prenom_2\">Christophe</td></tr>"));
/*
// 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 ("<table>\n");
source.append (" <tr id='identity'><td>noid</td><td id='nom'>un nom</td><td id='prenom'>un prenom</td></tr>\n");
source.append (" <tr id='identity2'><td>noid</td><td id='nom2'>un nom</td><td id='prenom2'>un prenom</td></tr>\n");
source.append (" <tr><td>noid</td><td id='nom3'>un nom</td><td id='prenom3'>un prenom</td></tr>\n");
source.append ("</table>");
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 ("<div>", "class", "aDivClass");
datas.setAttribute ("<div>", "style", "background-color: #000000;");
datas.setAttribute ("number", "style", "background-color: #0000FF;");
errorMessage = new StringBuffer ();
source = new StringBuffer ();
source.append ("<div>\n");
source.append (" <h1>one</h1>\n");
source.append ("</div>\n");
source.append ("<div id=\"number\">\n");
source.append (" <h1>three</h1>\n");
source.append ("</div>");
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 (\"<div>\", \"class\", \"aDivClass\");");
System.out.println ("datas.setAttribute (\"<div>\", \"style\", \"background-color: #000000;\");");
System.out.println ("datas.setAttribute (\"number\", \"style\", \"background-color: #0000FF;\");");
System.out.println ("=>");
System.out.println (html);
*/
}
}

1
tests/TestTree/.test/arf Normal file
View file

@ -0,0 +1 @@
test sioux

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 869 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 853 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 798 KiB

View file

@ -0,0 +1 @@
azertyuiop

View file

@ -0,0 +1 @@
good weather today

View file

@ -0,0 +1 @@
Ceci est un test.

Binary file not shown.

After

Width:  |  Height:  |  Size: 771 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 869 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 853 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 798 KiB

BIN
tests/TestTree/xine.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB