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