1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.ftp.parser;
19
20 import java.util.regex.Pattern;
21
22 import org.apache.commons.net.ftp.Configurable;
23 import org.apache.commons.net.ftp.FTPClientConfig;
24 import org.apache.commons.net.ftp.FTPFileEntryParser;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class DefaultFTPFileEntryParserFactory
38 implements FTPFileEntryParserFactory
39 {
40
41
42 private static final String JAVA_IDENTIFIER = "\\p{javaJavaIdentifierStart}(\\p{javaJavaIdentifierPart})*";
43
44 private static final String JAVA_QUALIFIED_NAME = "("+JAVA_IDENTIFIER+"\\.)+"+JAVA_IDENTIFIER;
45
46 private static final Pattern JAVA_QUALIFIED_NAME_PATTERN = Pattern.compile(JAVA_QUALIFIED_NAME);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public FTPFileEntryParser createFileEntryParser(String key)
91 {
92 if (key == null) {
93 throw new ParserInitializationException("Parser key cannot be null");
94 }
95 return createFileEntryParser(key, null);
96 }
97
98
99 private FTPFileEntryParser createFileEntryParser(String key, FTPClientConfig config) {
100 FTPFileEntryParser parser = null;
101
102
103 if (JAVA_QUALIFIED_NAME_PATTERN.matcher(key).matches()) {
104 try
105 {
106 Class<?> parserClass = Class.forName(key);
107 try {
108 parser = (FTPFileEntryParser) parserClass.newInstance();
109 } catch (ClassCastException e) {
110 throw new ParserInitializationException(parserClass.getName()
111 + " does not implement the interface "
112 + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
113 } catch (Exception e) {
114 throw new ParserInitializationException("Error initializing parser", e);
115 } catch (ExceptionInInitializerError e) {
116 throw new ParserInitializationException("Error initializing parser", e);
117 }
118 } catch (ClassNotFoundException e) {
119
120 }
121 }
122
123 if (parser == null) {
124 String ukey = key.toUpperCase(java.util.Locale.ENGLISH);
125 if (ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0)
126 {
127 parser = new UnixFTPEntryParser(config);
128 }
129 else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0)
130 {
131 parser = new VMSVersioningFTPEntryParser(config);
132 }
133 else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0)
134 {
135 parser = createNTFTPEntryParser(config);
136 }
137 else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0)
138 {
139 parser = new OS2FTPEntryParser(config);
140 }
141 else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0 ||
142 ukey.indexOf(FTPClientConfig.SYST_AS400) >= 0)
143 {
144 parser = createOS400FTPEntryParser(config);
145 }
146 else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0)
147 {
148 parser = new MVSFTPEntryParser();
149 }
150 else if (ukey.indexOf(FTPClientConfig.SYST_NETWARE) >= 0)
151 {
152 parser = new NetwareFTPEntryParser(config);
153 }
154 else if (ukey.indexOf(FTPClientConfig.SYST_MACOS_PETER) >= 0)
155 {
156 parser = new MacOsPeterFTPEntryParser(config);
157 }
158 else if (ukey.indexOf(FTPClientConfig.SYST_L8) >= 0)
159 {
160
161
162 parser = new UnixFTPEntryParser(config);
163 }
164 else
165 {
166 throw new ParserInitializationException("Unknown parser type: " + key);
167 }
168 }
169
170 if (parser instanceof Configurable) {
171 ((Configurable)parser).configure(config);
172 }
173 return parser;
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 public FTPFileEntryParser createFileEntryParser(FTPClientConfig config)
199 throws ParserInitializationException
200 {
201 String key = config.getServerSystemKey();
202 return createFileEntryParser(key, config);
203 }
204
205
206 public FTPFileEntryParser createUnixFTPEntryParser()
207 {
208 return new UnixFTPEntryParser();
209 }
210
211 public FTPFileEntryParser createVMSVersioningFTPEntryParser()
212 {
213 return new VMSVersioningFTPEntryParser();
214 }
215
216 public FTPFileEntryParser createNetwareFTPEntryParser() {
217 return new NetwareFTPEntryParser();
218 }
219
220 public FTPFileEntryParser createNTFTPEntryParser()
221 {
222 return createNTFTPEntryParser(null);
223 }
224
225
226
227
228
229
230
231
232 private FTPFileEntryParser createNTFTPEntryParser(FTPClientConfig config)
233 {
234 if (config != null && FTPClientConfig.SYST_NT.equals(
235 config.getServerSystemKey()))
236 {
237 return new NTFTPEntryParser(config);
238 } else {
239 return new CompositeFileEntryParser(new FTPFileEntryParser[]
240 {
241 new NTFTPEntryParser(config),
242 new UnixFTPEntryParser(config)
243 });
244 }
245 }
246
247 public FTPFileEntryParser createOS2FTPEntryParser()
248 {
249 return new OS2FTPEntryParser();
250 }
251
252 public FTPFileEntryParser createOS400FTPEntryParser()
253 {
254 return createOS400FTPEntryParser(null);
255 }
256
257
258
259
260
261
262
263
264 private FTPFileEntryParser createOS400FTPEntryParser(FTPClientConfig config)
265 {
266 if (config != null &&
267 FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey()))
268 {
269 return new OS400FTPEntryParser(config);
270 } else {
271 return new CompositeFileEntryParser(new FTPFileEntryParser[]
272 {
273 new OS400FTPEntryParser(config),
274 new UnixFTPEntryParser(config)
275 });
276 }
277 }
278
279 public FTPFileEntryParser createMVSEntryParser()
280 {
281 return new MVSFTPEntryParser();
282 }
283
284 }
285