01: /*
02: * Copyright 2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.net.ftp.parser;
17:
18: import org.apache.commons.net.ftp.FTPFile;
19: import org.apache.commons.net.ftp.FTPFileEntryParser;
20: import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
21:
22: /**
23: * This implementation allows to pack some FileEntryParsers together
24: * and handle the case where to returned dirstyle isnt clearly defined.
25: * The matching parser will be cached.
26: * If the cached parser wont match due to the server changed the dirstyle,
27: * a new matching parser will be searched.
28: *
29: * @author Mario Ivankovits <mario@ops.co.at>
30: */
31: public class CompositeFileEntryParser extends FTPFileEntryParserImpl {
32: private final FTPFileEntryParser[] ftpFileEntryParsers;
33: private FTPFileEntryParser cachedFtpFileEntryParser;
34:
35: public CompositeFileEntryParser(
36: FTPFileEntryParser[] ftpFileEntryParsers) {
37: super ();
38:
39: this .cachedFtpFileEntryParser = null;
40: this .ftpFileEntryParsers = ftpFileEntryParsers;
41: }
42:
43: public FTPFile parseFTPEntry(String listEntry) {
44: if (cachedFtpFileEntryParser != null) {
45: FTPFile matched = cachedFtpFileEntryParser
46: .parseFTPEntry(listEntry);
47: if (matched != null) {
48: return matched;
49: }
50: } else {
51: for (int iterParser = 0; iterParser < ftpFileEntryParsers.length; iterParser++) {
52: FTPFileEntryParser ftpFileEntryParser = ftpFileEntryParsers[iterParser];
53:
54: FTPFile matched = ftpFileEntryParser
55: .parseFTPEntry(listEntry);
56: if (matched != null) {
57: cachedFtpFileEntryParser = ftpFileEntryParser;
58: return matched;
59: }
60: }
61: }
62: return null;
63: }
64: }
|