dfs 2004/01/06 11:53:58
Modified: net/src/java/org/apache/commons/net/ftp
DefaultFTPFileListParser.java
FTPFileEntryParser.java FTPFileListParser.java
Log:
Deprecated FTPFileListParser and copied its parserFileList method to
the FTPFileEntryParser interface. This is not an irreversible change,
but is consistent with what we've been discussing on the mailing list.
We can always back it out if need be. I also deleted all of the code
for DefaultFTPFileListParser and changed the class to extend
UnixFTPEntryParser. This change is in accordance with Jeffrey Brekke's
suggestion that we not maintain multiple implementations of the same thing.
DefaultFTPFileListParser is deprecated and will be phased out by version 2.0.
Revision Changes Path
1.9 +5 -362
jakarta-commons/net/src/java/org/apache/commons/net/ftp/DefaultFTPFileListParser.java
Index: DefaultFTPFileListParser.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/ftp/DefaultFTPFileListParser.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- DefaultFTPFileListParser.java 5 Jan 2004 22:11:12 -0000 1.8
+++ DefaultFTPFileListParser.java 6 Jan 2004 19:53:58 -0000 1.9
@@ -61,6 +61,8 @@
import java.util.Calendar;
import java.util.Vector;
+import org.apache.commons.net.ftp.parser.UnixFTPEntryParser;
+
/**
* DefaultFTPFileListParser is the default implementation of
* <a href="org.apache.commons.net.ftp.FTPFileListParser.html"> FTPFileListParser
</a>
@@ -70,6 +72,8 @@
* case you would create your own implementation of FTPFileListParser and
* if necessary, subclass FTPFile.
* <p>
+ * As of version 1.2, this class merely extends UnixFTPEntryParser.
+ * It will be removed in version 2.0.
* <p>
* @author Daniel F. Savarese
* @see FTPFileListParser
@@ -78,370 +82,9 @@
* @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
* @deprecated use autodetect mechanism in DefaultFTPFileEntryParserFactory instead
*/
-public final class DefaultFTPFileListParser implements FTPFileListParser
+public final class DefaultFTPFileListParser extends UnixFTPEntryParser
{
- // end is one beyond end
- private int __charArrayToInt(char[] arr, int start, int end)
- {
- int value = 0, decimal;
- decimal = 1;
- while (end-- > start)
- {
- value += (decimal * (arr[end] - '0'));
- decimal *= 10;
- }
- return value;
- }
-
- private long __charArrayToLong(char[] arr, int start, int end)
- {
- long value = 0, decimal;
- decimal = 1;
- while (end-- > start)
- {
- value += (decimal * (arr[end] - '0'));
- decimal *= 10;
- }
- return value;
- }
-
- private int __skipWhitespace(char[] cToken, int start)
- {
- while (start < cToken.length && Character.isWhitespace(cToken[start]))
- ++start;
- return start;
- }
-
- private int __skipDigits(char[] cToken, int start)
- {
- while (start < cToken.length && Character.isDigit(cToken[start]))
- ++start;
- return start;
- }
-
- private int __skipNonWhitespace(char[] cToken, int start)
- {
- while (start < cToken.length && !Character.isWhitespace(cToken[start]))
- ++start;
- return start;
- }
-
- private int __skipNonWhitespaceToLower(char[] cToken, int start)
- {
- while (start < cToken.length && !Character.isWhitespace(cToken[start]))
- {
- cToken[start] = Character.toLowerCase(cToken[start]);
- ++start;
- }
- return start;
- }
-
-
- /***
- * Parses an FTP server listing entry (a single line) and returns an
- * FTPFile instance with the resulting information. If the entry could
- * not be parsed, returns null.
- * <p>
- * @param entry A single line of an FTP server listing with the
- * end of line truncated.
- * @return An FTPFile instance representing the file information. If
- * the entry could not be parsed, returns null.
- ***/
- public FTPFile parseFTPEntry(String entry)
- {
- int access, start, end, type, month, year, hour, minutes;
- boolean isDevice;
- Calendar date;
- String sToken;
- char cToken[];
- FTPFile file;
-
- try
- {
- cToken = entry.toCharArray();
-
- file = new FTPFile();
- file.setRawListing(entry);
-
- isDevice = (cToken[0] == 'b' || cToken[0] == 'c');
-
- switch (cToken[0])
- {
- case 'd':
- type = FTPFile.DIRECTORY_TYPE;
- break;
- case 'l':
- type = FTPFile.SYMBOLIC_LINK_TYPE;
- break;
- default:
- type = FTPFile.FILE_TYPE;
- break;
- }
-
- file.setType(type);
-
- for (access = 0, start = 1; access < 3; access++)
- {
- // We use != '-' so we avoid having to check for suid and sticky
bits
- file.setPermission(access, FTPFile.READ_PERMISSION,
- (cToken[start++] != '-'));
- file.setPermission(access, FTPFile.WRITE_PERMISSION,
- (cToken[start++] != '-'));
- file.setPermission(access, FTPFile.EXECUTE_PERMISSION,
- (cToken[start++] != '-'));
- }
-
- start = __skipWhitespace(cToken, start);
- end = __skipDigits(cToken, start);
- file.setHardLinkCount(__charArrayToInt(cToken, start, end));
-
- start = __skipWhitespace(cToken, end);
- end = __skipNonWhitespace(cToken, start);
- // Get user and group
- file.setUser(new String(cToken, start, end - start));
-
- start = __skipWhitespace(cToken, end);
- end = __skipNonWhitespace(cToken, start);
- file.setGroup(new String(cToken, start, end - start));
-
- // Get size, if block or character device, set size to zero and skip
- // next two tokens.
- if (isDevice)
- {
- start = __skipWhitespace(cToken, end);
- end = __skipNonWhitespace(cToken, start);
- start = __skipWhitespace(cToken, end);
- end = __skipNonWhitespace(cToken, start);
- // Don't explicitly set size because it is zero by default
- }
- else
- {
- start = __skipWhitespace(cToken, end);
- end = __skipDigits(cToken, start);
- file.setSize(__charArrayToLong(cToken, start, end));
- }
-
- start = __skipWhitespace(cToken, end);
- end = __skipNonWhitespaceToLower(cToken, start);
-
- // Get month
- switch (cToken[start])
- {
- case 'a':
- if (cToken[end - 1] == 'r')
- month = 3;
- else
- month = 7;
- break;
- case 'd':
- month = 11;
- break;
- case 'f':
- month = 1;
- break;
- case 'j':
- if (cToken[end - 1] == 'l')
- month = 6;
- else if (cToken[start + 1] == 'a')
- month = 0;
- else
- month = 5;
- break;
- case 'm':
- if (cToken[end - 1] == 'y')
- month = 4;
- else
- month = 2;
- break;
- case 'n':
- month = 10;
- break;
- case 'o':
- month = 9;
- break;
- case 's':
- month = 8;
- break;
- default:
- month = 0;
- break;
- }
-
- // Get day, and store in access
- start = __skipWhitespace(cToken, end);
- end = __skipDigits(cToken, start);
- access = __charArrayToInt(cToken, start, end);
-
- start = __skipWhitespace(cToken, end);
- end = __skipDigits(cToken, start);
-
- date = Calendar.getInstance();
-
- try
- {
- // If token contains a :, it must be a time, otherwise a year
- if (cToken[end] == ':')
- {
- year = date.get(Calendar.YEAR);
- hour = date.get(Calendar.MONTH);
- if (hour < month)
- --year;
- hour = __charArrayToInt(cToken, start, end);
- start = end + 1;
- end = __skipDigits(cToken, start);
- minutes = __charArrayToInt(cToken, start, end);
- }
- else
- {
- // Have to set minutes or compiler will complain not initialized
- hour = minutes = -1;
- year = __charArrayToInt(cToken, start, end);
- }
-
- date.clear();
- date.set(Calendar.YEAR, year);
- date.set(Calendar.MONTH, month);
- date.set(Calendar.DATE, access);
-
- if (hour != -1)
- {
- date.set(Calendar.HOUR, hour);
- date.set(Calendar.MINUTE, minutes);
- }
-
- }
- catch (IllegalArgumentException e)
- {
- // Do nothing
- }
-
- file.setTimestamp(date);
-
- // This is dangerous, but we're going to assume there is only one
- // space after the date. Most servers seem to use that format and
- // we need to be able to preserve leading spacesin filenames.
- //start = __skipWhitespace(cToken, end);
- start = end + 1;
- end = __skipNonWhitespace(cToken, start);
-
- if (end >= cToken.length)
- {
- file.setName(new String(cToken, start, end - start));
- return file;
- }
-
- // Now we have to deal with the possibilities of symbolic links and
- // filenames with spaces. The filename and/or link may contain
- // spaces, numbers, or appear like the date entry, group, etc.,
-
- sToken = new String(cToken, start, cToken.length - start);
-
- if (type == FTPFile.SYMBOLIC_LINK_TYPE)
- {
- end = sToken.indexOf(" -> ");
- // Give up if no link indicator is present
- if (end == -1)
- {
- file.setName(sToken);
- return file;
- }
-
- file.setName(sToken.substring(0, end));
- file.setLink(sToken.substring(end + 4));
- return file;
- }
-
- // For other cases, just take the entire token
-
- file.setName(sToken);
- }
- catch (ArrayIndexOutOfBoundsException e)
- {
- return null;
- }
- catch (StringIndexOutOfBoundsException e)
- {
- return null;
- }
-
- return file;
- }
-
-
- /***
- * Parses an FTP server file listing and converts it into a usable format
- * in the form of an array of <code> FTPFile </code> instances. If the
- * file list contains no files, <code> null </code> is returned, otherwise
- * an array of <code> FTPFile </code> instances representing the files in
- * the directory is returned.
- * <p>
- * @param listStream The InputStream from which the file list should be
- * read.
- * @return The list of file information contained in the given path. null
- * if the list could not be obtained or if there are no files in
- * the directory.
- * @exception IOException If an I/O error occurs reading the listStream.
- ***/
- public FTPFile[] parseFileList(InputStream listStream) throws IOException
- {
- String line;
- Vector results;
- BufferedReader reader;
- FTPFile entry;
-
- reader = new BufferedReader(new InputStreamReader(listStream));
-
- if ((line = reader.readLine()) == null)
- {
- results = null;
- }
- else
- {
- results = new Vector();
-
- // This is to handle a line at the beginning of the listing
- // that says "total xx" or "Gemstat xx" or something else.
- if (line.toLowerCase().startsWith("total"))
- line = reader.readLine();
- else
- {
- if ((entry = parseFTPEntry(line)) != null)
- results.addElement(entry);
- line = reader.readLine();
- }
-
- while (line != null)
- {
- if (line.length() == 0 || (entry = parseFTPEntry(line)) == null)
- {
- results = null;
- break;
- }
- results.addElement(entry);
- line = reader.readLine();
- }
- }
-
- // Finish reading from stream just in case
- if (line != null)
- while ((line = reader.readLine()) != null)
- ;
-
- reader.close();
-
- if (results != null)
- {
- FTPFile[] result;
-
- result = new FTPFile[results.size()];
- if (result.length > 0)
- results.copyInto(result);
- return result;
- }
-
- return null;
- }
}
1.10 +18 -1
jakarta-commons/net/src/java/org/apache/commons/net/ftp/FTPFileEntryParser.java
Index: FTPFileEntryParser.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/ftp/FTPFileEntryParser.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- FTPFileEntryParser.java 5 Jan 2004 22:29:21 -0000 1.9
+++ FTPFileEntryParser.java 6 Jan 2004 19:53:58 -0000 1.10
@@ -56,6 +56,7 @@
import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
/**
* FTPFileEntryParser defines the interface for parsing a single FTP file
@@ -160,4 +161,20 @@
* @exception IOException thrown on any IO Error reading from the reader.
*/
String readNextEntry(BufferedReader reader) throws IOException;
+
+ /***
+ * Parses an FTP server file listing and converts it into a usable format
+ * in the form of an array of <code> FTPFile </code> instances. If the
+ * file list contains no files, <code> null </code> should be
+ * returned, otherwise an array of <code> FTPFile </code> instances
+ * representing the files in the directory is returned.
+ * <p>
+ * @param listStream The InputStream from which the file list should be
+ * read.
+ * @return The list of file information contained in the given path. null
+ * if the list could not be obtained or if there are no files in
+ * the directory.
+ * @exception IOException If an I/O error occurs reading the listStream.
+ ***/
+ FTPFile[] parseFileList(InputStream listStream) throws IOException;
}
1.7 +2 -0
jakarta-commons/net/src/java/org/apache/commons/net/ftp/FTPFileListParser.java
Index: FTPFileListParser.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/ftp/FTPFileListParser.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- FTPFileListParser.java 2 Jan 2004 03:39:04 -0000 1.6
+++ FTPFileListParser.java 6 Jan 2004 19:53:58 -0000 1.7
@@ -69,6 +69,8 @@
* @author Daniel F. Savarese
* @see FTPFile
* @see FTPClient#listFiles
+ * @deprecated This interface is deprecated as of version 1.2 and will be
+ * removed in version 2.0.
***/
public interface FTPFileListParser
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]