001 /*
002 * Copyright 2004-2005 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.commons.net.ftp.parser;
017
018 import java.text.ParseException;
019
020 import org.apache.commons.net.ftp.FTPClientConfig;
021 import org.apache.commons.net.ftp.FTPFile;
022
023 /**
024 * @version $Id: OS400FTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $
025 */
026
027 public class OS400FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
028 {
029 private static final String DEFAULT_DATE_FORMAT
030 = "yy/MM/dd HH:mm:ss"; //01/11/09 12:30:24
031
032
033
034 private static final String REGEX =
035 "(\\S+)\\s+" // user
036 + "(\\d+)\\s+" // size
037 + "(\\S+)\\s+(\\S+)\\s+" // date stuff
038 + "(\\*\\S+)\\s+" // *STMF/*DIR
039 + "(\\S+/?)\\s*"; // filename
040
041
042 /**
043 * The default constructor for a OS400FTPEntryParser object.
044 *
045 * @exception IllegalArgumentException
046 * Thrown if the regular expression is unparseable. Should not be seen
047 * under normal conditions. It it is seen, this is a sign that
048 * <code>REGEX</code> is not a valid regular expression.
049 */
050 public OS400FTPEntryParser()
051 {
052 this(null);
053 }
054
055 /**
056 * This constructor allows the creation of an OS400FTPEntryParser object
057 * with something other than the default configuration.
058 *
059 * @param config The {@link FTPClientConfig configuration} object used to
060 * configure this parser.
061 * @exception IllegalArgumentException
062 * Thrown if the regular expression is unparseable. Should not be seen
063 * under normal conditions. It it is seen, this is a sign that
064 * <code>REGEX</code> is not a valid regular expression.
065 * @since 1.4
066 */
067 public OS400FTPEntryParser(FTPClientConfig config)
068 {
069 super(REGEX);
070 configure(config);
071 }
072
073
074 public FTPFile parseFTPEntry(String entry)
075 {
076
077 FTPFile file = new FTPFile();
078 file.setRawListing(entry);
079 int type;
080
081 if (matches(entry))
082 {
083 String usr = group(1);
084 String filesize = group(2);
085 String datestr = group(3)+" "+group(4);
086 String typeStr = group(5);
087 String name = group(6);
088
089 try
090 {
091 file.setTimestamp(super.parseTimestamp(datestr));
092 }
093 catch (ParseException e)
094 {
095 return null; // this is a parsing failure too.
096 }
097
098
099 if (typeStr.equalsIgnoreCase("*STMF"))
100 {
101 type = FTPFile.FILE_TYPE;
102 }
103 else if (typeStr.equalsIgnoreCase("*DIR"))
104 {
105 type = FTPFile.DIRECTORY_TYPE;
106 }
107 else
108 {
109 type = FTPFile.UNKNOWN_TYPE;
110 }
111
112 file.setType(type);
113
114 file.setUser(usr);
115
116 try
117 {
118 file.setSize(Long.parseLong(filesize));
119 }
120 catch (NumberFormatException e)
121 {
122 // intentionally do nothing
123 }
124
125 if (name.endsWith("/"))
126 {
127 name = name.substring(0, name.length() - 1);
128 }
129 int pos = name.lastIndexOf('/');
130 if (pos > -1)
131 {
132 name = name.substring(pos + 1);
133 }
134
135 file.setName(name);
136
137 return file;
138 }
139 return null;
140 }
141
142 /**
143 * Defines a default configuration to be used when this class is
144 * instantiated without a {@link FTPClientConfig FTPClientConfig}
145 * parameter being specified.
146 * @return the default configuration for this parser.
147 */
148 protected FTPClientConfig getDefaultConfiguration() {
149 return new FTPClientConfig(
150 FTPClientConfig.SYST_OS400,
151 DEFAULT_DATE_FORMAT,
152 null, null, null, null);
153 }
154
155 }