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