001    /*
002     * ftp4j - A pure Java FTP client library
003     * 
004     * Copyright (C) 2008 Carlo Pelliccia (www.sauronsoftware.it)
005     * 
006     * This program is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation, either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     */
019    package it.sauronsoftware.ftp4j.extrecognizers;
020    
021    import java.util.ArrayList;
022    
023    import it.sauronsoftware.ftp4j.FTPClient;
024    import it.sauronsoftware.ftp4j.FTPTextualExtensionRecognizer;
025    
026    /**
027     * A textual extension recognizer with parametric extensions, which can be added
028     * or removed at runtime.
029     * 
030     * @author Carlo Pelliccia
031     * @see FTPClient#setTextualExtensionRecognizer(FTPTextualExtensionRecognizer)
032     */
033    public class ParametricTextualExtensionRecognizer implements
034                    FTPTextualExtensionRecognizer {
035    
036            /**
037             * Extension list.
038             */
039            private ArrayList exts = new ArrayList();
040    
041            /**
042             * It builds the recognizer with an empty extension list.
043             */
044            public ParametricTextualExtensionRecognizer() {
045                    ;
046            }
047    
048            /**
049             * It builds the recognizer with an initial extension list.
050             * 
051             * @param exts
052             *            The initial extension list.
053             */
054            public ParametricTextualExtensionRecognizer(String[] exts) {
055                    for (int i = 0; i < exts.length; i++) {
056                            addExtension(exts[i]);
057                    }
058            }
059    
060            /**
061             * It builds the recognizer with an initial extension list.
062             * 
063             * @param exts
064             *            The initial extension list.
065             */
066            public ParametricTextualExtensionRecognizer(ArrayList exts) {
067                    int size = exts.size();
068                    for (int i = 0; i < size; i++) {
069                            Object aux = exts.get(i);
070                            if (aux instanceof String) {
071                                    String ext = (String) aux;
072                                    addExtension(ext);
073                            }
074                    }
075            }
076    
077            /**
078             * This method adds an extension to the recognizer.
079             * 
080             * @param ext
081             *            The extension.
082             */
083            public void addExtension(String ext) {
084                    synchronized (exts) {
085                            ext = ext.toLowerCase();
086                            exts.add(ext);
087                    }
088            }
089    
090            /**
091             * This method removes an extension to the recognizer.
092             * 
093             * @param ext
094             *            The extension to be removed.
095             */
096            public void removeExtension(String ext) {
097                    synchronized (exts) {
098                            ext = ext.toLowerCase();
099                            exts.remove(ext);
100                    }
101            }
102    
103            /**
104             * This method returns the recognized extension list.
105             * 
106             * @return The list with all the extensions recognized to be for textual
107             *         files.
108             */
109            public String[] getExtensions() {
110                    synchronized (exts) {
111                            int size = exts.size();
112                            String[] ret = new String[size];
113                            for (int i = 0; i < size; i++) {
114                                    ret[i] = (String) exts.get(i);
115                            }
116                            return ret;
117                    }
118            }
119    
120            public boolean isTextualExt(String ext) {
121                    synchronized (exts) {
122                            return exts.contains(ext);
123                    }
124            }
125    
126    }