package org.apache.commons.net.ftp.parser;
import java.text.*;
import java.util.Calendar;
import java.util.TimeZone;
import org.apache.commons.net.ftp.Configurable;
import org.apache.commons.net.ftp.FTPClientConfig;
// Referenced classes of package org.apache.commons.net.ftp.parser:
// FTPTimestampParser
public class FTPTimestampParserImpl
implements FTPTimestampParser, Configurable
{
public FTPTimestampParserImpl()
{
setDefaultDateFormat("MMM d yyyy");
setRecentDateFormat("MMM d HH:mm");
}
public Calendar parseTimestamp(String timestampStr)
throws ParseException
{
Calendar now = Calendar.getInstance();
now.setTimeZone(getServerTimeZone());
Calendar working = Calendar.getInstance();
working.setTimeZone(getServerTimeZone());
ParsePosition pp = new ParsePosition(0);
java.util.Date parsed = null;
if(recentDateFormat != null)
parsed = recentDateFormat.parse(timestampStr, pp);
if(parsed != null && pp.getIndex() == timestampStr.length())
{
working.setTime(parsed);
working.set(1, now.get(1));
if(working.after(now))
working.add(1, -1);
} else
{
pp = new ParsePosition(0);
parsed = defaultDateFormat.parse(timestampStr, pp);
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;
}
public SimpleDateFormat getDefaultDateFormat()
{
return defaultDateFormat;
}
public String getDefaultDateFormatString()
{
return defaultDateFormat.toPattern();
}
private void setDefaultDateFormat(String format)
{
if(format != null)
{
defaultDateFormat = new SimpleDateFormat(format);
defaultDateFormat.setLenient(false);
}
}
public SimpleDateFormat getRecentDateFormat()
{
return recentDateFormat;
}
public String getRecentDateFormatString()
{
return recentDateFormat.toPattern();
}
private void setRecentDateFormat(String format)
{
if(format != null)
{
recentDateFormat = new SimpleDateFormat(format);
recentDateFormat.setLenient(false);
}
}
public String[] getShortMonths()
{
return defaultDateFormat.getDateFormatSymbols().getShortMonths();
}
public TimeZone getServerTimeZone()
{
return defaultDateFormat.getTimeZone();
}
private void setServerTimeZone(String serverTimeZoneId)
{
TimeZone serverTimeZone = TimeZone.getDefault();
if(serverTimeZoneId != null)
serverTimeZone = TimeZone.getTimeZone(serverTimeZoneId);
defaultDateFormat.setTimeZone(serverTimeZone);
if(recentDateFormat != null)
recentDateFormat.setTimeZone(serverTimeZone);
}
import java.text.*;
import java.util.Calendar;
import java.util.TimeZone;
import org.apache.commons.net.ftp.Configurable;
import org.apache.commons.net.ftp.FTPClientConfig;
// Referenced classes of package org.apache.commons.net.ftp.parser:
// FTPTimestampParser
public class FTPTimestampParserImpl
implements FTPTimestampParser, Configurable
{
public FTPTimestampParserImpl()
{
setDefaultDateFormat("MMM d yyyy");
setRecentDateFormat("MMM d HH:mm");
}
public Calendar parseTimestamp(String timestampStr)
throws ParseException
{
Calendar now = Calendar.getInstance();
now.setTimeZone(getServerTimeZone());
Calendar working = Calendar.getInstance();
working.setTimeZone(getServerTimeZone());
ParsePosition pp = new ParsePosition(0);
java.util.Date parsed = null;
if(recentDateFormat != null)
parsed = recentDateFormat.parse(timestampStr, pp);
if(parsed != null && pp.getIndex() == timestampStr.length())
{
working.setTime(parsed);
working.set(1, now.get(1));
if(working.after(now))
working.add(1, -1);
} else
{
pp = new ParsePosition(0);
parsed = defaultDateFormat.parse(timestampStr, pp);
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;
}
public SimpleDateFormat getDefaultDateFormat()
{
return defaultDateFormat;
}
public String getDefaultDateFormatString()
{
return defaultDateFormat.toPattern();
}
private void setDefaultDateFormat(String format)
{
if(format != null)
{
defaultDateFormat = new SimpleDateFormat(format);
defaultDateFormat.setLenient(false);
}
}
public SimpleDateFormat getRecentDateFormat()
{
return recentDateFormat;
}
public String getRecentDateFormatString()
{
return recentDateFormat.toPattern();
}
private void setRecentDateFormat(String format)
{
if(format != null)
{
recentDateFormat = new SimpleDateFormat(format);
recentDateFormat.setLenient(false);
}
}
public String[] getShortMonths()
{
return defaultDateFormat.getDateFormatSymbols().getShortMonths();
}
public TimeZone getServerTimeZone()
{
return defaultDateFormat.getTimeZone();
}
private void setServerTimeZone(String serverTimeZoneId)
{
TimeZone serverTimeZone = TimeZone.getDefault();
if(serverTimeZoneId != null)
serverTimeZone = TimeZone.getTimeZone(serverTimeZoneId);
defaultDateFormat.setTimeZone(serverTimeZone);
if(recentDateFormat != null)
recentDateFormat.setTimeZone(serverTimeZone);
}