FileDocCategorySizeDatePackage
FTPTimestampParserImpl.javaAPI DocApache Commons NET 1.4.1 API8036Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.ftp.parser

FTPTimestampParserImpl

public class FTPTimestampParserImpl extends Object implements FTPTimestampParser, org.apache.commons.net.ftp.Configurable
Default implementation of the {@link FTPTimestampParser FTPTimestampParser} interface also implements the {@link org.apache.commons.net.ftp.Configurable Configurable} interface to allow the parsing to be configured from the outside.
see
ConfigurableFTPFileEntryParserImpl
since
1.4

Fields Summary
private SimpleDateFormat
defaultDateFormat
private SimpleDateFormat
recentDateFormat
Constructors Summary
public FTPTimestampParserImpl()
The only constructor for this class.

		setDefaultDateFormat(DEFAULT_SDF);
		setRecentDateFormat(DEFAULT_RECENT_SDF);
	
Methods Summary
public voidconfigure(org.apache.commons.net.ftp.FTPClientConfig config)
Implementation of the {@link Configurable Configurable} interface. Configures this FTPTimestampParser according to the following logic:

Set up the {@link FTPClientConfig#setDefaultDateFormatStr(java.lang.String) defaultDateFormat} and optionally the {@link FTPClientConfig#setRecentDateFormatStr(String) recentDateFormat} to values supplied in the config based on month names configured as follows:

  • If a {@link FTPClientConfig#setShortMonthNames(String) shortMonthString} has been supplied in the config, use that to parse parse timestamps.
  • Otherwise, if a {@link FTPClientConfig#setServerLanguageCode(String) serverLanguageCode} has been supplied in the config, use the month names represented by that {@link FTPClientConfig#lookupDateFormatSymbols(String) language} to parse timestamps.
  • otherwise use default English month names

Finally if a {@link org.apache.commons.net.ftp.FTPClientConfig#setServerTimeZoneId(String) serverTimeZoneId} has been supplied via the config, set that into all date formats that have been configured.

		DateFormatSymbols dfs = null;
		
		String languageCode = config.getServerLanguageCode();
		String shortmonths = config.getShortMonthNames();
		if (shortmonths != null) {
			dfs = FTPClientConfig.getDateFormatSymbols(shortmonths);
		} else if (languageCode != null) {
			dfs = FTPClientConfig.lookupDateFormatSymbols(languageCode);
		} else {
			dfs = FTPClientConfig.lookupDateFormatSymbols("en");
		}
		
		
		String recentFormatString = config.getRecentDateFormatStr();
		if (recentFormatString == null) {
		    this.recentDateFormat = null;
		} else {
			this.recentDateFormat = new SimpleDateFormat(recentFormatString, dfs);
			this.recentDateFormat.setLenient(false);
		}
			
		String defaultFormatString = config.getDefaultDateFormatStr();
		if (defaultFormatString == null) {
			throw new IllegalArgumentException("defaultFormatString cannot be null");
		}
		this.defaultDateFormat = new SimpleDateFormat(defaultFormatString, dfs);
		this.defaultDateFormat.setLenient(false);
		
		setServerTimeZone(config.getServerTimeZoneId());
	
public java.text.SimpleDateFormatgetDefaultDateFormat()

return
Returns the defaultDateFormat.

		return defaultDateFormat;
	
public java.lang.StringgetDefaultDateFormatString()

return
Returns the defaultDateFormat pattern string.

		return defaultDateFormat.toPattern();
	
public java.text.SimpleDateFormatgetRecentDateFormat()

return
Returns the recentDateFormat.

		return recentDateFormat;
	
public java.lang.StringgetRecentDateFormatString()

return
Returns the recentDateFormat.

		return recentDateFormat.toPattern();
	
public java.util.TimeZonegetServerTimeZone()

return
Returns the serverTimeZone used by this parser.

		return this.defaultDateFormat.getTimeZone();
	
public java.lang.String[]getShortMonths()

return
returns an array of 12 strings representing the short month names used by this parse.

		return defaultDateFormat.getDateFormatSymbols().getShortMonths();
	
public java.util.CalendarparseTimestamp(java.lang.String timestampStr)
Implements the one {@link FTPTimestampParser#parseTimestamp(String) method} in the {@link FTPTimestampParser FTPTimestampParser} interface according to this algorithm: If the recentDateFormat member has been defined, try to parse the supplied string with that. If that parse fails, or if the recentDateFormat member has not been defined, attempt to parse with the defaultDateFormat member. If that fails, throw a ParseException.

see
org.apache.commons.net.ftp.parser.FTPTimestampParser#parseTimestamp(java.lang.String)

		Calendar now = Calendar.getInstance();
		now.setTimeZone(this.getServerTimeZone());
		
		Calendar working = Calendar.getInstance();
		working.setTimeZone(this.getServerTimeZone());
		ParsePosition pp = new ParsePosition(0);

		Date parsed = null;
		if (this.recentDateFormat != null) {
			parsed = recentDateFormat.parse(timestampStr, pp);
		}
		if (parsed != null && pp.getIndex() == timestampStr.length()) 
		{
			working.setTime(parsed);
			working.set(Calendar.YEAR, now.get(Calendar.YEAR));
			if (working.after(now)) {
				working.add(Calendar.YEAR, -1);
			}
		} else {
			pp = new ParsePosition(0);
			parsed = defaultDateFormat.parse(timestampStr, pp);
			// note, length checks are mandatory for us since
			// SimpleDateFormat methods will succeed if less than
			// full string is matched.  They will also accept, 
			// despite "leniency" setting, a two-digit number as
			// a valid year (e.g. 22:04 will parse as 22 A.D.) 
			// so could mistakenly confuse an hour with a year, 
			// if we don't insist on full length parsing.
			if (parsed != null && pp.getIndex() == timestampStr.length()) {
				working.setTime(parsed);
			} else {
				throw new ParseException(
					"Timestamp could not be parsed with older or recent DateFormat", 
					pp.getIndex());
			}
		}
		return working;
	
private voidsetDefaultDateFormat(java.lang.String format)

param
defaultDateFormat The defaultDateFormat to be set.

		if (format != null) {
			this.defaultDateFormat = new SimpleDateFormat(format);
			this.defaultDateFormat.setLenient(false);
		}
	
private voidsetRecentDateFormat(java.lang.String format)

param
recentDateFormat The recentDateFormat to set.

		if (format != null) {
			this.recentDateFormat = new SimpleDateFormat(format);
			this.recentDateFormat.setLenient(false);
		}
	
private voidsetServerTimeZone(java.lang.String serverTimeZoneId)
sets a TimeZone represented by the supplied ID string into all of the parsers used by this server.

param
serverTimeZone Time Id java.util.TimeZone id used by the ftp server. If null the client's local time zone is assumed.

		TimeZone serverTimeZone = TimeZone.getDefault();
		if (serverTimeZoneId != null) {
			serverTimeZone = TimeZone.getTimeZone(serverTimeZoneId);
		}
		this.defaultDateFormat.setTimeZone(serverTimeZone);
		if (this.recentDateFormat != null) {
			this.recentDateFormat.setTimeZone(serverTimeZone);
		}