001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.ftp.parser;
019
020import java.text.ParseException;
021import java.util.Calendar;
022
023import org.apache.commons.net.ftp.Configurable;
024import org.apache.commons.net.ftp.FTPClientConfig;
025
026
027/**
028 * <p>
029 * This abstract class implements the common timestamp parsing
030 * algorithm for all the concrete parsers.  Classes derived from
031 * this one will parse file listings via a supplied regular expression
032 * that pulls out the date portion as a separate string which is
033 * passed to the underlying {@link FTPTimestampParser delegate} to
034 * handle parsing of the file timestamp.
035 * <p>
036 * This class also implements the {@link Configurable Configurable}
037 * interface to allow the parser to be configured from the outside.
038 *
039 * @since 1.4
040 */
041public abstract class ConfigurableFTPFileEntryParserImpl
042extends RegexFTPFileEntryParserImpl
043implements Configurable
044{
045
046    private final FTPTimestampParser timestampParser;
047
048    /**
049     * constructor for this abstract class.
050     * @param regex  Regular expression used main parsing of the
051     * file listing.
052     */
053    public ConfigurableFTPFileEntryParserImpl(String regex)
054    {
055        super(regex);
056        this.timestampParser = new FTPTimestampParserImpl();
057    }
058
059    /**
060     * constructor for this abstract class.
061     * @param regex  Regular expression used main parsing of the
062     * file listing.
063     * @param flags the flags to apply, see
064     * {@link java.util.regex.Pattern#compile(String, int) Pattern#compile(String, int)}. Use 0 for none.
065     * @since 3.4
066     */
067    public ConfigurableFTPFileEntryParserImpl(String regex, int flags)
068    {
069        super(regex, flags);
070        this.timestampParser = new FTPTimestampParserImpl();
071    }
072
073    /**
074     * This method is called by the concrete parsers to delegate
075     * timestamp parsing to the timestamp parser.
076     *
077     * @param timestampStr the timestamp string pulled from the
078     * file listing by the regular expression parser, to be submitted
079     * to the <code>timestampParser</code> for extracting the timestamp.
080     * @return a <code>java.util.Calendar</code> containing results of the
081     * timestamp parse.
082     * @throws ParseException on parse error
083     */
084    public Calendar parseTimestamp(String timestampStr) throws ParseException {
085        return this.timestampParser.parseTimestamp(timestampStr);
086    }
087
088
089    /**
090     * Implementation of the {@link  Configurable  Configurable}
091     * interface. Configures this parser by delegating to the
092     * underlying Configurable FTPTimestampParser implementation, '
093     * passing it the supplied {@link  FTPClientConfig FTPClientConfig}
094     * if that is non-null or a default configuration defined by
095     * each concrete subclass.
096     *
097     * @param config the configuration to be used to configure this parser.
098     * If it is null, a default configuration defined by
099     * each concrete subclass is used instead.
100     */
101    @Override
102    public void configure(FTPClientConfig config)
103    {
104        if (this.timestampParser instanceof Configurable) {
105            FTPClientConfig defaultCfg = getDefaultConfiguration();
106            if (config != null) {
107                if (null == config.getDefaultDateFormatStr()) {
108                    config.setDefaultDateFormatStr(defaultCfg.getDefaultDateFormatStr());
109                }
110                if (null == config.getRecentDateFormatStr()) {
111                    config.setRecentDateFormatStr(defaultCfg.getRecentDateFormatStr());
112                }
113                ((Configurable)this.timestampParser).configure(config);
114            } else {
115                ((Configurable)this.timestampParser).configure(defaultCfg);
116            }
117        }
118    }
119
120    /**
121     * Each concrete subclass must define this member to create
122     * a default configuration to be used when that subclass is
123     * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
124     * parameter being specified.
125     * @return the default configuration for the subclass.
126     */
127    protected abstract FTPClientConfig getDefaultConfiguration();
128}