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
18 package org.apache.commons.net.ftp.parser;
19
20 import java.text.ParseException;
21 import java.util.Calendar;
22
23 import org.apache.commons.net.ftp.Configurable;
24 import org.apache.commons.net.ftp.FTPClientConfig;
25
26
27 /**
28 * <p>
29 * This abstract class implements the common timestamp parsing
30 * algorithm for all the concrete parsers. Classes derived from
31 * this one will parse file listings via a supplied regular expression
32 * that pulls out the date portion as a separate string which is
33 * passed to the underlying {@link FTPTimestampParser delegate} to
34 * handle parsing of the file timestamp.
35 * </p><p>
36 * This class also implements the {@link Configurable Configurable}
37 * interface to allow the parser to be configured from the outside.
38 * </p>
39 * @since 1.4
40 */
41 /**
42 * To change the template for this generated type comment go to
43 * Window - Preferences - Java - Code Style - Code Templates - Comments
44 */
45 public abstract class ConfigurableFTPFileEntryParserImpl
46 extends RegexFTPFileEntryParserImpl
47 implements Configurable
48 {
49
50 private FTPTimestampParser timestampParser;
51
52 /**
53 * Only constructor for this absract class.
54 * @param regex Regular expression used main parsing of the
55 * file listing.
56 */
57 public ConfigurableFTPFileEntryParserImpl(String regex)
58 {
59 super(regex);
60 this.timestampParser = new FTPTimestampParserImpl();
61 }
62
63 /**
64 * This method is called by the concrete parsers to delegate
65 * timestamp parsing to the timestamp parser.
66 * <p>
67 * @param timestampStr the timestamp string pulled from the
68 * file listing by the regular expression parser, to be submitted
69 * to the <code>timestampParser</code> for extracting the timestamp.
70 * @return a <code>java.util.Calendar</code> containing results of the
71 * timestamp parse.
72 */
73 public Calendar parseTimestamp(String timestampStr) throws ParseException {
74 return this.timestampParser.parseTimestamp(timestampStr);
75 }
76
77
78 /**
79 * Implementation of the {@link Configurable Configurable}
80 * interface. Configures this parser by delegating to the
81 * underlying Configurable FTPTimestampParser implementation, '
82 * passing it the supplied {@link FTPClientConfig FTPClientConfig}
83 * if that is non-null or a default configuration defined by
84 * each concrete subclass.
85 * </p>
86 * @param config the configuration to be used to configure this parser.
87 * If it is null, a default configuration defined by
88 * each concrete subclass is used instead.
89 */
90 public void configure(FTPClientConfig config)
91 {
92 if (this.timestampParser instanceof Configurable) {
93 FTPClientConfig defaultCfg = getDefaultConfiguration();
94 if (config != null) {
95 if (null == config.getDefaultDateFormatStr()) {
96 config.setDefaultDateFormatStr(defaultCfg.getDefaultDateFormatStr());
97 }
98 if (null == config.getRecentDateFormatStr()) {
99 config.setRecentDateFormatStr(defaultCfg.getRecentDateFormatStr());
100 }
101 ((Configurable)this.timestampParser).configure(config);
102 } else {
103 ((Configurable)this.timestampParser).configure(defaultCfg);
104 }
105 }
106 }
107
108 /**
109 * Each concrete subclass must define this member to create
110 * a default configuration to be used when that subclass is
111 * instantiated without a {@link FTPClientConfig FTPClientConfig}
112 * parameter being specified.
113 * @return the default configuration for the subclass.
114 */
115 protected abstract FTPClientConfig getDefaultConfiguration();
116 }