1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.net.ftp.parser;
17
18 import java.text.ParseException;
19
20 import org.apache.commons.net.ftp.FTPClientConfig;
21 import org.apache.commons.net.ftp.FTPFile;
22
23 /**
24 * @version $Id: OS400FTPEntryParser.java 155429 2005-02-26 13:13:04Z dirkv $
25 */
26
27 public class OS400FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
28 {
29 private static final String DEFAULT_DATE_FORMAT
30 = "yy/MM/dd HH:mm:ss";
31
32
33
34 private static final String REGEX =
35 "(\\S+)\\s+"
36 + "(\\d+)\\s+"
37 + "(\\S+)\\s+(\\S+)\\s+"
38 + "(\\*\\S+)\\s+"
39
40
41
42
43
44
45
46
47
48
49
50 public OS400FTPEntryParser()
51 {
52 this(null);
53 }
54
55 /**
56 * This constructor allows the creation of an OS400FTPEntryParser object
57 * with something other than the default configuration.
58 *
59 * @param config The {@link FTPClientConfig configuration} object used to
60 * configure this parser.
61 * @exception IllegalArgumentException
62 * Thrown if the regular expression is unparseable. Should not be seen
63 * under normal conditions. It it is seen, this is a sign that
64 * <code>REGEX</code> is not a valid regular expression.
65 * @since 1.4
66 */
67 public OS400FTPEntryParser(FTPClientConfig config)
68 {
69 super(REGEX);
70 configure(config);
71 }
72
73
74 public FTPFile parseFTPEntry(String entry)
75 {
76
77 FTPFile file = new FTPFile();
78 file.setRawListing(entry);
79 int type;
80
81 if (matches(entry))
82 {
83 String usr = group(1);
84 String filesize = group(2);
85 String datestr = group(3)+" "+group(4);
86 String typeStr = group(5);
87 String name = group(6);
88
89 try
90 {
91 file.setTimestamp(super.parseTimestamp(datestr));
92 }
93 catch (ParseException e)
94 {
95 return null;
96 }
97
98
99 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
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 }