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