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