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: * <p>
026: * This abstract class implements the common timestamp parsing
027: * algorithm for all the concrete parsers. Classes derived from
028: * this one will parse file listings via a supplied regular expression
029: * that pulls out the date portion as a separate string which is
030: * passed to the underlying {@link FTPTimestampParser delegate} to
031: * handle parsing of the file timestamp.
032: * </p><p>
033: * This class also implements the {@link Configurable Configurable}
034: * interface to allow the parser to be configured from the outside.
035: * </p>
036: * @since 1.4
037: */
038: /**
039: * To change the template for this generated type comment go to
040: * Window - Preferences - Java - Code Style - Code Templates - Comments
041: */
042: public abstract class ConfigurableFTPFileEntryParserImpl extends
043: RegexFTPFileEntryParserImpl implements Configurable {
044:
045: private FTPTimestampParser timestampParser;
046:
047: /**
048: * Only constructor for this absract class.
049: * @param regex Regular expression used main parsing of the
050: * file listing.
051: */
052: public ConfigurableFTPFileEntryParserImpl(String regex) {
053: super (regex);
054: this .timestampParser = new FTPTimestampParserImpl();
055: }
056:
057: /**
058: * This method is called by the concrete parsers to delegate
059: * timestamp parsing to the timestamp parser.
060: * <p>
061: * @param timestampStr the timestamp string pulled from the
062: * file listing by the regular expression parser, to be submitted
063: * to the <code>timestampParser</code> for extracting the timestamp.
064: * @return a <code>java.util.Calendar</code> containing results of the
065: * timestamp parse.
066: */
067: public Calendar parseTimestamp(String timestampStr)
068: throws ParseException {
069: return this .timestampParser.parseTimestamp(timestampStr);
070: }
071:
072: /**
073: * Implementation of the {@link Configurable Configurable}
074: * interface. Configures this parser by delegating to the
075: * underlying Configurable FTPTimestampParser implementation, '
076: * passing it the supplied {@link FTPClientConfig FTPClientConfig}
077: * if that is non-null or a default configuration defined by
078: * each concrete subclass.
079: * </p>
080: * @param config the configuration to be used to configure this parser.
081: * If it is null, a default configuration defined by
082: * each concrete subclass is used instead.
083: */
084: public void configure(FTPClientConfig config) {
085: if (this .timestampParser instanceof Configurable) {
086: FTPClientConfig defaultCfg = getDefaultConfiguration();
087: if (config != null) {
088: if (null == config.getDefaultDateFormatStr()) {
089: config.setDefaultDateFormatStr(defaultCfg
090: .getDefaultDateFormatStr());
091: }
092: if (null == config.getRecentDateFormatStr()) {
093: config.setRecentDateFormatStr(defaultCfg
094: .getRecentDateFormatStr());
095: }
096: ((Configurable) this .timestampParser).configure(config);
097: } else {
098: ((Configurable) this .timestampParser)
099: .configure(defaultCfg);
100: }
101: }
102: }
103:
104: /**
105: * Each concrete subclass must define this member to create
106: * a default configuration to be used when that subclass is
107: * instantiated without a {@link FTPClientConfig FTPClientConfig}
108: * parameter being specified.
109: * @return the default configuration for the subclass.
110: */
111: protected abstract FTPClientConfig getDefaultConfiguration();
112: }
|