1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.net.ftp.parser;
19
20 import java.text.ParseException;
21
22 import org.apache.commons.net.ftp.FTPClientConfig;
23 import org.apache.commons.net.ftp.FTPFile;
24
25 /**
26 * @version $Id: OS400FTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
27 */
28
29 public class OS400FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
30 {
31 private static final String DEFAULT_DATE_FORMAT
32 = "yy/MM/dd HH:mm:ss"; //01/11/09 12:30:24
33
34
35
36 private static final String REGEX =
37 "(\\S+)\\s+" // user
38 + "(\\d+)\\s+" // size
39 + "(\\S+)\\s+(\\S+)\\s+" // date stuff
40 + "(\\*\\S+)\\s+" // *STMF/*DIR
41 + "(\\S+/?)\\s*"; // filename
42
43
44 /**
45 * The default constructor for a OS400FTPEntryParser object.
46 *
47 * @exception IllegalArgumentException
48 * Thrown if the regular expression is unparseable. Should not be seen
49 * under normal conditions. It it is seen, this is a sign that
50 * <code>REGEX</code> is not a valid regular expression.
51 */
52 public OS400FTPEntryParser()
53 {
54 this(null);
55 }
56
57 /**
58 * This constructor allows the creation of an OS400FTPEntryParser object
59 * with something other than the default configuration.
60 *
61 * @param config The {@link FTPClientConfig configuration} object used to
62 * configure this parser.
63 * @exception IllegalArgumentException
64 * Thrown if the regular expression is unparseable. Should not be seen
65 * under normal conditions. It it is seen, this is a sign that
66 * <code>REGEX</code> is not a valid regular expression.
67 * @since 1.4
68 */
69 public OS400FTPEntryParser(FTPClientConfig config)
70 {
71 super(REGEX);
72 configure(config);
73 }
74
75
76 public FTPFile parseFTPEntry(String entry)
77 {
78
79 FTPFile file = new FTPFile();
80 file.setRawListing(entry);
81 int type;
82
83 if (matches(entry))
84 {
85 String usr = group(1);
86 String filesize = group(2);
87 String datestr = group(3)+" "+group(4);
88 String typeStr = group(5);
89 String name = group(6);
90
91 try
92 {
93 file.setTimestamp(super.parseTimestamp(datestr));
94 }
95 catch (ParseException e)
96 {
97 // intentionally do nothing
98 }
99
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 }