OS2FTPEntryParser.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 org.apache.commons.net.ftp.Configurable;
  20. import org.apache.commons.net.ftp.FTPClientConfig;
  21. import org.apache.commons.net.ftp.FTPFile;
  22. import org.apache.commons.net.ftp.FTPFileEntryParser;

  23. /**
  24.  * Implements of {@link FTPFileEntryParser} and {@link Configurable} for OS/2 Systems.
  25.  *
  26.  * @see FTPFileEntryParser Usage instructions.
  27.  */
  28. public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl

  29. {

  30.     private static final String DEFAULT_DATE_FORMAT = "MM-dd-yy HH:mm"; // 11-09-01 12:30
  31.     /**
  32.      * this is the regular expression used by this parser.
  33.      */
  34.     private static final String REGEX = "\\s*([0-9]+)\\s*" + "(\\s+|[A-Z]+)\\s*" + "(DIR|\\s+)\\s*" + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
  35.             + "(\\S.*)";

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

  45.     /**
  46.      * This constructor allows the creation of an OS2FTPEntryParser object with something other than the default configuration.
  47.      *
  48.      * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
  49.      * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
  50.      *                                  sign that <code>REGEX</code> is not a valid regular expression.
  51.      * @since 1.4
  52.      */
  53.     public OS2FTPEntryParser(final FTPClientConfig config) {
  54.         super(REGEX);
  55.         configure(config);
  56.     }

  57.     /**
  58.      * Defines a default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
  59.      *
  60.      * @return the default configuration for this parser.
  61.      */
  62.     @Override
  63.     protected FTPClientConfig getDefaultConfiguration() {
  64.         return new FTPClientConfig(FTPClientConfig.SYST_OS2, DEFAULT_DATE_FORMAT, null);
  65.     }

  66.     /**
  67.      * Parses a line of an OS2 FTP server file listing and converts it into a usable format in the form of an <code>FTPFile</code> instance. If the file
  68.      * listing line doesn't describe a file, <code>null</code> is returned, otherwise a <code>FTPFile</code> instance representing the files in the
  69.      * directory is returned.
  70.      *
  71.      * @param entry A line of text from the file listing
  72.      * @return An FTPFile instance corresponding to the supplied entry
  73.      */
  74.     @Override
  75.     public FTPFile parseFTPEntry(final String entry) {

  76.         final FTPFile f = new FTPFile();
  77.         if (matches(entry)) {
  78.             final String size = group(1);
  79.             final String attrib = group(2);
  80.             final String dirString = group(3);
  81.             final String datestr = group(4) + " " + group(5);
  82.             final String name = group(6);
  83.             try {
  84.                 f.setTimestamp(super.parseTimestamp(datestr));
  85.             } catch (final ParseException e) {
  86.                 // intentionally do nothing
  87.             }

  88.             // is it a DIR or a file
  89.             if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR")) {
  90.                 f.setType(FTPFile.DIRECTORY_TYPE);
  91.             } else {
  92.                 f.setType(FTPFile.FILE_TYPE);
  93.             }

  94.             // set the name
  95.             f.setName(name.trim());

  96.             // set the size
  97.             f.setSize(Long.parseLong(size.trim()));

  98.             return f;
  99.         }
  100.         return null;

  101.     }

  102. }