001    /**
002     * Copyright (c) 2010, SIB. All rights reserved.
003     * 
004     * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005     * https://sourceforge.net/projects/javaprotlib/
006     * 
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions are met:
009     * Redistributions of source code must retain the above copyright notice, this
010     * list of conditions and the following disclaimer. Redistributions in binary
011     * form must reproduce the above copyright notice, this list of conditions and
012     * the following disclaimer in the documentation and/or other materials provided
013     * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014     * its contributors may be used to endorse or promote products derived from this
015     * software without specific prior written permission.
016     * 
017     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020     * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     */
028    package org.expasy.jpl.commons.base.io;
029    
030    
031    import java.io.File;
032    import java.io.FileNotFoundException;
033    import java.util.ArrayList;
034    import java.util.List;
035    import java.util.regex.Matcher;
036    import java.util.regex.Pattern;
037    
038    
039    /**
040     * Class to find all files matching a regular expression in a directory tree.
041     * 
042     * @author Markus Muller
043     * 
044     * @version 1.0
045     * 
046     */
047    public final class FileFinder {
048            
049            public static List<File> getFiles(final String root, final String regex)
050                throws FileNotFoundException {
051                    return getFiles(root, regex, true);
052            }
053            
054            /**
055             * Find all files in the directory sub-tree under root
056             * 
057             * @param root Root directory for search
058             * @param regex Regular expression to match file name
059             * @param recurse recursive search if true
060             * @return List of matched files throws FileNotFoundException if root does
061             *         not exist or is not a directory
062             */
063            public static List<File> getFiles(final String root, final String regex,
064                final boolean recurse) throws FileNotFoundException {
065                    
066                    final File rootDir = new File(root);
067                    
068                    if (!rootDir.isDirectory()) {
069                            throw new FileNotFoundException(root
070                                + " does not exist or is not a directory!");
071                    }
072                    
073                    final List<File> files =
074                        listFilesAndDirs(rootDir, Pattern.compile(regex), recurse);
075                    
076                    return files;
077            }
078            
079            public static List<File> getFiles(final String root, final Pattern pattern)
080                throws FileNotFoundException {
081                    return getFiles(root, pattern, true);
082            }
083            
084            /**
085             * Find all files in the directory sub-tree under root
086             * 
087             * @param root Root directory for search
088             * @param pattern Regular expression pattern to match file name
089             * @param recurse recursive search if true
090             * @return List of matched files throws FileNotFoundException if root does
091             *         not exist or is not a directory
092             */
093            public static List<File> getFiles(final String root, final Pattern pattern,
094                final boolean recurse) throws FileNotFoundException {
095                    
096                    final File rootDir = new File(root);
097                    
098                    if (!rootDir.isDirectory()) {
099                            throw new FileNotFoundException(root
100                                + " does not exist or is not a directory!");
101                    }
102                    
103                    final List<File> files = listFilesAndDirs(rootDir, pattern, recurse);
104                    
105                    return files;
106            }
107            
108            /**
109             * List all files and directories under root
110             * 
111             * @param root Root directory
112             * @param regex Regular expression for file names
113             * @param filter Filter object implementing regex
114             * @param recurse if true, the search is extended to subdirectories
115             * @return
116             */
117            private static List<File> listFilesAndDirs(final File root,
118                final Pattern pattern, final boolean recurse) {
119                    // List of files / directories
120                    final List<File> files = new ArrayList<File>();
121                    
122                    // Get files / directories in the directory
123                    final File[] entries = root.listFiles();
124                    
125                    Matcher matcher;
126                    
127                    // Go over entries
128                    for (final File entry : entries) {
129                            
130                            // If there is no filter or the filter accepts the
131                            // file / directory, add it to the list
132                            if (pattern != null) {
133                                    matcher = pattern.matcher(entry.getName());
134                                    
135                                    if (matcher.matches()) {
136                                            files.add(entry);
137                                    }
138                            }
139                            
140                            // If the file is a directory and the recurse flag
141                            // is set, recurse into the directory
142                            if (recurse && entry.isDirectory()) {
143                                    files.addAll(listFilesAndDirs(entry, pattern, recurse));
144                            }
145                    }
146                    
147                    // Return collection of files
148                    return files;
149            }
150    }