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    *      https://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  
20  import java.util.Calendar;
21  
22  import org.apache.commons.net.ftp.FTPFile;
23  
24  /**
25   * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce. Here is a sample of the sort of output line this parser processes:
26   *
27   * <pre>
28   * "-C--E-----FTP B QUA1I1      18128       41 Aug 12 13:56 QUADTEST"
29   * </pre>
30   * <p>
31   * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the DefaultFTPParserFactory by class name. It will not be chosen by the autodetection
32   * scheme.
33   * </p>
34   *
35   * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
36   * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
37   */
38  public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl {
39  
40      /**
41       * months abbreviations looked for by this parser. Also used to determine <strong>which</strong> month has been matched by the parser.
42       */
43      private static final String MONTHS = "(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      // @formatter:off
49      private static final String REGEX = "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
50              + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
51              + "(\\S*)\\s*" // 12
52              + "(\\S+)\\s*" // 13
53              + "(\\S*)\\s*" // 14 user
54              + "(\\d*)\\s*" // 15 group
55              + "(\\d*)\\s*" // 16 file size
56              + MONTHS // 17 month
57              + "\\s*" // TODO should the space be optional?
58              // TODO \\d* should be \\d? surely ? Otherwise 01111 is allowed
59              + "((?:[012]\\d*)|(?:3[01]))\\s*" // 18 date [012]\d* or 3[01]
60              + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
61              // 20 \d\d\d\d = year OR
62              // 21 [01]\d or 2[0123] hour + ':'
63              // 22 [012345]\d = minute
64              + "(\\S*)(\\s*.*)"; // 23 name
65      // @formatter:on
66  
67      /**
68       * The sole constructor for a EnterpriseUnixFTPEntryParser object.
69       */
70      public EnterpriseUnixFTPEntryParser() {
71          super(REGEX);
72      }
73  
74      /**
75       * Parses a line of a Unix FTP server file listing and converts it into a usable format in the form of an {@code FTPFile} instance. If the file
76       * listing line doesn't describe a file, {@code null} is returned, otherwise a {@code FTPFile} instance representing the files in the
77       * directory is returned.
78       *
79       * @param entry A line of text from the file listing
80       * @return An FTPFile instance corresponding to the supplied entry
81       */
82      @Override
83      public FTPFile parseFTPEntry(final String entry) {
84  
85          final FTPFile file = new FTPFile();
86          file.setRawListing(entry);
87  
88          if (matches(entry)) {
89              final String usr = group(14);
90              final String grp = group(15);
91              final String fileSize = group(16);
92              final String mo = group(17);
93              final String da = group(18);
94              final String yr = group(20);
95              final String hr = group(21);
96              final String min = group(22);
97              final String name = group(23);
98  
99              file.setType(FTPFile.FILE_TYPE);
100             file.setUser(usr);
101             file.setGroup(grp);
102             try {
103                 file.setSize(Long.parseLong(fileSize));
104             } catch (final NumberFormatException e) {
105                 // intentionally do nothing
106             }
107 
108             final Calendar cal = Calendar.getInstance();
109             cal.set(Calendar.MILLISECOND, 0);
110             cal.set(Calendar.SECOND, 0);
111             cal.set(Calendar.MINUTE, 0);
112             cal.set(Calendar.HOUR_OF_DAY, 0);
113 
114             final int pos = MONTHS.indexOf(mo);
115             final int month = pos / 4;
116             final int missingUnit; // the first missing unit
117             try {
118 
119                 if (yr != null) {
120                     // it's a year; there are no hours and minutes
121                     cal.set(Calendar.YEAR, Integer.parseInt(yr));
122                     missingUnit = Calendar.HOUR_OF_DAY;
123                 } else {
124                     // it must be hour/minute, or we wouldn't have matched
125                     missingUnit = Calendar.SECOND;
126                     int year = cal.get(Calendar.YEAR);
127 
128                     // if the month we're reading is greater than now, it must
129                     // be last year
130                     if (cal.get(Calendar.MONTH) < month) {
131                         year--;
132                     }
133                     cal.set(Calendar.YEAR, year);
134                     cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr));
135                     cal.set(Calendar.MINUTE, Integer.parseInt(min));
136                 }
137                 cal.set(Calendar.MONTH, month);
138                 cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(da));
139                 cal.clear(missingUnit);
140                 file.setTimestamp(cal);
141             } catch (final NumberFormatException e) {
142                 // do nothing, date will be uninitialized
143             }
144             file.setName(name);
145 
146             return file;
147         }
148         return null;
149     }
150 }