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
020 import java.text.ParseException;
021
022 import org.apache.commons.net.ftp.FTPClientConfig;
023 import org.apache.commons.net.ftp.FTPFile;
024
025 /**
026 * @version $Id: OS400FTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
027 */
028
029 public 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 public FTPFile parseFTPEntry(String entry)
077 {
078
079 FTPFile file = new FTPFile();
080 file.setRawListing(entry);
081 int type;
082
083 if (matches(entry))
084 {
085 String usr = group(1);
086 String filesize = group(2);
087 String datestr = group(3)+" "+group(4);
088 String typeStr = group(5);
089 String name = group(6);
090
091 try
092 {
093 file.setTimestamp(super.parseTimestamp(datestr));
094 }
095 catch (ParseException e)
096 {
097 // intentionally do nothing
098 }
099
100
101 if (typeStr.equalsIgnoreCase("*STMF"))
102 {
103 type = FTPFile.FILE_TYPE;
104 }
105 else if (typeStr.equalsIgnoreCase("*DIR"))
106 {
107 type = FTPFile.DIRECTORY_TYPE;
108 }
109 else
110 {
111 type = FTPFile.UNKNOWN_TYPE;
112 }
113
114 file.setType(type);
115
116 file.setUser(usr);
117
118 try
119 {
120 file.setSize(Long.parseLong(filesize));
121 }
122 catch (NumberFormatException e)
123 {
124 // intentionally do nothing
125 }
126
127 if (name.endsWith("/"))
128 {
129 name = name.substring(0, name.length() - 1);
130 }
131 int pos = name.lastIndexOf('/');
132 if (pos > -1)
133 {
134 name = name.substring(pos + 1);
135 }
136
137 file.setName(name);
138
139 return file;
140 }
141 return null;
142 }
143
144 /**
145 * Defines a default configuration to be used when this class is
146 * instantiated without a {@link FTPClientConfig FTPClientConfig}
147 * parameter being specified.
148 * @return the default configuration for this parser.
149 */
150 @Override
151 protected FTPClientConfig getDefaultConfiguration() {
152 return new FTPClientConfig(
153 FTPClientConfig.SYST_OS400,
154 DEFAULT_DATE_FORMAT,
155 null, null, null, null);
156 }
157
158 }