001    /*
002     * Copyright 2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net.ftp.parser;
017    
018    import org.apache.commons.net.ftp.FTPFile;
019    import org.apache.commons.net.ftp.FTPFileEntryParser;
020    import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
021    
022    /**
023     * This implementation allows to pack some FileEntryParsers together
024     * and handle the case where to returned dirstyle isnt clearly defined.
025     * The matching parser will be cached.
026     * If the cached parser wont match due to the server changed the dirstyle,
027     * a new matching parser will be searched.
028     *
029     * @author Mario Ivankovits <mario@ops.co.at>
030     */
031    public class CompositeFileEntryParser extends FTPFileEntryParserImpl
032    {
033        private final FTPFileEntryParser[] ftpFileEntryParsers;
034        private FTPFileEntryParser cachedFtpFileEntryParser;
035    
036        public CompositeFileEntryParser(FTPFileEntryParser[] ftpFileEntryParsers)
037        {
038            super();
039    
040            this.cachedFtpFileEntryParser = null;
041            this.ftpFileEntryParsers = ftpFileEntryParsers;
042        }
043    
044        public FTPFile parseFTPEntry(String listEntry)
045        {
046            if (cachedFtpFileEntryParser != null)
047            {
048                FTPFile matched = cachedFtpFileEntryParser.parseFTPEntry(listEntry);
049                if (matched != null)
050                {
051                    return matched;
052                }
053            }
054            else
055            {
056                for (int iterParser=0; iterParser < ftpFileEntryParsers.length; iterParser++)
057                {
058                    FTPFileEntryParser ftpFileEntryParser = ftpFileEntryParsers[iterParser];
059    
060                    FTPFile matched = ftpFileEntryParser.parseFTPEntry(listEntry);
061                    if (matched != null)
062                    {
063                        cachedFtpFileEntryParser = ftpFileEntryParser;
064                        return matched;
065                    }
066                }
067            }
068            return null;
069        }
070    }