📄 virtualdirectory.java
字号:
package ranab.io;
import java.io.File;
import java.io.Writer;
import java.io.IOException;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.StringTokenizer;
import ranab.io.FileRegularFilter;
import ranab.util.DateUtils;
/**
* This class is responsible to handle all virtual directory activities.
*
* @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
*/
public
class VirtualDirectory implements Serializable {
private final static String NEWLINE = "\r\n";
private final static String DELIM = " ";
private String mstRoot = "/";
private String mstCurrDir = "/";
private boolean mbWritePerm = false;
/**
* Default constructor does nothing
*/
public VirtualDirectory() {
}
/**
* Set write permission.
*/
public void setWritePermission(boolean perm) {
mbWritePerm = perm;
}
/**
* Set root directory. Root directory string will always
* end with '/'.
*/
public void setRootDirectory(File root) {
if(root == null) {
root = new File("/");
}
mstRoot = normalizeSeparateChar(root.getAbsolutePath());
// if not ends with '/' - add one
if(mstRoot.charAt(mstRoot.length()-1) != '/') {
mstRoot = mstRoot + '/';
}
mstCurrDir = "/";
}
/**
* Set root directory.
*/
public void setRootDirectory(String root) throws IOException {
//File rootFile = new File(root).getCanonicalFile();
//setRootDirectory(rootFile);
mstRoot = normalizeSeparateChar(root);
// if not ends with '/' - add one
if(mstRoot.charAt(mstRoot.length()-1) != '/') {
mstRoot = mstRoot + '/';
}
mstCurrDir = "/";
}
/**
* Get write permission in this system
*/
public boolean getWritePermission() {
return mbWritePerm;
}
/**
* Get current working directory.
*/
public String getCurrentDirectory() {
return mstCurrDir;
}
/**
* Get root directory.
*/
public String getRootDirectory() {
return mstRoot;
}
/**
* Get physical name (wrt the machine root).
*/
public String getPhysicalName(String virtualName) {
virtualName = normalizeSeparateChar(virtualName);
return replaceDots(virtualName);
}
/**
* Get virtual name (wrt the virtual root).
* The return value will never end with '/' unless it is '/'.
*/
public String getAbsoluteName(String virtualName) {
virtualName = normalizeSeparateChar(virtualName);
String physicalName = replaceDots(virtualName);
String absoluteName = physicalName.substring(mstRoot.length()-1).trim();
return removeLastSlash(absoluteName);
}
/**
* Get virtual name (wrt the virtual root). The virtual
* name will never end with '/' unless it is '/'.
*/
public String getVirtualName(String physicalName) {
physicalName = normalizeSeparateChar(physicalName);
if (!physicalName.startsWith(mstRoot)) {
return null;
}
String virtualName = physicalName.substring(mstRoot.length()-1).trim();
return removeLastSlash(virtualName);
}
/**
* Change directory. The current directory will never have '/'
* at the end unless it is '/'.
* @param dirName change the current working directory.
* @return true if success
*/
public boolean changeDirectory(String virtualDir) {
String physcialDir = getPhysicalName(virtualDir);
if (physcialDir.equals("")) {
physcialDir = "/";
}
File dirFl = new File(physcialDir);
if (dirFl.exists() && dirFl.isDirectory()) {
mstCurrDir = physcialDir.substring(mstRoot.length() - 1).trim();
mstCurrDir = removeLastSlash(mstCurrDir);
return true;
}
return false;
}
/**
* Check read permission.
*/
public boolean hasReadPermission(String fileName, boolean bPhysical) {
if(bPhysical) {
fileName = normalizeSeparateChar(fileName);
}
else {
fileName = getPhysicalName(fileName);
}
if(!fileName.startsWith(mstRoot)) {
return false;
}
return new File(fileName).canRead();
}
/**
* Chech file write/delete permission.
*/
public boolean hasWritePermission(String fileName, boolean bPhysical) {
// no write permission
if(!mbWritePerm) {
return false;
}
// if virtual name - get the physical name
if(bPhysical) {
fileName = normalizeSeparateChar(fileName);
}
else {
fileName = getPhysicalName(fileName);
}
if(!fileName.startsWith(mstRoot)) {
return false;
}
return new File(fileName).canWrite();
}
/**
* Check file create permission.
*/
public boolean hasCreatePermission(String fileName, boolean bPhysical) {
// no write permission
if(!mbWritePerm) {
return false;
}
// if virtual name - get the physical name
if(bPhysical) {
fileName = normalizeSeparateChar(fileName);
}
else {
fileName = getPhysicalName(fileName);
}
return fileName.startsWith(mstRoot);
}
/**
* Print file list. Detail listing.
* <pre>
* -a : display all (including hidden files)
* </pre>
* @return true if success
*/
public boolean printList(String argument, Writer out) throws IOException {
String lsDirName = "./";
String options = "";
String pattern = "*";
// get options, directory name and pattern
if(argument != null) {
argument = argument.trim();
StringBuffer optionsSb = new StringBuffer(4);
StringTokenizer st = new StringTokenizer(argument, " ");
while(st.hasMoreTokens()) {
String token = st.nextToken();
if(token.charAt(0) == '-') {
if (token.length() > 1) {
optionsSb.append(token.substring(1));
}
}
else {
lsDirName = token;
}
}
options = optionsSb.toString();
}
// check options
boolean bAll = options.indexOf('a') != -1;
boolean bDetail = options.indexOf('l') != -1;
// check pattern
lsDirName = getPhysicalName(lsDirName);
int slashIndex = lsDirName.lastIndexOf('/');
if( (slashIndex != -1) && (slashIndex != (lsDirName.length() -1)) ) {
pattern = lsDirName.substring(slashIndex+1);
lsDirName = lsDirName.substring(0, slashIndex+1);
}
// check directory
File lstDirObj = new File(lsDirName);
if(!lstDirObj.exists()) {
return false;
}
if(!lstDirObj.isDirectory()) {
return false;
}
// get file list
File flLst[];
if ( (pattern == null) || pattern.equals("*") || pattern.equals("") ) {
flLst = lstDirObj.listFiles();
}
else {
flLst = lstDirObj.listFiles(new FileRegularFilter(pattern));
}
// print file list
if(flLst != null) {
for(int i=0; i<flLst.length; i++) {
if ( (!bAll) && flLst[i].isHidden() ) {
continue;
}
printLine(flLst[i], out);
out.write(NEWLINE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -