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;
019
020import java.text.ParseException;
021
022import org.apache.commons.net.ftp.FTPClientConfig;
023import org.apache.commons.net.ftp.FTPFile;
024
025/**
026 * @version $Id: OS400FTPEntryParser.java 1489361 2013-06-04 09:48:36Z sebb $
027 */
028
029public class OS400FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
030{
031    private static final String DEFAULT_DATE_FORMAT
032        = "yy/MM/dd HH:mm:ss"; //01/11/09 12:30:24
033
034
035
036    private static final String REGEX =
037        "(\\S+)\\s+"                // user
038        + "(\\d+)\\s+"              // size
039        + "(\\S+)\\s+(\\S+)\\s+"    // date stuff
040        + "(\\*\\S+)\\s+"               // *STMF/*DIR
041        + "(\\S+/?)\\s*";               // filename
042
043
044    /**
045     * The default constructor for a OS400FTPEntryParser object.
046     *
047     * @exception IllegalArgumentException
048     * Thrown if the regular expression is unparseable.  Should not be seen
049     * under normal conditions.  It it is seen, this is a sign that
050     * <code>REGEX</code> is  not a valid regular expression.
051     */
052    public OS400FTPEntryParser()
053    {
054        this(null);
055    }
056
057    /**
058     * This constructor allows the creation of an OS400FTPEntryParser object
059     * with something other than the default configuration.
060     *
061     * @param config The {@link FTPClientConfig configuration} object used to
062     * configure this parser.
063     * @exception IllegalArgumentException
064     * Thrown if the regular expression is unparseable.  Should not be seen
065     * under normal conditions.  It it is seen, this is a sign that
066     * <code>REGEX</code> is  not a valid regular expression.
067     * @since 1.4
068     */
069    public OS400FTPEntryParser(FTPClientConfig config)
070    {
071        super(REGEX);
072        configure(config);
073    }
074
075
076//    @Override
077    public FTPFile parseFTPEntry(String entry)
078    {
079
080        FTPFile file = new FTPFile();
081        file.setRawListing(entry);
082        int type;
083
084        if (matches(entry))
085        {
086            String usr = group(1);
087            String filesize = group(2);
088            String datestr = group(3)+" "+group(4);
089            String typeStr = group(5);
090            String name = group(6);
091
092            try
093            {
094                file.setTimestamp(super.parseTimestamp(datestr));
095            }
096            catch (ParseException e)
097            {
098                // intentionally do nothing
099            }
100
101
102            if (typeStr.equalsIgnoreCase("*STMF"))
103            {
104                type = FTPFile.FILE_TYPE;
105            }
106            else if (typeStr.equalsIgnoreCase("*DIR"))
107            {
108                type = FTPFile.DIRECTORY_TYPE;
109            }
110            else
111            {
112                type = FTPFile.UNKNOWN_TYPE;
113            }
114
115            file.setType(type);
116
117            file.setUser(usr);
118
119            try
120            {
121                file.setSize(Long.parseLong(filesize));
122            }
123            catch (NumberFormatException e)
124            {
125                // intentionally do nothing
126            }
127
128            if (name.endsWith("/"))
129            {
130                name = name.substring(0, name.length() - 1);
131            }
132            int pos = name.lastIndexOf('/');
133            if (pos > -1)
134            {
135                name = name.substring(pos + 1);
136            }
137
138            file.setName(name);
139
140            return file;
141        }
142        return null;
143    }
144
145    /**
146     * Defines a default configuration to be used when this class is
147     * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
148     * parameter being specified.
149     * @return the default configuration for this parser.
150     */
151    @Override
152    protected FTPClientConfig getDefaultConfiguration() {
153        return new FTPClientConfig(
154                FTPClientConfig.SYST_OS400,
155                DEFAULT_DATE_FORMAT,
156                null, null, null, null);
157    }
158
159}