    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.text.ParseException;
    020
    021import org.apache.commons.net.ftp.FTPClientConfig;
    022import org.apache.commons.net.ftp.FTPFile;
    023
    024/**
    025 * Implementation of FTPFileEntryParser and FTPFileListParser for OS2 Systems.
    026 *
    027 * @author <a href="Winston.Ojeda@qg.com">Winston Ojeda</a>
    028 * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
    029 * @version $Id: OS2FTPEntryParser.java 1489361 2013-06-04 09:48:36Z sebb $
    030 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
    031 */
    032public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
    033
    034{
    035
    036    private static final String DEFAULT_DATE_FORMAT
    037        = "MM-dd-yy HH:mm"; //11-09-01 12:30
    038    /**
    039     * this is the regular expression used by this parser.
    040     */
    041    private static final String REGEX =
    042        "\\s*([0-9]+)\\s*"
    043        + "(\\s+|[A-Z]+)\\s*"
    044        + "(DIR|\\s+)\\s*"
    045        + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
    046        + "(\\S.*)";
    047
    048    /**
    049     * The default constructor for a OS2FTPEntryParser object.
    050     *
    051     * @exception IllegalArgumentException
    052     * Thrown if the regular expression is unparseable.  Should not be seen
    053     * under normal conditions.  It it is seen, this is a sign that
    054     * <code>REGEX</code> is  not a valid regular expression.
    055     */
    056    public OS2FTPEntryParser()
    057    {
    058        this(null);
    059    }
    060
    061    /**
    062     * This constructor allows the creation of an OS2FTPEntryParser object
    063     * with something other than the default configuration.
    064     *
    065     * @param config The {@link FTPClientConfig configuration} object used to
    066     * configure this parser.
    067     * @exception IllegalArgumentException
    068     * Thrown if the regular expression is unparseable.  Should not be seen
    069     * under normal conditions.  It it is seen, this is a sign that
    070     * <code>REGEX</code> is  not a valid regular expression.
    071     * @since 1.4
    072     */
    073     public OS2FTPEntryParser(FTPClientConfig config)
    074    {
    075        super(REGEX);
    076        configure(config);
    077    }
    078
    079    /**
    080     * Parses a line of an OS2 FTP server file listing and converts it into a
    081     * usable format in the form of an <code> FTPFile </code> instance.  If the
    082     * file listing line doesn't describe a file, <code> null </code> is
    083     * returned, otherwise a <code> FTPFile </code> instance representing the
    084     * files in the directory is returned.
    085     * <p>
    086     * @param entry A line of text from the file listing
    087     * @return An FTPFile instance corresponding to the supplied entry
    088     */
    089//    @Override
    090    public FTPFile parseFTPEntry(String entry)
    091    {
    092
    093        FTPFile f = new FTPFile();
    094        if (matches(entry))
    095        {
    096            String size = group(1);
    097            String attrib = group(2);
    098            String dirString = group(3);
    099            String datestr = group(4)+" "+group(5);
    100            String name = group(6);
    101            try
    102            {
    103                f.setTimestamp(super.parseTimestamp(datestr));
    104            }
    105            catch (ParseException e)
    106            {
    107                // intentionally do nothing
    108            }
    109
    110
    111            //is it a DIR or a file
    112            if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR"))
    113            {
    114                f.setType(FTPFile.DIRECTORY_TYPE);
    115            }
    116            else
    117            {
    118                f.setType(FTPFile.FILE_TYPE);
    119            }
    120
    121
    122            //set the name
    123            f.setName(name.trim());
    124
    125            //set the size
    126            f.setSize(Long.parseLong(size.trim()));
    127
    128            return (f);
    129        }
    130        return null;
    131
    132    }
    133
    134    /**
    135     * Defines a default configuration to be used when this class is
    136     * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
    137     * parameter being specified.
    138     * @return the default configuration for this parser.
    139     */
    140    @Override
    141    protected FTPClientConfig getDefaultConfiguration() {
    142        return new FTPClientConfig(
    143                FTPClientConfig.SYST_OS2,
    144                DEFAULT_DATE_FORMAT,
    145                null, null, null, null);
    146    }
    147
    148}