    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;
    019
    020import java.text.ParseException;
    021
    022import org.apache.commons.net.ftp.FTPClientConfig;
    023import org.apache.commons.net.ftp.FTPFile;
    024
    025/**
    026 * Implementation of FTPFileEntryParser and FTPFileListParser for OS2 Systems.
    027 *
    028 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
    029 */
    030public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
    031
    032{
    033
    034    private static final String DEFAULT_DATE_FORMAT = "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 = "\\s*([0-9]+)\\s*" + "(\\s+|[A-Z]+)\\s*" + "(DIR|\\s+)\\s*" + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
    039            + "(\\S.*)";
    040
    041    /**
    042     * The default constructor for a OS2FTPEntryParser object.
    043     *
    044     * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. It it is seen, this is a
    045     *                                  sign that <code>REGEX</code> is not a valid regular expression.
    046     */
    047    public OS2FTPEntryParser() {
    048        this(null);
    049    }
    050
    051    /**
    052     * This constructor allows the creation of an OS2FTPEntryParser object with something other than the default configuration.
    053     *
    054     * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
    055     * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. It it is seen, this is a
    056     *                                  sign that <code>REGEX</code> is not a valid regular expression.
    057     * @since 1.4
    058     */
    059    public OS2FTPEntryParser(final FTPClientConfig config) {
    060        super(REGEX);
    061        configure(config);
    062    }
    063
    064    /**
    065     * Defines a default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
    066     *
    067     * @return the default configuration for this parser.
    068     */
    069    @Override
    070    protected FTPClientConfig getDefaultConfiguration() {
    071        return new FTPClientConfig(FTPClientConfig.SYST_OS2, DEFAULT_DATE_FORMAT, null);
    072    }
    073
    074    /**
    075     * Parses a line of an OS2 FTP server file listing and converts it into a usable format in the form of an <code> FTPFile </code> instance. If the file
    076     * listing line doesn't describe a file, <code> null </code> is returned, otherwise a <code> FTPFile </code> instance representing the files in the
    077     * directory is returned.
    078     *
    079     * @param entry A line of text from the file listing
    080     * @return An FTPFile instance corresponding to the supplied entry
    081     */
    082    @Override
    083    public FTPFile parseFTPEntry(final String entry) {
    084
    085        final FTPFile f = new FTPFile();
    086        if (matches(entry)) {
    087            final String size = group(1);
    088            final String attrib = group(2);
    089            final String dirString = group(3);
    090            final String datestr = group(4) + " " + group(5);
    091            final String name = group(6);
    092            try {
    093                f.setTimestamp(super.parseTimestamp(datestr));
    094            } catch (final ParseException e) {
    095                // intentionally do nothing
    096            }
    097
    098            // is it a DIR or a file
    099            if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR")) {
    100                f.setType(FTPFile.DIRECTORY_TYPE);
    101            } else {
    102                f.setType(FTPFile.FILE_TYPE);
    103            }
    104
    105            // set the name
    106            f.setName(name.trim());
    107
    108            // set the size
    109            f.setSize(Long.parseLong(size.trim()));
    110
    111            return f;
    112        }
    113        return null;
    114
    115    }
    116
    117}