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 1417442 2012-12-05 14:32:02Z 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 @Override
77 public FTPFile parseFTPEntry(String entry)
78 {
79
80 FTPFile file = new FTPFile();
81 file.setRawListing(entry);
82 int type;
83
84 if (matches(entry))
85 {
86 String usr = group(1);
87 String filesize = group(2);
88 String datestr = group(3)+" "+group(4);
89 String typeStr = group(5);
90 String name = group(6);
91
92 try
93 {
94 file.setTimestamp(super.parseTimestamp(datestr));
95 }
96 catch (ParseException e)
97 {
98 // intentionally do nothing
99 }
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 }