/**
* Copyright (C) 2003 Alexander Kout
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package de.kout.wlFxp.ftp;
import java.io.File;
import java.nio.*;
import java.nio.channels.*;
import de.kout.wlFxp.Utilities;
/**
* the class represents a ftp file
*
*@author Alexander Kout
*@created 30. M�rz 2002
*/
public class FtpFile {
String name = "", mode = "---", modified = "";
long size = 0;
/**
* size as a human readable String
*/
public String hSize = "0B";
String absolutePath = "";
boolean ftp = true;
boolean visible = true;
String date = "";
String parent = "";
String user, group;
/**
* Constructor for the FtpFile object
*
*@param name Description of Parameter
*/
public FtpFile(String name) {
this.name = name;
if (name.startsWith(".")) {
visible = false;
} else {
visible = true;
}
}
/**
* Constructor for the FtpFile object
*/
public FtpFile() { }
/**
* Gets the date attribute of the FtpFile object
*
*@return The date value
*/
public String getDate() {
return date;
}
/**
* Sets the date attribute of the FtpFile object
*
*@param dateString The new date value
*/
public void setDate(String dateString) {
date = dateString;
}
/**
* Sets the name attribute of the FtpFile object
*
*@param name The new name value
*/
public void setName(String name) {
this.name = name;
if (name.startsWith(".")) {
visible = false;
} else {
visible = true;
}
if (!absolutePath.equals("")) {
if (absolutePath.indexOf("/") != -1) {
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/") + 1) + name;
} else {
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("\\") + 1) + name;
}
}
}
/**
* Sets the size attribute of the FtpFile object
*
*@param size The new size value
*/
public void setSize(long size) {
this.size = size;
hSize = Utilities.humanReadable(size);
}
/**
* Sets the mode attribute of the FtpFile object
*
*@param mode The new mode value
*/
public void setMode(String mode) {
this.mode = mode;
}
/**
* Gets the name attribute of the FtpFile object
*
*@return The name value
*/
public String getName() {
return name;
}
/**
* Gets the size attribute of the FtpFile object
*
*@return The size value
*/
public long getSize() {
return size;
}
/**
* Gets the mode attribute of the FtpFile object
*
*@return The mode value
*/
public String getMode() {
return mode;
}
/**
* returns true if FtpFile is a real file
*
*@return The file value
*/
public boolean isFile() {
return mode.charAt(0) == '-';
}
/**
* return true if FtpFile is a directory
*
*@return The directory value
*/
public boolean isDirectory() {
return mode.charAt(0) == 'd';
}
/**
* return true if FtpFile is a link
*
*@return The link value
*/
public boolean isLink() {
return mode.charAt(0) == 'l';
}
/**
* Sets the absolutePath attribute of the FtpFile object
*
*@param absPath The new absolutePath value
*/
public void setAbsolutePath(String absPath) {
absolutePath = absPath;
if (absolutePath.indexOf("/") != -1) {
name = absolutePath.substring(absolutePath.lastIndexOf("/") + 1, absolutePath.length());
} else {
name = absolutePath.substring(absolutePath.lastIndexOf("\\") + 1, absolutePath.length());
}
if (ftp) {
parent = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
if (parent.equals("")) {
parent = "/";
}
} else {
parent = new File(absolutePath).getParent();
}
}
/**
* Gets the absolutePath attribute of the FtpFile object
*
*@return The absolutePath value
*/
public String getAbsolutePath() {
return absolutePath;
}
/**
* Gets the parent attribute of the FtpFile object
*
*@return The parent value
*/
public String getParent() {
return parent;
}
/**
* Sets the parent attribute of the FtpFile object
*
*@param s The new parent value
*/
public void setParent(String s) {
parent = s;
if (absolutePath.indexOf("/") != -1) {
if (parent.endsWith("/")) {
absolutePath = parent + name;
} else {
absolutePath = parent + "/" + name;
}
} else
if (parent.endsWith("\\")) {
absolutePath = parent + name;
} else {
absolutePath = parent + "\\" + name;
}
}
/**
* Sets the ftpMode attribute of the FtpFile object
*
*@param mode The new ftpMode value
*/
public void setFtpMode(boolean mode) {
ftp = mode;
if (!ftp && new File(getAbsolutePath()).isHidden()) {
visible = false;
}
}
/**
* Gets the ftpMode attribute of the FtpFile object
*
*@return The ftpMode value
*/
public boolean getFtpMode() {
return ftp;
}
/**
* if the Object is a local directory this method lists its content
*
*@return Description of the Return Value
*/
public FtpFile[] list() {
if (!getFtpMode()) {
String[] list = new File(getAbsolutePath()).list();
if (list == null)
return new FtpFile[0];
FtpFile[] files = new FtpFile[list.length];
for (int i = 0; i < files.length; i++) {
files[i] = new FtpFile(list[i]);
File f = new File(getAbsolutePath() + File.separator + list[i]);
files[i].setSize(f.length());
String first = (f.isFile()) ? "-" : "d";
String sec = (f.canRead()) ? "r" : "-";
String third = (f.canWrite()) ? "w" : "-";
files[i].setMode(first + sec + third);
files[i].setFtpMode(false);
files[i].setAbsolutePath(getAbsolutePath() + File.separator + files[i].getName());
}
return files;
}
return new FtpFile[0];
}
/**
* delete the FtpFile
*
*@return Description of the Return Value
*/
public boolean delete() {
if (!getFtpMode()) {
return new File(getAbsolutePath()).delete();
}
return true;
}
/**
* returns name
*
*@return Description of the Returned Value
*/
public String toString() {
return name;
}
/**
* returns mode + size + name
*
*@return Description of the Return Value
*/
public String toStringLong() {
String s = " ";
s += hSize;
s = s.substring(s.length() - 10, s.length());
return mode + " " + s + " " + name;
}
/**
* returns a string with all attributes of the Object separated by a tab
*
*@return Description of the Return Value
*/
public String export() {
return absolutePath + "\t" + size + "\t" + mode + "\t" + (ftp ? 1 : 0);
}
/**
* load a previous exported String as FtpFile
*
*@param s Description of the Parameter
*/
public void load(String s) {
String t[] = Utilities.split(s, "\t");
setFtpMode(t[3].equals("0") ? false : true);
setAbsolutePath(t[0]);
setSize(Long.parseLong(t[1]));
setMode(t[2]);
}
/**
* Gets the visible attribute of the FtpFile object
*
*@return The visible value
*/
public boolean isVisible() {
return visible;
}
}