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.text.ParseException;
20  
21  import org.apache.commons.net.ftp.FTPClientConfig;
22  import org.apache.commons.net.ftp.FTPFile;
23  
24  /**
25   * Implementation of FTPFileEntryParser and FTPFileListParser for OS2 Systems.
26   *
27   * @version $Id: OS2FTPEntryParser.java 1741829 2016-05-01 00:24:44Z sebb $
28   * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
29   */
30  public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
31  
32  {
33  
34      private static final String DEFAULT_DATE_FORMAT
35          = "MM-dd-yy HH:mm"; //11-09-01 12:30
36      /**
37       * this is the regular expression used by this parser.
38       */
39      private static final String REGEX =
40          "\\s*([0-9]+)\\s*"
41          + "(\\s+|[A-Z]+)\\s*"
42          + "(DIR|\\s+)\\s*"
43          + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
44          + "(\\S.*)";
45  
46      /**
47       * The default constructor for a OS2FTPEntryParser object.
48       *
49       * @exception IllegalArgumentException
50       * Thrown if the regular expression is unparseable.  Should not be seen
51       * under normal conditions.  It it is seen, this is a sign that
52       * <code>REGEX</code> is  not a valid regular expression.
53       */
54      public OS2FTPEntryParser()
55      {
56          this(null);
57      }
58  
59      /**
60       * This constructor allows the creation of an OS2FTPEntryParser object
61       * with something other than the default configuration.
62       *
63       * @param config The {@link FTPClientConfig configuration} object used to
64       * configure this parser.
65       * @exception IllegalArgumentException
66       * Thrown if the regular expression is unparseable.  Should not be seen
67       * under normal conditions.  It it is seen, this is a sign that
68       * <code>REGEX</code> is  not a valid regular expression.
69       * @since 1.4
70       */
71       public OS2FTPEntryParser(FTPClientConfig config)
72      {
73          super(REGEX);
74          configure(config);
75      }
76  
77      /**
78       * Parses a line of an OS2 FTP server file listing and converts it into a
79       * usable format in the form of an <code> FTPFile </code> instance.  If the
80       * file listing line doesn't describe a file, <code> null </code> is
81       * returned, otherwise a <code> FTPFile </code> instance representing the
82       * files in the directory is returned.
83       *
84       * @param entry A line of text from the file listing
85       * @return An FTPFile instance corresponding to the supplied entry
86       */
87      @Override
88      public FTPFile parseFTPEntry(String entry)
89      {
90  
91          FTPFile f = new FTPFile();
92          if (matches(entry))
93          {
94              String size = group(1);
95              String attrib = group(2);
96              String dirString = group(3);
97              String datestr = group(4)+" "+group(5);
98              String name = group(6);
99              try
100             {
101                 f.setTimestamp(super.parseTimestamp(datestr));
102             }
103             catch (ParseException e)
104             {
105                 // intentionally do nothing
106             }
107 
108 
109             //is it a DIR or a file
110             if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR"))
111             {
112                 f.setType(FTPFile.DIRECTORY_TYPE);
113             }
114             else
115             {
116                 f.setType(FTPFile.FILE_TYPE);
117             }
118 
119 
120             //set the name
121             f.setName(name.trim());
122 
123             //set the size
124             f.setSize(Long.parseLong(size.trim()));
125 
126             return (f);
127         }
128         return null;
129 
130     }
131 
132     /**
133      * Defines a default configuration to be used when this class is
134      * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
135      * parameter being specified.
136      * @return the default configuration for this parser.
137      */
138     @Override
139     protected FTPClientConfig getDefaultConfiguration() {
140         return new FTPClientConfig(
141                 FTPClientConfig.SYST_OS2,
142                 DEFAULT_DATE_FORMAT,
143                 null, null, null, null);
144     }
145 
146 }