import java.io.*;
import java.util.*;

//import Html;

/**
 *  This type represents a directory and methods to
 *  display that.
 *
 *  @see FaqItem, Question, Answer
 *  @author http://bumble.sf.net
 */
 
  
public class Folder extends Object
{
  //--------------------------------------------
  private static String NEWLINE = System.getProperty("line.separator");
  //--------------------------------------------
  private boolean isGood;
  //--------------------------------------------
  private String errors;
  //--------------------------------------------
  private String directoryName;
  //--------------------------------------------
  public static String INDICATOR = "[dir";
  //--------------------------------------------
  public static String TERMINATOR = "]";

  public Folder()
  {
    this.errors = "";
    this.directoryName = "";
  } 

  //--------------------------------------------
  public Folder(String sText)
  {
    this();
    this.loadData(sText);
  } 

  //--------------------------------------------
  public String getAbsoluteName()
  {
    if (!this.isGood)
     { return ""; }

    File fDirectory = new File(this.directoryName);
    return fDirectory.getAbsolutePath();
  }

  //--------------------------------------------
  public String getCanonicalName()
  {
    if (!this.isGood)
     { return ""; }

    File fDirectory = new File(this.directoryName);
    try
    {
      return fDirectory.getCanonicalPath();
    }
    catch (IOException e)
    {
      return "";
    }
  }

  //--------------------------------------------
  public String getFolderName()
  {
    return this.directoryName;
  }

  //--------------------------------------------
  public void setFolderName(String sFolderName)
  {
    this.directoryName = sFolderName;
  }

  //--------------------------------------------
  public void loadData(String sText)
  {
    this.isGood = this.isFolder(sText);
    if (!this.isGood)
    {
      return;
    }

    this.directoryName = sText.trim().substring(
      this.INDICATOR.length(),
      sText.trim().length() - this.TERMINATOR.length()).trim();

    if (this.directoryName.equals(""))
    {
      this.directoryName = ".";
    }

    File fDirectory = new File(this.directoryName);

    if (!fDirectory.exists())
    {
      this.isGood = false;
      this.errors = "The directory does not exist";
      return;
    }

    if (!fDirectory.isDirectory())
    {
      this.isGood = false;
      this.errors = "The path indicated is not a directory";
      return;
    }

    if (!fDirectory.canRead())
    {
      this.isGood = false;
      this.errors = "The directory cannot be read";
      return;
    }

  } //-- m: loadData()

  //--------------------------------------------
  /** checks if this looks like a folder indicator */
  public static boolean isFolder(String sText)
  {
    if (sText.trim().length() == 0)
     { return false; }

    if (!sText.trim().startsWith(Folder.INDICATOR))
      { return false; }

    if (!sText.trim().endsWith(Folder.TERMINATOR))
      { return false; }

    return true;
  }

  
  //--------------------------------------------
  /** creates a string suitable for a plain text doc */
  public String toString()
  {
    StringBuffer sbReturn = new StringBuffer("");

    sbReturn.append(this.INDICATOR);
    sbReturn.append(" ");
    sbReturn.append(this.directoryName.toString());
    sbReturn.append(this.TERMINATOR);

    return sbReturn.toString();
  }

  //--------------------------------------------
  public String printHtml()
  {
    if (!this.isGood)
    {
      return "";
    }

    StringBuffer sbReturn = new StringBuffer("");

    File fFolder = new File(this.directoryName);
    //File fParent = fFolder.getParent();

    File fCurrentFile = new File("");
    String sDisplayName = new String();
    String sName = new String();
    String[] ssFiles = fFolder.list();

    sbReturn.append("<ul class='dir-list'>");

    for (int ii = 0; ii < ssFiles.length; ii++)
    {
      fCurrentFile = new File(ssFiles[ii]);

      if (fCurrentFile.isDirectory())
      {
        sDisplayName = fCurrentFile.getName() + "/";
      }
      else
      {
        sDisplayName = fCurrentFile.getName();
      }

      sbReturn.append("<li class='dir-item'><a href='");
      sbReturn.append(sDisplayName);
      sbReturn.append("'>");

      sbReturn.append(Html.encode(sDisplayName));
      sbReturn.append("</a></li>");
      sbReturn.append(NEWLINE);

    } //-- for

    sbReturn.append("</ul>");
    return sbReturn.toString();

  } //-- m: printHtml()

  //--------------------------------------------
  public String printReport()
  {
    StringBuffer sbReturn = new StringBuffer("");
    sbReturn.append("");
    sbReturn.append(NEWLINE);
    sbReturn.append("Directory name   >");
    sbReturn.append(this.directoryName);
    sbReturn.append(NEWLINE);
    sbReturn.append("Absolute name    >");
    sbReturn.append(this.getAbsoluteName());
    sbReturn.append(NEWLINE);
    sbReturn.append("Canonical name   >");
    sbReturn.append(this.getCanonicalName());
    sbReturn.append(NEWLINE);
    sbReturn.append("last data good   >");
    sbReturn.append(this.isGood);
    sbReturn.append(NEWLINE);
    sbReturn.append("error messages   >");
    sbReturn.append(this.errors);
    sbReturn.append(NEWLINE);
    return sbReturn.toString();
  }

  //--------------------------------------------
  public String print()
  {
    StringBuffer sbReturn = new StringBuffer("");
    return this.toString();
  }


  //--------------------------------------------
  /** a main method for testing */
  public static void main(String[] args) throws Exception
  {

    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("test usage: java Folder dir-text");
    sbUsageMessage.append(NEWLINE);
    sbUsageMessage.append(NEWLINE);

    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }

    StringBuffer sbTest = new StringBuffer("");

    Folder dirTest = new Folder(args[0]);
    System.out.println(dirTest.printHtml());

    System.out.println(NEWLINE);
    System.out.println(dirTest.toString());
    System.out.println(NEWLINE);
    System.out.println(dirTest.printReport());

  } //-- main()
  
} //-- FAQ class






