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