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