Coverage report

  %line %branch
org.apache.commons.net.ftp.parser.EnterpriseUnixFTPEntryParser
91% 
100% 

 1  
 /*
 2  
  * Copyright 2001-2005 The Apache Software Foundation
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.net.ftp.parser;
 17  
 import java.util.Calendar;
 18  
 
 19  
 import org.apache.commons.net.ftp.FTPFile;
 20  
 
 21  
 /**
 22  
  * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce.
 23  
  * Here is a sample of the sort of output line this parser processes:
 24  
  *  "-C--E-----FTP B QUA1I1      18128       41 Aug 12 13:56 QUADTEST"
 25  
  * <P><B>
 26  
  * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the
 27  
  * DefaultFTPParserFactory by classname.  It will not be chosen
 28  
  * by the autodetection scheme.
 29  
  * </B>
 30  
  * @version $Id: EnterpriseUnixFTPEntryParser.java 165675 2005-05-02 20:09:55Z rwinston $
 31  
  * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
 32  
  * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
 33  
  * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
 34  
  */
 35  
 public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl
 36  
 {
 37  
 
 38  
     /**
 39  
      * months abbreviations looked for by this parser.  Also used
 40  
      * to determine <b>which</b> month has been matched by the parser.
 41  
      */
 42  
     private static final String MONTHS =
 43  
         "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
 44  
 
 45  
     /**
 46  
      * this is the regular expression used by this parser.
 47  
      */
 48  
     private static final String REGEX =
 49  
         "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
 50  
         + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
 51  
         + "(\\S*)\\s*"
 52  
         + "(\\S+)\\s*"
 53  
         + "(\\S*)\\s*"
 54  
         + "(\\d*)\\s*"
 55  
         + "(\\d*)\\s*"
 56  
         + MONTHS
 57  
         + "\\s*"
 58  
         + "((?:[012]\\d*)|(?:3[01]))\\s*"
 59  
         + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
 60  
         + "(\\S*)(\\s*.*)";
 61  
 
 62  
     /**
 63  
      * The sole constructor for a EnterpriseUnixFTPEntryParser object.
 64  
      *
 65  
      */
 66  
     public EnterpriseUnixFTPEntryParser()
 67  
     {
 68  5
         super(REGEX);
 69  5
     }
 70  
 
 71  
     /**
 72  
      * Parses a line of a unix FTP server file listing and converts  it into a
 73  
      * usable format in the form of an <code> FTPFile </code>  instance.  If
 74  
      * the file listing line doesn't describe a file,  <code> null </code> is
 75  
      * returned, otherwise a <code> FTPFile </code>  instance representing the
 76  
      * files in the directory is returned.
 77  
      *
 78  
      * @param entry A line of text from the file listing
 79  
      * @return An FTPFile instance corresponding to the supplied entry
 80  
      */
 81  
     public FTPFile parseFTPEntry(String entry)
 82  
     {
 83  
 
 84  25
         FTPFile file = new FTPFile();
 85  25
         file.setRawListing(entry);
 86  
 
 87  25
         if (matches(entry))
 88  
         {
 89  3
             String usr = group(14);
 90  3
             String grp = group(15);
 91  3
             String filesize = group(16);
 92  3
             String mo = group(17);
 93  3
             String da = group(18);
 94  3
             String yr = group(20);
 95  3
             String hr = group(21);
 96  3
             String min = group(22);
 97  3
             String name = group(23);
 98  
 
 99  3
             file.setType(FTPFile.FILE_TYPE);
 100  3
             file.setUser(usr);
 101  3
             file.setGroup(grp);
 102  
             try
 103  
             {
 104  3
                 file.setSize(Long.parseLong(filesize));
 105  
             }
 106  0
             catch (NumberFormatException e)
 107  
             {
 108  
                 // intentionally do nothing
 109  3
             }
 110  
 
 111  3
             Calendar cal = Calendar.getInstance();
 112  3
 	    cal.set(Calendar.MILLISECOND, 0);
 113  3
             cal.set(Calendar.SECOND,
 114  
                     0);
 115  3
             cal.set(Calendar.MINUTE,
 116  
                     0);
 117  3
             cal.set(Calendar.HOUR_OF_DAY,
 118  
                     0);
 119  
             try
 120  
             {
 121  
 
 122  3
                 int pos = MONTHS.indexOf(mo);
 123  3
                 int month = pos / 4;
 124  3
                 if (yr != null)
 125  
                 {
 126  
                     // it's a year
 127  0
                     cal.set(Calendar.YEAR,
 128  
                             Integer.parseInt(yr));
 129  
                 }
 130  
                 else
 131  
                 {
 132  
                     // it must be  hour/minute or we wouldn't have matched
 133  3
                     int year = cal.get(Calendar.YEAR);
 134  
 
 135  
                     // if the month we're reading is greater than now, it must
 136  
                     // be last year
 137  3
                     if (cal.get(Calendar.MONTH) < month)
 138  
                     {
 139  0
                         year--;
 140  
                     }
 141  3
                     cal.set(Calendar.YEAR,
 142  
                             year);
 143  3
                     cal.set(Calendar.HOUR_OF_DAY,
 144  
                             Integer.parseInt(hr));
 145  3
                     cal.set(Calendar.MINUTE,
 146  
                             Integer.parseInt(min));
 147  
                 }
 148  3
                 cal.set(Calendar.MONTH,
 149  
                         month);
 150  3
                 cal.set(Calendar.DATE,
 151  
                         Integer.parseInt(da));
 152  3
                 file.setTimestamp(cal);
 153  
             }
 154  0
             catch (NumberFormatException e)
 155  
             {
 156  
                 // do nothing, date will be uninitialized
 157  3
             }
 158  3
             file.setName(name);
 159  
 
 160  3
             return file;
 161  
         }
 162  22
         return null;
 163  
     }
 164  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.