commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mario Ivankovits <ma...@ops.co.at>
Subject [NET] OS400 FTP FileEntry parser
Date Wed, 17 Mar 2004 15:31:43 GMT
Here you will find my OS/400 FileEntry parser.
Maybe one would add this to commons.net?

Ciao,
Mario

---cut here---
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileListParserImpl;

import java.util.Calendar;

public class OS400FTPEntryParser extends FTPFileListParserImpl
{
    private static final String REGEX =
        "(\\S+)\\s+"                // user
        + "(\\d+)\\s+"                // size
        + "(\\d\\d)/(\\d\\d)/(\\d\\d)\\s+" // year/month/day
        + "(\\d\\d):(\\d\\d):(\\d\\d)\\s+" // hour/minutes/seconds
        + "(\\*\\S+)\\s+"                // *STMF/*DIR
        + "(\\S+/?)\\s*";                // filename

    public OS400FTPEntryParser()
    {
        super(REGEX);
    }

    public FTPFile parseFTPEntry(String entry)
    {

        FTPFile file = new FTPFile();
        file.setRawListing(entry);
        int type;

        if (matches(entry))
        {
            String usr = group(1);
            String filesize = group(2);
            String yr = group(3);
            String mo = group(4);
            String da = group(5);
            String hr = group(6);
            String min = group(7);
            String sec = group(8);
            String typeStr = group(9);
            String name = group(10);

            if (typeStr.equalsIgnoreCase("*STMF"))
            {
                type = FTPFile.FILE_TYPE;
            }
            else if (typeStr.equalsIgnoreCase("*DIR"))
            {
                type = FTPFile.DIRECTORY_TYPE;
            }
            else
            {
                type = FTPFile.FILE_TYPE;
            }

            file.setType(type);

            file.setUser(usr);

            try
            {
                file.setSize(Integer.parseInt(filesize));
            }
            catch (NumberFormatException e)
            {
                // intentionally do nothing
            }

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.HOUR_OF_DAY, 0);

            try
            {
                int year = Integer.parseInt(yr, 10);
                if (year < 70)
                {
                    year += 2000;
                }
                else
                {
                    year += 1900;
                }

                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, Integer.parseInt(mo, 10));
                cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(da, 10));

                cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr, 10));
                cal.set(Calendar.MINUTE, Integer.parseInt(min, 10));
                cal.set(Calendar.SECOND, Integer.parseInt(sec, 10));

                file.setTimestamp(cal);
            }
            catch (NumberFormatException e)
            {
                // do nothing, date will be uninitialized
            }

            if (name.endsWith("/"))
            {
                name = name.substring(0, name.length()-1);
            }
            int pos = name.lastIndexOf('/');
            if (pos > -1)
            {
                name=name.substring(pos+1);
            }

            file.setName(name);

            return file;
        }
        return null;
    }
}
---cut here---


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message