ConfigurableFTPFileEntryParserImpl.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.Calendar;

  20. import org.apache.commons.net.ftp.Configurable;
  21. import org.apache.commons.net.ftp.FTPClientConfig;

  22. /**
  23.  * <p>
  24.  * This abstract class implements the common timestamp parsing algorithm for all the concrete parsers. Classes derived from this one will parse file listings
  25.  * via a supplied regular expression that pulls out the date portion as a separate string which is passed to the underlying {@link FTPTimestampParser delegate}
  26.  * to handle parsing of the file timestamp.
  27.  * <p>
  28.  * This class also implements the {@link Configurable Configurable} interface to allow the parser to be configured from the outside.
  29.  *
  30.  * @since 1.4
  31.  */
  32. public abstract class ConfigurableFTPFileEntryParserImpl extends RegexFTPFileEntryParserImpl implements Configurable {

  33.     private final FTPTimestampParser timestampParser;

  34.     /**
  35.      * constructor for this abstract class.
  36.      *
  37.      * @param regex Regular expression used main parsing of the file listing.
  38.      */
  39.     public ConfigurableFTPFileEntryParserImpl(final String regex) {
  40.         super(regex);
  41.         this.timestampParser = new FTPTimestampParserImpl();
  42.     }

  43.     /**
  44.      * constructor for this abstract class.
  45.      *
  46.      * @param regex Regular expression used main parsing of the file listing.
  47.      * @param flags the flags to apply, see {@link java.util.regex.Pattern#compile(String, int) Pattern#compile(String, int)}. Use 0 for none.
  48.      * @since 3.4
  49.      */
  50.     public ConfigurableFTPFileEntryParserImpl(final String regex, final int flags) {
  51.         super(regex, flags);
  52.         this.timestampParser = new FTPTimestampParserImpl();
  53.     }

  54.     /**
  55.      * Implements the {@link Configurable Configurable} interface. Configures this parser by delegating to the underlying Configurable FTPTimestampParser
  56.      * implementation, ' passing it the supplied {@link FTPClientConfig FTPClientConfig} if that is non-null or a default configuration defined by each concrete
  57.      * subclass.
  58.      *
  59.      * @param config the configuration to be used to configure this parser. If it is null, a default configuration defined by each concrete subclass is used
  60.      *               instead.
  61.      */
  62.     @Override
  63.     public void configure(final FTPClientConfig config) {
  64.         if (this.timestampParser instanceof Configurable) {
  65.             final FTPClientConfig defaultCfg = getDefaultConfiguration();
  66.             if (config != null) {
  67.                 if (null == config.getDefaultDateFormatStr()) {
  68.                     config.setDefaultDateFormatStr(defaultCfg.getDefaultDateFormatStr());
  69.                 }
  70.                 if (null == config.getRecentDateFormatStr()) {
  71.                     config.setRecentDateFormatStr(defaultCfg.getRecentDateFormatStr());
  72.                 }
  73.                 ((Configurable) this.timestampParser).configure(config);
  74.             } else {
  75.                 ((Configurable) this.timestampParser).configure(defaultCfg);
  76.             }
  77.         }
  78.     }

  79.     /**
  80.      * Each concrete subclass must define this member to create a default configuration to be used when that subclass is instantiated without a
  81.      * {@link FTPClientConfig FTPClientConfig} parameter being specified.
  82.      *
  83.      * @return the default configuration for the subclass.
  84.      */
  85.     protected abstract FTPClientConfig getDefaultConfiguration();

  86.     /**
  87.      * This method is called by the concrete parsers to delegate timestamp parsing to the timestamp parser.
  88.      *
  89.      * @param timestampStr the timestamp string pulled from the file listing by the regular expression parser, to be submitted to the
  90.      *                     <code>timestampParser</code> for extracting the timestamp.
  91.      * @return a <code>java.util.Calendar</code> containing results of the timestamp parse.
  92.      * @throws ParseException on parse error
  93.      */
  94.     public Calendar parseTimestamp(final String timestampStr) throws ParseException {
  95.         return this.timestampParser.parseTimestamp(timestampStr);
  96.     }
  97. }