/*
 * $Log: Dir2Xml.java,v $
 * Revision 1.4  2005-05-31 09:20:37  europe\L190409
 * added sort for files
 *
 */
package nl.nn.adapterframework.util;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Arrays;
/**
 * List the contents of a directory as XML.
 * @version Id
 *
 * @author Johan Verrips IOS
 */
public class Dir2Xml  {
	public static final String version="$RCSfile: Dir2Xml.java,v $ $Revision: 1.4 $ $Date: 2005-05-31 09:20:37 $";
	
  String path;
  String wildcard="*.*";
  public Dir2Xml() {
	  super();
  }
  public String getDirList() {
    WildCardFilter filter = new WildCardFilter(wildcard);
    File dir = new File(path);
    File files[] = dir.listFiles(filter);
    Arrays.sort(files);
    int count=(files==null ? 0 : files.length);
    XmlBuilder dirXml=new XmlBuilder("directory");
    dirXml.addAttribute("name", path);
    for (int i=0; i<count; i++ ) {
	    File file=files[i];
	    if (file.isDirectory()) {
		    continue;
	    }
	    dirXml.addSubElement(getFileAsXmlBuilder(file));
    }
    return dirXml.toXML();
  }
  private XmlBuilder getFileAsXmlBuilder(File file){
	  
	  XmlBuilder fileXml=new XmlBuilder("file");
	  fileXml.addAttribute("name", file.getName());
	  fileXml.addAttribute("size", ""+file.length());
	  try {
	  	fileXml.addAttribute("canonicalName", file.getCanonicalPath());
	  } catch (IOException e) { }
      // Get the modification date of the file
      Date modificationDate=new Date(file.lastModified());
	  //add date
	  String date=DateUtils.format(modificationDate, DateUtils.FORMAT_DATE);
	  fileXml.addAttribute("modificationDate", date);

      // add the time
      String time=DateUtils.format(modificationDate, DateUtils.FORMAT_TIME_HMS);
      fileXml.addAttribute("modificationTime", time);
      

	  return fileXml;
  }
  public static void main(String[] argv) {
    Dir2Xml dx=new Dir2Xml();
    dx.setPath("c:/temp");
    dx.setWildCard("log*.txt*");
    System.out.println(dx.getDirList());
  }
  public void setPath(String path) {
    this.path=path;
  }
  /**
   * Set a Wildcard
   * @see WildCardFilter
   */
  public void setWildCard(String wildcard) {
    this.wildcard=wildcard;

  }
}

@@ -40,17 +40,17 @@
 n 1.
-4
+5
   2005-0
 5-31
@@ -45,29 +45,29 @@
   2005-0
-5-31 09:20:37
+7-19 11:04:09
   europe
@@ -75,16 +75,145 @@
 L190409%0A
+ * use explicit FileNameComparator%0A *%0A * Revision 1.4  2005/05/31 09:20:37  Gerrit van Brakel %3Cgerrit.van.brakel@ibissource.org%3E%0A
  * added
@@ -570,17 +570,17 @@
 sion: 1.
-4
+5
  $ $Date
@@ -591,29 +591,38 @@
 05-0
-5-31 09:20:37 $%22;%0A%09%0A 
+7-19 11:04:09 $%22;%0A%09%0A  %09private
  Str
@@ -633,16 +633,25 @@
 path;%0A  
+%09private 
 String w
@@ -667,16 +667,19 @@
 *.*%22;%0A  
+%09%0A%09
 public D
@@ -690,18 +690,17 @@
 ml() %7B%0A%09
-  
+%09
 super();
@@ -696,27 +696,30 @@
 %0A%09%09super();%0A
+%09%7D%0A%09
   
-%7D
 %0A  public St
@@ -890,16 +890,42 @@
 rt(files
+, new FileNameComparator()
 );%0A    i
@@ -1262,16 +1262,19 @@
 ();%0A  %7D%0A
+  %0A
   privat
@@ -1986,17 +1986,16 @@
 %0A      %0A
-%0A
 %09  retur
@@ -2013,185 +2013,8 @@
   %7D%0A
-  public static void main(String%5B%5D argv) %7B%0A    Dir2Xml dx=new Dir2Xml();%0A    dx.setPath(%22c:/temp%22);%0A    dx.setWildCard(%22log*.txt*%22);%0A    System.out.println(dx.getDirList());%0A  %7D
 %0A  p
@@ -2071,16 +2071,19 @@
 th;%0A  %7D%0A
+  %0A
   /**%0A  

use explicit FileNameComparator