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