View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.net.ftp.parser;
17  
18  import java.text.ParseException;
19  import java.util.Calendar;
20  
21  import org.apache.commons.net.ftp.Configurable;
22  import org.apache.commons.net.ftp.FTPClientConfig;
23  
24  
25  /**
26   * <p>
27   * This abstract class implements the common timestamp parsing
28   * algorithm for all the concrete parsers.  Classes derived from
29   * this one will parse file listings via a supplied regular expression
30   * that pulls out the date portion as a separate string which is
31   * passed to the underlying {@link FTPTimestampParser delegate} to
32   * handle parsing of the file timestamp.
33   * </p><p>
34   * This class also implements the {@link Configurable Configurable}
35   * interface to allow the parser to be configured from the outside.
36   * </p>
37   * @since 1.4
38   */
39  /**
40   * To change the template for this generated type comment go to
41   * Window - Preferences - Java - Code Style - Code Templates - Comments
42   */
43  public abstract class ConfigurableFTPFileEntryParserImpl 
44  extends RegexFTPFileEntryParserImpl 
45  implements Configurable 
46  {
47  
48  	private FTPTimestampParser timestampParser;
49  	
50  	/**
51  	 * Only constructor for this absract class.
52  	 * @param regex  Regular expression used main parsing of the 
53  	 * file listing.
54  	 */
55  	public ConfigurableFTPFileEntryParserImpl(String regex)
56  	{
57  		super(regex);
58  		this.timestampParser = new FTPTimestampParserImpl();
59  	}
60  
61  	/**
62  	 * This method is called by the concrete parsers to delegate
63  	 * timestamp parsing to the timestamp parser.
64  	 * <p>
65  	 * @param timestampStr the timestamp string pulled from the 
66  	 * file listing by the regular expression parser, to be submitted
67  	 * to the <code>timestampParser</code> for extracting the timestamp.
68  	 * @return a <code>java.util.Calendar</code> containing results of the 
69  	 * timestamp parse. 
70  	 */
71  	public Calendar parseTimestamp(String timestampStr) throws ParseException {
72  		return this.timestampParser.parseTimestamp(timestampStr);
73  	}
74  
75  
76  	/**
77  	 * Implementation of the {@link  Configurable  Configurable}
78  	 * interface. Configures this parser by delegating to the
79  	 * underlying Configurable FTPTimestampParser implementation, '
80  	 * passing it the supplied {@link  FTPClientConfig FTPClientConfig}
81  	 * if that is non-null or a default configuration defined by
82  	 * each concrete subclass.
83  	 * </p> 
84  	 * @param config the configuration to be used to configure this parser.
85  	 * If it is null, a default configuration defined by
86  	 * each concrete subclass is used instead.
87  	 */
88      public void configure(FTPClientConfig config)
89      {
90          if (this.timestampParser instanceof Configurable) {
91              FTPClientConfig defaultCfg = getDefaultConfiguration();
92  	        if (config != null) {
93  	            if (null == config.getDefaultDateFormatStr()) {
94  	                config.setDefaultDateFormatStr(defaultCfg.getDefaultDateFormatStr());
95  	            }
96  	            if (null == config.getRecentDateFormatStr()) {
97  	                config.setRecentDateFormatStr(defaultCfg.getRecentDateFormatStr());
98  	            }
99  	            ((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 }