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
018package org.apache.commons.net.ftp.parser;
019import java.util.Calendar;
020
021import org.apache.commons.net.ftp.FTPFile;
022
023/**
024 * Parser for the Connect Enterprise Unix FTP Server From Sterling Commerce.
025 * Here is a sample of the sort of output line this parser processes:
026 *  "-C--E-----FTP B QUA1I1      18128       41 Aug 12 13:56 QUADTEST"
027 * <P><B>
028 * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the
029 * DefaultFTPParserFactory by classname.  It will not be chosen
030 * by the autodetection scheme.
031 * </B>
032 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
033 * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
034 */
035public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl
036{
037
038    /**
039     * months abbreviations looked for by this parser.  Also used
040     * to determine <b>which</b> month has been matched by the parser.
041     */
042    private static final String MONTHS =
043        "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
044
045    /**
046     * this is the regular expression used by this parser.
047     */
048    private static final String REGEX =
049        "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
050        + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
051        + "(\\S*)\\s*" // 12
052        + "(\\S+)\\s*" // 13
053        + "(\\S*)\\s*" // 14 user
054        + "(\\d*)\\s*" // 15 group
055        + "(\\d*)\\s*" // 16 filesize
056        + MONTHS       // 17 month
057        + "\\s*"       // TODO should the space be optional?
058        // TODO \\d* should be \\d? surely ? Otherwise 01111 is allowed
059        + "((?:[012]\\d*)|(?:3[01]))\\s*" // 18 date [012]\d* or 3[01]
060        + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
061        // 20 \d\d\d\d  = year  OR
062        // 21 [01]\d or 2[0123] hour + ':'
063        // 22 [012345]\d = minute
064        + "(\\S*)(\\s*.*)"; // 23 name
065
066    /**
067     * The sole constructor for a EnterpriseUnixFTPEntryParser object.
068     *
069     */
070    public EnterpriseUnixFTPEntryParser()
071    {
072        super(REGEX);
073    }
074
075    /**
076     * Parses a line of a unix FTP server file listing and converts  it into a
077     * usable format in the form of an <code> FTPFile </code>  instance.  If
078     * the file listing line doesn't describe a file,  <code> null </code> is
079     * returned, otherwise a <code> FTPFile </code>  instance representing the
080     * files in the directory is returned.
081     *
082     * @param entry A line of text from the file listing
083     * @return An FTPFile instance corresponding to the supplied entry
084     */
085    @Override
086    public FTPFile parseFTPEntry(String entry)
087    {
088
089        FTPFile file = new FTPFile();
090        file.setRawListing(entry);
091
092        if (matches(entry))
093        {
094            String usr = group(14);
095            String grp = group(15);
096            String filesize = group(16);
097            String mo = group(17);
098            String da = group(18);
099            String yr = group(20);
100            String hr = group(21);
101            String min = group(22);
102            String name = group(23);
103
104            file.setType(FTPFile.FILE_TYPE);
105            file.setUser(usr);
106            file.setGroup(grp);
107            try
108            {
109                file.setSize(Long.parseLong(filesize));
110            }
111            catch (NumberFormatException e)
112            {
113                // intentionally do nothing
114            }
115
116            Calendar cal = Calendar.getInstance();
117            cal.set(Calendar.MILLISECOND, 0);
118            cal.set(Calendar.SECOND, 0);
119            cal.set(Calendar.MINUTE, 0);
120            cal.set(Calendar.HOUR_OF_DAY, 0);
121
122            int pos = MONTHS.indexOf(mo);
123            int month = pos / 4;
124            final int missingUnit; // the first missing unit
125            try
126            {
127
128                if (yr != null)
129                {
130                    // it's a year; there are no hours and minutes
131                    cal.set(Calendar.YEAR, Integer.parseInt(yr));
132                    missingUnit = Calendar.HOUR_OF_DAY;
133                }
134                else
135                {
136                    // it must be  hour/minute or we wouldn't have matched
137                    missingUnit = Calendar.SECOND;
138                    int year = cal.get(Calendar.YEAR);
139
140                    // if the month we're reading is greater than now, it must
141                    // be last year
142                    if (cal.get(Calendar.MONTH) < month)
143                    {
144                        year--;
145                    }
146                    cal.set(Calendar.YEAR, year);
147                    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hr));
148                    cal.set(Calendar.MINUTE, Integer.parseInt(min));
149                }
150                cal.set(Calendar.MONTH, month);
151                cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(da));
152                cal.clear(missingUnit);
153                file.setTimestamp(cal);
154            }
155            catch (NumberFormatException e)
156            {
157                // do nothing, date will be uninitialized
158            }
159            file.setName(name);
160
161            return file;
162        }
163        return null;
164    }
165}