    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;
    021import java.util.regex.Pattern;
    022
    023import org.apache.commons.net.ftp.Configurable;
    024import org.apache.commons.net.ftp.FTPClientConfig;
    025import org.apache.commons.net.ftp.FTPFile;
    026
    027/**
    028 * Implementation of FTPFileEntryParser and FTPFileListParser for NT Systems.
    029 *
    030 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
    031 */
    032public class NTFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {
    033
    034    private static final String DEFAULT_DATE_FORMAT = "MM-dd-yy hh:mma"; // 11-09-01 12:30PM
    035
    036    private static final String DEFAULT_DATE_FORMAT2 = "MM-dd-yy kk:mm"; // 11-09-01 18:30
    037
    038    /**
    039     * this is the regular expression used by this parser.
    040     */
    041    private static final String REGEX = "(\\S+)\\s+(\\S+)\\s+" // MM-dd-yy whitespace hh:mma|kk:mm; swallow trailing spaces
    042            + "(?:(<DIR>)|([0-9]+))\\s+" // <DIR> or ddddd; swallow trailing spaces
    043            + "(\\S.*)"; // First non-space followed by rest of line (name)
    044
    045    private final FTPTimestampParser timestampParser;
    046
    047    /**
    048     * The sole constructor for an NTFTPEntryParser object.
    049     *
    050     * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. It it is seen, this is a
    051     *                                  sign that <code>REGEX</code> is not a valid regular expression.
    052     */
    053    public NTFTPEntryParser() {
    054        this(null);
    055    }
    056
    057    /**
    058     * This constructor allows the creation of an NTFTPEntryParser object with something other than the default configuration.
    059     *
    060     * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
    061     * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. It it is seen, this is a
    062     *                                  sign that <code>REGEX</code> is not a valid regular expression.
    063     * @since 1.4
    064     */
    065    public NTFTPEntryParser(final FTPClientConfig config) {
    066        super(REGEX, Pattern.DOTALL);
    067        configure(config);
    068        final FTPClientConfig config2 = new FTPClientConfig(FTPClientConfig.SYST_NT, DEFAULT_DATE_FORMAT2, null);
    069        config2.setDefaultDateFormatStr(DEFAULT_DATE_FORMAT2);
    070        this.timestampParser = new FTPTimestampParserImpl();
    071        ((Configurable) this.timestampParser).configure(config2);
    072    }
    073
    074    /**
    075     * Defines a default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
    076     *
    077     * @return the default configuration for this parser.
    078     */
    079    @Override
    080    public FTPClientConfig getDefaultConfiguration() {
    081        return new FTPClientConfig(FTPClientConfig.SYST_NT, DEFAULT_DATE_FORMAT, null);
    082    }
    083
    084    /**
    085     * Parses a line of an NT FTP server file listing and converts it into a usable format in the form of an <code> FTPFile </code> instance. If the file
    086     * listing line doesn't describe a file, <code> null </code> is returned, otherwise a <code> FTPFile </code> instance representing the files in the
    087     * directory is returned.
    088     *
    089     * @param entry A line of text from the file listing
    090     * @return An FTPFile instance corresponding to the supplied entry
    091     */
    092    @Override
    093    public FTPFile parseFTPEntry(final String entry) {
    094        final FTPFile f = new FTPFile();
    095        f.setRawListing(entry);
    096
    097        if (matches(entry)) {
    098            final String datestr = group(1) + " " + group(2);
    099            final String dirString = group(3);
    100            final String size = group(4);
    101            final String name = group(5);
    102            try {
    103                f.setTimestamp(super.parseTimestamp(datestr));
    104            } catch (final ParseException e) {
    105                // parsing fails, try the other date format
    106                try {
    107                    f.setTimestamp(timestampParser.parseTimestamp(datestr));
    108                } catch (final ParseException e2) {
    109                    // intentionally do nothing
    110                }
    111            }
    112
    113            if (null == name || name.equals(".") || name.equals("..")) {
    114                return null;
    115            }
    116            f.setName(name);
    117
    118            if ("<DIR>".equals(dirString)) {
    119                f.setType(FTPFile.DIRECTORY_TYPE);
    120                f.setSize(0);
    121            } else {
    122                f.setType(FTPFile.FILE_TYPE);
    123                if (null != size) {
    124                    f.setSize(Long.parseLong(size));
    125                }
    126            }
    127            return f;
    128        }
    129        return null;
    130    }
    131
    132}