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