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