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