commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sco...@apache.org
Subject cvs commit: jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser OS400FTPEntryParser.java DefaultFTPFileEntryParserFactory.java
Date Fri, 26 Mar 2004 12:54:57 GMT
scohen      2004/03/26 04:54:57

  Modified:    net/src/test/org/apache/commons/net/ftp/parser
                        DefaultFTPFileEntryParserFactoryTest.java
               net/src/java/org/apache/commons/net/ftp/parser
                        DefaultFTPFileEntryParserFactory.java
  Added:       net/src/test/org/apache/commons/net/ftp/parser
                        OS400FTPEntryParserTest.java
               net/src/java/org/apache/commons/net/ftp/parser
                        OS400FTPEntryParser.java
  Log:
  PR: 27753
  Submitted by:mario@ops.co.at (Mario Ivankovits)
  Reviewed by:	Steve Cohen
  
  Revision  Changes    Path
  1.4       +3 -0      jakarta-commons/net/src/test/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactoryTest.java
  
  Index: DefaultFTPFileEntryParserFactoryTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/net/src/test/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactoryTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DefaultFTPFileEntryParserFactoryTest.java	29 Feb 2004 10:26:53 -0000	1.3
  +++ DefaultFTPFileEntryParserFactoryTest.java	26 Mar 2004 12:54:57 -0000	1.4
  @@ -58,6 +58,9 @@
           parser = factory.createFileEntryParser("OS/2");
           assertTrue(parser instanceof OS2FTPEntryParser);
   
  +        parser = factory.createFileEntryParser("OS/400");
  +        assertTrue(parser instanceof OS400FTPEntryParser);
  +        
           try {
               parser = factory.createFileEntryParser("OS2FTPFileEntryParser");
               fail("Exception should have been thrown. \"OS2FTPFileEntryParser\" is not a
recognized key");
  
  
  
  1.1                  jakarta-commons/net/src/test/org/apache/commons/net/ftp/parser/OS400FTPEntryParserTest.java
  
  Index: OS400FTPEntryParserTest.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.net.ftp.parser;
  import java.util.Calendar;
  
  import junit.framework.TestSuite;
  
  import org.apache.commons.net.ftp.FTPFile;
  import org.apache.commons.net.ftp.FTPFileEntryParser;
  
  /**
   * @version $Id: OS400FTPEntryParserTest.java,v 1.1 2004/03/26 12:54:57 scohen Exp $
   */
  
  public class OS400FTPEntryParserTest extends FTPParseTestFramework
  {
  
      private static final String[] badsamples =
      {
  		"PEP              4019 04/03/18 18:58:16 STMF       einladung.zip",
  		"PEP               422 03/24 14:06:26 *STMF      readme",
  		"PEP              6409 04/03/24 30:06:29 *STMF      build.xml",
  		"PEP USR         36864 04/03/24 14:06:34 *DIR       dir1/",
  		"PEP             3686404/03/24 14:06:47 *DIR       zdir2/"
      };
  
      private static final String[] goodsamples =
      {
  		"PEP              4019 04/03/18 18:58:16 *STMF      einladung.zip",
  		"PEP               422 04/03/24 14:06:26 *STMF      readme",
  		"PEP              6409 04/03/24 14:06:29 *STMF      build.xml",
  		"PEP             36864 04/03/24 14:06:34 *DIR       dir1/",
  		"PEP             36864 04/03/24 14:06:47 *DIR       zdir2/"
      };
  
      /**
       * @see junit.framework.TestCase#TestCase(String)
       */
      public OS400FTPEntryParserTest(String name)
      {
          super(name);
      }
  
      /**
       * @see FTPParseTestFramework#getBadListing()
       */
      protected String[] getBadListing()
      {
          return(badsamples);
      }
  
      /**
       * @see FTPParseTestFramework#getGoodListing()
       */
      protected String[] getGoodListing()
      {
          return(goodsamples);
      }
  
      /**
       * @see FTPParseTestFramework#getParser()
       */
      protected FTPFileEntryParser getParser()
      {
          return(new OS400FTPEntryParser());
      }
  
      /**
       * @see FTPParseTestFramework#testParseFieldsOnDirectory()
       */
      public void testParseFieldsOnDirectory() throws Exception
      {
          FTPFile f = getParser().parseFTPEntry("PEP             36864 04/03/24 14:06:34 *DIR
      dir1/");
          assertNotNull("Could not parse entry.",
                        f);
          assertTrue("Should have been a directory.",
                     f.isDirectory());
          assertEquals("PEP",
                       f.getUser());
          assertEquals("dir1",
                       f.getName());
          assertEquals(36864,
                       f.getSize());
  
          Calendar cal = Calendar.getInstance();
          cal.set(Calendar.MONTH, Calendar.MARCH);
  
          cal.set(Calendar.YEAR, 2004);
  		cal.set(Calendar.DATE, 24);
          cal.set(Calendar.HOUR_OF_DAY, 14);
  		cal.set(Calendar.MINUTE, 6);
          cal.set(Calendar.SECOND, 34);
  
          assertEquals(df.format(cal.getTime()),
                       df.format(f.getTimestamp().getTime()));
      }
  
      /**
       * @see FTPParseTestFramework#testParseFieldsOnFile()
       */
      public void testParseFieldsOnFile() throws Exception
      {
          FTPFile f = getParser().parseFTPEntry("PEP              6409 04/03/24 14:06:29 *STMF
     build.xml");
          assertNotNull("Could not parse entry.",
                        f);
          assertTrue("Should have been a file.",
                     f.isFile());
          assertEquals("PEP",
                       f.getUser());
          assertEquals("build.xml",
                       f.getName());
          assertEquals(6409,
                       f.getSize());
  
          Calendar cal = Calendar.getInstance();
  
          cal.set(Calendar.DATE, 24);
  		cal.set(Calendar.MONTH, Calendar.MARCH);
          cal.set(Calendar.YEAR, 2004);
          cal.set(Calendar.HOUR_OF_DAY, 14);
          cal.set(Calendar.MINUTE, 6);
  		cal.set(Calendar.SECOND, 29);
          assertEquals(df.format(cal.getTime()),
                       df.format(f.getTimestamp().getTime()));
      }
  
      /**
       * Method suite.
       * @return TestSuite
       */
      public static TestSuite suite()
      {
          return(new TestSuite(OS400FTPEntryParserTest.class));
      }
  }
  
  
  
  1.9       +2 -0      jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactory.java
  
  Index: DefaultFTPFileEntryParserFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/DefaultFTPFileEntryParserFactory.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DefaultFTPFileEntryParserFactory.java	29 Feb 2004 10:26:56 -0000	1.8
  +++ DefaultFTPFileEntryParserFactory.java	26 Mar 2004 12:54:57 -0000	1.9
  @@ -83,6 +83,8 @@
                   return new NTFTPEntryParser();
               } else if (ukey.indexOf("OS/2") >= 0) {
                   return new OS2FTPEntryParser();
  +			} else if (ukey.indexOf("OS/400") >= 0) {
  +                return new OS400FTPEntryParser();
               } else {
                   throw new ParserInitializationException(
                       "Unknown parser type: " + key);
  
  
  
  1.1                  jakarta-commons/net/src/java/org/apache/commons/net/ftp/parser/OS400FTPEntryParser.java
  
  Index: OS400FTPEntryParser.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.net.ftp.parser;
  
  import org.apache.commons.net.ftp.FTPFile;
  import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
  
  import java.util.Calendar;
  
  /**
   * @version $Id: OS400FTPEntryParser.java,v 1.1 2004/03/26 12:54:57 scohen Exp $
   */
  
  public class OS400FTPEntryParser extends FTPFileEntryParserImpl
  {
  	private static final String REGEX =
  		"(\\S+)\\s+"				// user
  		+ "(\\d+)\\s+"				// size
  		+ "(\\d\\d)/(\\d\\d)/(\\d\\d)\\s+" // year/month/day
  		+ "([0-2][0-9]):([0-5][0-9]):([0-5][0-9])\\s+" // hour:minutes:seconds
  		+ "(\\*\\S+)\\s+"				// *STMF/*DIR
  		+ "(\\S+/?)\\s*";				// filename
  
  	public OS400FTPEntryParser()
  	{
  		super(REGEX);
  	}
  
  	public FTPFile parseFTPEntry(String entry)
  	{
  
  		FTPFile file = new FTPFile();
  		file.setRawListing(entry);
  		int type;
  
  		if (matches(entry))
  		{
  			String usr = group(1);
  			String filesize = group(2);
  			String yr = group(3);
  			String mo = group(4);
  			String da = group(5);
  			String hr = group(6);
  			String min = group(7);
  			String sec = group(8);
  			String typeStr = group(9);
  			String name = group(10);
  
  			if (typeStr.equalsIgnoreCase("*STMF"))
  			{
  				type = FTPFile.FILE_TYPE;
  			}
  			else if (typeStr.equalsIgnoreCase("*DIR"))
  			{
  				type = FTPFile.DIRECTORY_TYPE;
  			}
  			else
  			{
  				type = FTPFile.UNKNOWN_TYPE;
  			}
  
  			file.setType(type);
  
  			file.setUser(usr);
  
  			try
  			{
  				file.setSize(Integer.parseInt(filesize));
  			}
  			catch (NumberFormatException e)
  			{
  				// intentionally do nothing
  			}
  
  			Calendar cal = Calendar.getInstance();
  			cal.set(Calendar.SECOND, 0);
  			cal.set(Calendar.MINUTE, 0);
  			cal.set(Calendar.HOUR_OF_DAY, 0);
  
  			try
  			{
  				int year = Integer.parseInt(yr, 10);
  				if (year < 70)
  				{
  					year += 2000;
  				}
  				else
  				{
  					year += 1900;
  				}
  
  				cal.set(Calendar.YEAR, year);
  				cal.set(Calendar.MONTH, Integer.parseInt(mo, 10)-1);
  				cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(da, 10));
  
  				cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr, 10));
  				cal.set(Calendar.MINUTE, Integer.parseInt(min, 10));
  				cal.set(Calendar.SECOND, Integer.parseInt(sec, 10));
  
  				file.setTimestamp(cal);
  			}
  			catch (NumberFormatException e)
  			{
  				// do nothing, date will be uninitialized
  			}
  
  			if (name.endsWith("/"))
  			{
  				name = name.substring(0, name.length() - 1);
  			}
  			int pos = name.lastIndexOf('/');
  			if (pos > -1)
  			{
  				name = name.substring(pos + 1);
  			}
  
  			file.setName(name);
  
  			return file;
  		}
  		return null;
  	}
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message