NTFTPEntryParser.java

  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. package org.apache.commons.net.ftp.parser;

  18. import java.text.ParseException;
  19. import java.util.regex.Pattern;

  20. import org.apache.commons.net.ftp.Configurable;
  21. import org.apache.commons.net.ftp.FTPClientConfig;
  22. import org.apache.commons.net.ftp.FTPFile;
  23. import org.apache.commons.net.ftp.FTPFileEntryParser;

  24. /**
  25.  * Implements {@link FTPFileEntryParser} and {@link Configurable} for NT Systems.
  26.  *
  27.  * @see FTPFileEntryParser Usage instructions.
  28.  */
  29. public class NTFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {

  30.     private static final String DEFAULT_DATE_FORMAT = "MM-dd-yy hh:mma"; // 11-09-01 12:30PM

  31.     private static final String DEFAULT_DATE_FORMAT2 = "MM-dd-yy kk:mm"; // 11-09-01 18:30

  32.     /**
  33.      * this is the regular expression used by this parser.
  34.      */
  35.     private static final String REGEX = "(\\S+)\\s+(\\S+)\\s+" // MM-dd-yy whitespace hh:mma|kk:mm; swallow trailing spaces
  36.             + "(?:(<DIR>)|([0-9]+))\\s+" // <DIR> or ddddd; swallow trailing spaces
  37.             + "(\\S.*)"; // First non-space followed by rest of line (name)

  38.     private final FTPTimestampParser timestampParser;

  39.     /**
  40.      * The sole constructor for an NTFTPEntryParser object.
  41.      *
  42.      * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
  43.      *                                  sign that <code>REGEX</code> is not a valid regular expression.
  44.      */
  45.     public NTFTPEntryParser() {
  46.         this(null);
  47.     }

  48.     /**
  49.      * This constructor allows the creation of an NTFTPEntryParser object with something other than the default configuration.
  50.      *
  51.      * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
  52.      * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
  53.      *                                  sign that <code>REGEX</code> is not a valid regular expression.
  54.      * @since 1.4
  55.      */
  56.     public NTFTPEntryParser(final FTPClientConfig config) {
  57.         super(REGEX, Pattern.DOTALL);
  58.         configure(config);
  59.         final FTPClientConfig config2 = new FTPClientConfig(FTPClientConfig.SYST_NT, DEFAULT_DATE_FORMAT2, null);
  60.         config2.setDefaultDateFormatStr(DEFAULT_DATE_FORMAT2);
  61.         this.timestampParser = new FTPTimestampParserImpl();
  62.         ((Configurable) this.timestampParser).configure(config2);
  63.     }

  64.     /**
  65.      * Defines a default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
  66.      *
  67.      * @return the default configuration for this parser.
  68.      */
  69.     @Override
  70.     public FTPClientConfig getDefaultConfiguration() {
  71.         return new FTPClientConfig(FTPClientConfig.SYST_NT, DEFAULT_DATE_FORMAT, null);
  72.     }

  73.     /**
  74.      * Parses a line of an NT FTP server file listing and converts it into a usable format in the form of an <code>FTPFile</code> instance. If the file
  75.      * listing line doesn't describe a file, <code>null</code> is returned, otherwise a <code>FTPFile</code> instance representing the files in the
  76.      * 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.     @Override
  82.     public FTPFile parseFTPEntry(final String entry) {
  83.         final FTPFile f = new FTPFile();
  84.         f.setRawListing(entry);

  85.         if (matches(entry)) {
  86.             final String datestr = group(1) + " " + group(2);
  87.             final String dirString = group(3);
  88.             final String size = group(4);
  89.             final String name = group(5);
  90.             try {
  91.                 f.setTimestamp(super.parseTimestamp(datestr));
  92.             } catch (final ParseException e) {
  93.                 // parsing fails, try the other date format
  94.                 try {
  95.                     f.setTimestamp(timestampParser.parseTimestamp(datestr));
  96.                 } catch (final ParseException e2) {
  97.                     // intentionally do nothing
  98.                 }
  99.             }

  100.             if (null == name || name.equals(".") || name.equals("..")) {
  101.                 return null;
  102.             }
  103.             f.setName(name);

  104.             if ("<DIR>".equals(dirString)) {
  105.                 f.setType(FTPFile.DIRECTORY_TYPE);
  106.                 f.setSize(0);
  107.             } else {
  108.                 f.setType(FTPFile.FILE_TYPE);
  109.                 if (null != size) {
  110.                     f.setSize(Long.parseLong(size));
  111.                 }
  112.             }
  113.             return f;
  114.         }
  115.         return null;
  116.     }

  117. }