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