dfs 2003/07/28 19:35:35 Modified: net/src/java/org/apache/commons/net/ftp/parser VMSFTPEntryParser.java net/src/test/org/apache/commons/net/ftp/parser VMSFTPEntryParserTest.java Log: Applied patch from Stephane Este-Gracias that fixes the parsing of VMS listings. I verified that it would compile and pass its tests. I also visually inspected the code, but did not perform a deep examination. On the surface, everything looks okay. PR: 20796 Submitted by: [EMAIL PROTECTED] (Stephane ESTE-GRACIAS) Reviewed by: dfs Revision Changes Path 1.6 +95 -10 jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/VMSFTPEntryParser.java Index: VMSFTPEntryParser.java =================================================================== RCS file: /home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/VMSFTPEntryParser.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- VMSFTPEntryParser.java 18 May 2003 04:03:17 -0000 1.5 +++ VMSFTPEntryParser.java 29 Jul 2003 02:35:34 -0000 1.6 @@ -55,8 +55,13 @@ */ import java.util.Calendar; +import java.util.HashMap; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPFileListParserImpl; @@ -112,6 +117,7 @@ * * @author <a href="[EMAIL PROTECTED]">Winston Ojeda</a> * @author <a href="mailto:[EMAIL PROTECTED]">Steve Cohen</a> + * @author <a href="[EMAIL PROTECTED]">Stephane ESTE-GRACIAS</a> * @version $Id$ */ public class VMSFTPEntryParser extends FTPFileListParserImpl @@ -139,8 +145,8 @@ + MONTHS + "-([0-9]{4})\\s*" + "((?:[01]\\d)|(?:2[0-3])):([012345]\\d):([012345]\\d)\\s*" - + "\\[([0-9$A-Za-z_]+),([0-9$a-zA-Z_]+)\\]\\s*" - + "(\\([a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*\\))"; + + "\\[(([0-9$A-Za-z_]+)|([0-9$A-Za-z_]+),([0-9$a-zA-Z_]+))\\]\\s*" + + "\\([a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*,[a-zA-Z]*\\)"; /** @@ -156,7 +162,8 @@ { this(false); } - + + /** * Constructor for a VMSFTPEntryParser object. Sets the versioning member * to the supplied value. @@ -174,6 +181,68 @@ this.versioning = versioning; } + + /*** + * 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. + ***/ + public FTPFile[] parseFileList(InputStream listStream) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(listStream)); + String listing = null; + FTPFile[] files; + + String line = reader.readLine(); + while (line != null) { + if ((line.trim().equals("")) || + (line.startsWith("Directory")) || + (line.startsWith("Total")) + ) { + line = reader.readLine(); + continue; + } + if (listing == null) { + listing = line; + } else { + listing += "\r\n" + line; + } + line = reader.readLine(); + } + reader.close(); + + byte[] bytes = listing.getBytes(); + ByteArrayInputStream listingStream = new ByteArrayInputStream(bytes); + + if (versioning) { + files = super.parseFileList(listingStream); + } else { + FTPFile[] tempFiles = super.parseFileList(listingStream); + HashMap filesHash = new HashMap(); + String fileName; + + for (int index = 0; index < tempFiles.length; index++) { + fileName = tempFiles[index].getName(); + if (!filesHash.containsKey(fileName)) { + filesHash.put(fileName, (FTPFile) tempFiles[index]); + } + } + + files = (FTPFile[]) filesHash.values().toArray(new FTPFile[0]); + } + + return files; + } + + /** * Parses a line of a VMS FTP server file listing and converts it into a * usable format in the form of an <code> FTPFile </code> instance. If the @@ -201,9 +270,25 @@ String hr = group(6); String min = group(7); String sec = group(8); - String grp = group(9); - String owner = group(10); - + String owner = group(9); + String[] array = owner.split(","); + String grp; + String user; + + switch (array.length) { + case 1: + grp = null; + user = array[0]; + break; + case 2: + grp = array[0]; + user = array[1]; + break; + default: + grp = null; + user = null; + } + if (name.lastIndexOf(".DIR") != -1) { f.setType(FTPFile.DIRECTORY_TYPE); @@ -243,7 +328,7 @@ f.setTimestamp(cal); f.setGroup(grp); - f.setUser(owner); + f.setUser(user); //set group and owner //Since I don't need the persmissions on this file (RWED), I'll //leave that for further development. 'Cause it will be a bit @@ -253,6 +338,7 @@ return null; } + /** * Reads the next entry using the supplied BufferedReader object up to * whatever delemits one entry from the next. This parser cannot use @@ -265,7 +351,6 @@ * @return A string representing the next ftp entry or null if none found. * @exception IOException thrown on any IO Error reading from the reader. */ - public String readNextEntry(BufferedReader reader) throws IOException { String line = reader.readLine(); @@ -279,6 +364,6 @@ } line = reader.readLine(); } - return (entry.length() == 0 ? null : entry.toString()); + return (entry.length() == 0 ? null : entry.toString()); } } 1.5 +73 -6 jakarta-commons/net/src/test/org/apache/commons/net/ftp/parser/VMSFTPEntryParserTest.java Index: VMSFTPEntryParserTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/net/src/test/org/apache/commons/net/ftp/parser/VMSFTPEntryParserTest.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- VMSFTPEntryParserTest.java 3 Mar 2003 15:25:56 -0000 1.4 +++ VMSFTPEntryParserTest.java 29 Jul 2003 02:35:35 -0000 1.5 @@ -54,6 +54,8 @@ * <http://www.apache.org/>. */ +import java.io.ByteArrayInputStream; + import junit.framework.TestSuite; import org.apache.commons.net.ftp.FTPFile; @@ -61,6 +63,7 @@ /** * @author <a href="mailto:[EMAIL PROTECTED]">Steve Cohen</a> + * @author <a href="[EMAIL PROTECTED]">Stephane ESTE-GRACIAS</a> * @version $Id$ */ public class VMSFTPEntryParserTest extends FTPParseTestFramework @@ -80,11 +83,11 @@ private static final String[] goodsamples = { - + "Directory USER1:[TEMP]\r\n\r\n", "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)", + "1-JUN.LIS;3 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)", "1-JUN.LIS;2 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)", - "1-JUN.LIS;2 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,)", - "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (,RWED,RWED,RE)", + "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [TRANSLATED] (,RWED,RWED,RE)", "120196.TXT;1 118/126 14-APR-1997 12:45:27 [GROUP,OWNER] (RWED,,RWED,RE)", "30CHARBAR.TXT;1 11/18 2-JUN-1998 08:38:42 [GROUP,OWNER] (RWED,RWED,RWED,RE)", "A.;2 18/18 1-JUL-1998 08:43:20 [GROUP,OWNER] (RWED,RWED,RWED,RE)", @@ -93,8 +96,9 @@ "UNARCHIVE.COM;1 2/15 7-JUL-1997 16:37:45 [POSTWARE,LP] (RWE,RWE,RWE,RE)", "UNXMERGE.COM;15 1/15 20-AUG-1996 13:59:50 [POSTWARE,LP] (RWE,RWE,RWE,RE)", "UNXTEMP.COM;7 1/15 15-AUG-1996 14:10:38 [POSTWARE,LP] (RWE,RWE,RWE,RE)", - "UNZIP_AND_ATTACH_FILES.COM;12\r\n 14/15 24-JUL-2002 14:35:40 [POSTWARE,LP] (RWE,RWE,RWE,RE)", - "UNZIP_AND_ATTACH_FILES.SAV;1\r\n 14/15 17-JAN-2002 11:13:53 [POSTWARE,LP] (RWE,RWED,RWE,RE)" + "UNZIP_AND_ATTACH_FILES.COM;12\r\n 14/15 24-JUL-2002 14:35:40 [TRANSLATED] (RWE,RWE,RWE,RE)", + "UNZIP_AND_ATTACH_FILES.SAV;1\r\n 14/15 17-JAN-2002 11:13:53 [POSTWARE,LP] (RWE,RWED,RWE,RE)", + "\r\nTotal 14 files" }; /** @@ -127,6 +131,22 @@ assertEquals("OWNER", dir.getUser()); checkPermisions(dir); + + + dir = getParser().parseFTPEntry("DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [TRANSLATED] (RWED,RWED,RWED,RE)"); + assertTrue("Should be a directory.", + dir.isDirectory()); + assertEquals("DATA.DIR", + dir.getName()); + assertEquals(512, + dir.getSize()); + assertEquals("Tue Jun 02 07:32:04 1998", + df.format(dir.getTimestamp().getTime())); + assertEquals(null, + dir.getGroup()); + assertEquals("TRANSLATED", + dir.getUser()); + checkPermisions(dir); } /** @@ -148,6 +168,22 @@ assertEquals("OWNER", file.getUser()); checkPermisions(file); + + + file = getParser().parseFTPEntry("1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [TRANSLATED] (RWED,RWED,RWED,RE)"); + assertTrue("Should be a file.", + file.isFile()); + assertEquals("1-JUN.LIS", + file.getName()); + assertEquals(9 * 512, + file.getSize()); + assertEquals("Tue Jun 02 07:32:04 1998", + df.format(file.getTimestamp().getTime())); + assertEquals(null, + file.getGroup()); + assertEquals("TRANSLATED", + file.getUser()); + checkPermisions(file); } /** @@ -221,4 +257,35 @@ { return(new TestSuite(VMSFTPEntryParserTest.class)); } + + public void testBadListing() throws Exception { + super.testBadListing(); + } + + public void testGoodListing() throws Exception { + String[] goodsamples = getGoodListing(); + String listing = ""; + + for (int i = 0; i < goodsamples.length; i++) { + listing += goodsamples[i] + "\r\n"; + } + + byte[] bytes = listing.getBytes(); + ByteArrayInputStream listingStream = new ByteArrayInputStream(bytes); + + FTPFile[] files = new VMSFTPEntryParser(true).parseFileList(listingStream); + + System.out.println(files.length); + for (int index = 0; index < files.length; index++) { + System.out.println(files[index].getName()); + } + + listingStream.reset(); + files = new VMSFTPEntryParser(false).parseFileList(listingStream); + + System.out.println(files.length); + for (int index = 0; index < files.length; index++) { + System.out.println(files[index].getName()); + } + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]