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