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