jericho 01/03/09 03:07:45
Modified: src/webdav/client/src/org/apache/webdav/cmd Slide.java
Log:
- Add the commands like lcd, lpwd, lls using on the local system.
- Fix listing. Once getting an error, listing did't work properly.
- Make getting input robust.
Revision Changes Path
1.7 +216 -79
jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Slide.java
Index: Slide.java
===================================================================
RCS file:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Slide.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- Slide.java 2001/03/07 03:04:04 1.6
+++ Slide.java 2001/03/09 11:07:44 1.7
@@ -1,7 +1,7 @@
/*
- * $Header:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Slide.java,v 1.6
2001/03/07 03:04:04 jericho Exp $
- * $Revision: 1.6 $
- * $Date: 2001/03/07 03:04:04 $
+ * $Header:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Slide.java,v 1.7
2001/03/09 11:07:44 jericho Exp $
+ * $Revision: 1.7 $
+ * $Date: 2001/03/09 11:07:44 $
*
* ====================================================================
*
@@ -68,11 +68,14 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
+import java.util.Date;
import java.util.Stack;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.StringTokenizer;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.net.URL;
import java.net.MalformedURLException;
import org.apache.webdav.util.HttpURL;
@@ -116,6 +119,12 @@
/**
+ * The current path on the local system.
+ */
+ private File dir = new File(".");
+
+
+ /**
* The command prompt for the display information.
*/
private String commandPrompt = null;
@@ -205,10 +214,13 @@
WebdavStatus.SC_UNAUTHORIZED) {
System.out.print("UserName: ");
- String userName = in.readLine().trim();
+ String userName = in.readLine();
if (userName != null && userName.length() > 0) {
+ userName = userName.trim();
System.out.print("Password: ");
- String password = in.readLine().trim();
+ String password = in.readLine();
+ if (password != null)
+ password= password.trim();
try {
httpUrl.setUserInfo(userName, password);
if (webdavResource != null) {
@@ -245,7 +257,7 @@
String command = null;
do {
System.out.print(getPrompt());
- command = in.readLine().trim();
+ command = in.readLine();
if (command == null || command.length() == 0)
continue;
@@ -263,9 +275,13 @@
inputUrl = (String) params.pop();
} else {
System.out.print("Enter http URL: ");
- inputUrl = in.readLine().trim();
- if (inputUrl != null && inputUrl.length() == 0)
- continue;
+ inputUrl = in.readLine();
+ if (inputUrl != null) {
+ inputUrl = inputUrl.trim();
+ if (inputUrl.length() == 0)
+ // Do not go further.
+ continue;
+ }
}
try {
@@ -283,10 +299,13 @@
WebdavStatus.SC_UNAUTHORIZED) {
System.out.print("UserName: ");
- String userName = in.readLine().trim();
+ String userName = in.readLine();
if (userName != null && userName.length() > 0) {
+ userName = userName.trim();
System.out.print("Password: ");
- String password = in.readLine().trim();
+ String password = in.readLine();
+ if (password != null)
+ password= password.trim();
try {
try {
if (webdavResource != null)
@@ -336,6 +355,113 @@
todo.equalsIgnoreCase("?")) {
printSlideClientUsage();
} else
+ if (todo.equalsIgnoreCase("lpwd")) {
+ System.out.println(dir.getCanonicalPath());
+ } else
+ if (todo.equalsIgnoreCase("lcd")) {
+ if (params.size() == 0) {
+ System.out.println(dir.getCanonicalPath());
+ } else
+ if (params.size() == 1) {
+ String path = (String) params.pop();
+ File anotherDir = (path.startsWith("/") ||
+ path.startsWith("\\")) ? new File(path) :
+ new File(dir.getCanonicalPath(), path);
+
+ if (anotherDir.isDirectory()) {
+ dir = anotherDir;
+ } else {
+ System.err.println("Warning: Not found the path");
+ }
+ }
+ } else
+ if (todo.equalsIgnoreCase("lls") ||
+ todo.equalsIgnoreCase("ldir")) {
+ String path = null;
+ // set default option.
+ char option = 'F';
+ while (!params.empty()) {
+ String token = (String) params.pop();
+ // parse the input token.
+ if (token != null) {
+ if (!token.startsWith("-")) {
+ path = token;
+ } else {
+ if (token.indexOf('l') > 0)
+ option = 'l';
+ }
+ }
+ }
+
+ File temp = (path != null) ? new File(dir, path) : dir;
+ if (!temp.exists()) {
+ System.err.println("Warning: Not found the path");
+ continue;
+ }
+
+ String[] list = temp.list();
+ // TODO: consider of options like '-a' for all and so on.
+ switch (option) {
+ case 'l':
+ for (int i = 0; i < list.length; i++) {
+ String s = list[i];
+ File file = new File(temp, s);
+ for (int j = 0; j < 4; j++) {
+ switch (j) {
+ case 0:
+ // Print the filename.
+ System.out.print(s);
+ for (int k = list[i].length();
+ k < 35; k++)
+ System.out.print(SP);
+ break;
+ case 1:
+ s = Long.toString(file.length());
+ for (int k = 10 - s.length();
+ k > 0 ; k--)
+ System.out.print(SP);
+ // don't cut the size.
+ System.out.print(s + SP);
+ break;
+ case 2:
+ // cut the description.
+ s = file.isDirectory() ?
+ "DIR" : "";
+ System.out.print(SP +
+ ((s.length() > 5) ?
+ s.substring(0, 5) : s));
+ for (int k = s.length(); k < 5; k++)
+ System.out.print(SP);
+ break;
+ case 3:
+ s = new SimpleDateFormat().format(
+ new Date(file.lastModified()));
+ System.out.print(SP + s);
+ default:
+ }
+ }
+ // Start with a new line.
+ System.out.println();
+ }
+ break;
+ case 'F':
+ int i = 0;
+ for (; i < list.length; i++) {
+ System.out.print(list[i] + SP);
+ for (int j = list[i].length();
+ j < 25; j++) {
+ System.out.print(SP);
+ }
+ if (i % 3 == 2)
+ System.out.println();
+ }
+ if (list.length > 0 && i % 3 != 0) {
+ System.out.println();
+ }
+ break;
+ default:
+ } // end of switch
+ } else
if (todo.equalsIgnoreCase("options")) {
int count = params.size();
String param = null;
@@ -441,18 +567,23 @@
webdavResource.setPath(cdPath);
if (webdavResource.exists()) {
setPath(webdavResource.getPath());
+ } else {
+ System.err.println
+ ("Warning: Not found the path");
}
} catch (WebdavException we) {
if (we.getStatusCode() ==
WebdavStatus.SC_UNAUTHORIZED) {
System.out.print("UserName: ");
- String userName = in.readLine().trim();
+ String userName = in.readLine();
if (userName != null &&
userName.length() > 0) {
-
+ userName = userName.trim();
System.out.print("Password: ");
- String password = in.readLine().trim();
+ String password = in.readLine();
+ if (password != null)
+ password= password.trim();
try {
httpUrl.setUserInfo(userName,
password);
@@ -477,7 +608,8 @@
+ we.getMessage());
}
} catch (IOException e) {
- System.err.println("Error: check! " + e.getMessage());
+ System.err.println(
+ "Error: check! " + e.getMessage());
httpUrl = null;
}
updatePrompt(getPath());
@@ -486,14 +618,13 @@
if (todo.equalsIgnoreCase("ls") ||
todo.equalsIgnoreCase("dir")) {
- String token = null;
String path = null;
// set default options.
char[] options = { '-', 'F' };
while (!params.empty()) {
// get the input token.
- token = (String) params.pop();
+ String token = (String) params.pop();
// parse the input token.
if (token != null) {
if (!token.startsWith("-")) {
@@ -504,6 +635,33 @@
}
}
}
+ try {
+ // Check that the path is ok.
+ if (path != null) {
+ webdavResource.setPath(path);
+ } else {
+ path = checkUri("./");
+ webdavResource.setPath(path);
+ }
+ if (webdavResource.exists()) {
+ setPath(webdavResource.getPath());
+ } else {
+ System.err.println
+ ("Warning: Not found the path");
+ }
+ } catch (WebdavException we) {
+ if (webdavResource.getStatusCode() ==
+ WebdavStatus.SC_METHOD_NOT_ALLOWED) {
+ System.err.println
+ ("Warning: Not WebDAV-enabled?");
+ } else {
+ System.err.println("failed. " +
+ webdavResource.getStatusMessage());
+ }
+ // Gotten error, do not go further!
+ continue;
+ }
+ // FIXME: do not loop for the repeated options.
for (int o = 1; o < options.length; o++) {
switch (options[o]) {
case 'l':
@@ -554,46 +712,19 @@
}
break;
case 'F':
- try {
- if (path != null)
- webdavResource.setPath(path);
- // FIXME: the problem, once it has an error
- if (webdavResource.exists()) {
- String[] list = webdavResource.list();
- int i = 0;
- for (; i < list.length; i++) {
- System.out.print(list[i] + SP);
- for (int j = list[i].length();
- j < 25; j++) {
- System.out.print(SP);
- }
- if (i % 3 == 2)
- System.out.println();
- }
- if (list.length > 0 && i % 3 != 0)
- System.out.println();
- } else {
- if (webdavResource.getStatusCode() ==
- WebdavStatus.SC_NOT_FOUND) {
- System.err.println
- ("Warning: Not found the path");
- } else
- if (webdavResource.getStatusCode() ==
- WebdavStatus.SC_METHOD_NOT_ALLOWED) {
- System.err.println
- ("Warning: Not WebDAV-enabled?");
- } else {
- System.out.println("failed. " +
- webdavResource.getStatusMessage());
- }
+ String[] list = webdavResource.list();
+ int i = 0;
+ for (; i < list.length; i++) {
+ System.out.print(list[i] + SP);
+ for (int j = list[i].length();
+ j < 25; j++) {
+ System.out.print(SP);
}
- } catch (WebdavException we) {
- System.err.println
- ("Error: Couldn't list the collection");
- } catch (IOException e) {
- System.err.println("Error: check! " +
- e.getMessage());
+ if (i % 3 == 2)
+ System.out.println();
}
+ if (list.length > 0 && i % 3 != 0)
+ System.out.println();
break;
default:
} // end of switch
@@ -623,8 +754,8 @@
propertyValues.nextElement());
}
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -650,8 +781,8 @@
System.out.println
("�� WebDAV dead property doesn't work.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -678,8 +809,8 @@
if (webdavResource.putMethod(dest, file)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} else
@@ -716,17 +847,17 @@
if (file.exists()) {
System.out.print("Aleady exists. " +
"Do you want to overwrite it(Y/n)? ");
- y = in.readLine().trim();
+ y = in.readLine();
}
- if (y.equalsIgnoreCase("y") ||
+ if (y.trim().equalsIgnoreCase("y") ||
(y != null && y.length() == 0)) {
System.out.print("Downloading '" + src +
"' to '" + dest + "': ");
if (webdavResource.getMethod(src, file)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
}
@@ -749,8 +880,8 @@
if (webdavResource.deleteMethod(path)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -772,8 +903,8 @@
if (webdavResource.mkcolMethod(path)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -795,8 +926,8 @@
if (webdavResource.copyMethod(src, dst)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -818,8 +949,8 @@
if (webdavResource.moveMethod(src, dst)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -838,8 +969,8 @@
if (webdavResource.lockMethod(path)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -858,8 +989,8 @@
if (webdavResource.unlockMethod(path)) {
System.out.println("succeeded.");
} else {
- System.out.println("failed.");
- System.out.println(
+ System.err.println("failed.");
+ System.err.println(
webdavResource.getStatusMessage());
}
} catch (WebdavException we) {
@@ -1043,6 +1174,12 @@
"Exit Slide");
System.out.println(" help " +
"Print this help message");
+ System.out.println(" lpwd " +
+ "Print local working directory");
+ System.out.println(" lcd [path] " +
+ "Change local working directory");
+ System.out.println(" lls [-lF] [path] " +
+ "List contents of local directory");
System.out.println(" options [abs_path|http_URL] " +
"Print available http methods");
System.out.println(" url " +
@@ -1075,9 +1212,9 @@
"Lock specified resource");
System.out.println(" unlock path " +
"Unlock specified resource");
- System.out.println("\nAliases: help=?, ls=dir, pwc=pwd, cc=cd, " +
+ System.out.println("Aliases: help=?, ls=dir, pwc=pwd, cc=cd, " +
"copy=cp, move=mv, delete=del=rm,\n mkcol=mkdir, " +
- "exit=quit");
+ "exit=quit, lls=ldir\n");
}
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... remm
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... juergen
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... remm
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... jericho
- cvs commit: jakarta-slide/src/webdav/client/src/org/apache/we... remm
