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