001/*
002 * Copyright 2001-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
003 * Distributed under the terms of either:
004 * - the common development and distribution license (CDDL), v1.0; or
005 * - the GNU Lesser General Public License, v2.1 or later
006 * $Id: FileUtils.java 3106 2006-03-13 17:53:50Z gbevin $
007 */
008package com.uwyn.jhighlight.tools;
009
010import java.io.File;
011import java.util.ArrayList;
012import java.util.Iterator;
013import java.util.regex.Pattern;
014
015/**
016 * Collection of utility methods to work with files.
017 * 
018 * @author Geert Bevin (gbevin[remove] at uwyn dot com)
019 * @version $Revision: 3106 $
020 * @since 1.0
021 */
022public abstract class FileUtils
023{
024        private FileUtils()
025        {
026        }
027
028        /**
029         * Recursively traverse a directory hierachy and obtain a list of all
030         * absolute file names.
031         * <p>Regular expression patterns can be provided to explicitly include
032         * and exclude certain file names.
033         * 
034         * @param file the directory whose file hierarchy will be traversed
035         * @param included an array of regular expression patterns that will be
036         * used to determine which files should be included; or
037         * <p><code>null</code> if all files should be included
038         * @param excluded an array of regular expression patterns that will be
039         * used to determine which files should be excluded; or
040         * <p><code>null</code> if no files should be excluded
041         * @return the list of absolute file names
042         * @since 1.0
043         */
044        public static ArrayList getFileList(File file, Pattern[] included, Pattern[] excluded)
045        {
046                return getFileList(file, included, excluded, true);
047        }
048        
049        private static ArrayList getFileList(File file, Pattern[] included, Pattern[] excluded, boolean root)
050        {
051                if (null == file)
052                {
053                        return new ArrayList();
054                }
055                
056                ArrayList filelist = new ArrayList();
057                if (file.isDirectory())
058                {
059                        String[] list = file.list();
060                        if (null != list)
061                        {
062                                String list_entry;
063                                for (int i = 0; i < list.length; i++)
064                                {
065                                        list_entry = list[i];
066                                        
067                                        File next_file = new File(file.getAbsolutePath() + File.separator + list_entry);
068                                        ArrayList dir = getFileList(next_file, included, excluded, false);
069                                        
070                                        Iterator dir_it = dir.iterator();
071                                        String file_name;
072                                        while (dir_it.hasNext())
073                                        {
074                                                file_name = (String)dir_it.next();
075                                                
076                                                if (root)
077                                                {
078                                                        // if the file is not accepted, don't process it further
079                                                        if (!StringUtils.filter(file_name, included, excluded))
080                                                        {
081                                                                continue;
082                                                        }
083                                                        
084                                                }
085                                                else
086                                                {
087                                                        file_name = file.getName() + File.separator + file_name;
088                                                }
089                                                
090                                                int filelist_size = filelist.size();
091                                                for (int j = 0; j < filelist_size; j++)
092                                                {
093                                                        if (((String)filelist.get(j)).compareTo(file_name) > 0)
094                                                        {
095                                                                filelist.add(j, file_name);
096                                                                break;
097                                                        }
098                                                }
099                                                if (filelist.size() == filelist_size)
100                                                {
101                                                        filelist.add(file_name);
102                                                }
103                                        }
104                                }
105                        }
106                }
107                else if (file.isFile())
108                {
109                        String  file_name = file.getName();
110                        
111                        if (root)
112                        {
113                                if (StringUtils.filter(file_name, included, excluded))
114                                {
115                                        filelist.add(file_name);
116                                }
117                        }
118                        else
119                        {
120                                filelist.add(file_name);
121                        }
122                }
123                
124                return filelist;
125        }
126        
127        public static String getExtension(String fileName)
128        {
129                if (null == fileName)   throw new IllegalArgumentException("fileName can't be null.");
130                
131                String  ext = null;
132                
133                int     index = fileName.lastIndexOf('.');
134                if (index > 0 &&  index < fileName.length() - 1)
135                {
136                        ext = fileName.substring(index+1).toLowerCase();
137                }
138                
139                return ext;
140        }
141}