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