001    /*
002      Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.util;
019    
020    import java.io.FilenameFilter;
021    import java.io.IOException;
022    import java.util.LinkedList;
023    import java.util.List;
024    
025    /**
026     * Provides recursive file listing, and replaces leading ~ by the
027     * user's home directory.  
028     */
029    public class File extends java.io.File {
030        public File(java.io.File file) {
031            this(file.getPath());
032        }
033        public File(String pathname) {
034            super(Files.expandFileName(pathname));
035        }
036        public File(File parent, String child) {
037            this(parent.getPath(),child);
038        }
039        public File(java.io.File parent, String child) {
040            this(parent.getPath(),child);
041        }
042        public File(String parent, String child) {
043            super(Files.expandFileName(parent),child);
044        }
045    
046        /**
047         * Recursively list files matching a filter
048         * @param filter list files matching this filter
049         * @return a List of File matching the filter
050         * @see #listFilesRecursively(FilenameFilter,List)
051         */
052        public List listFilesRecursively(FilenameFilter filter) {
053            LinkedList files = new LinkedList();
054            listFilesRecursively(filter,files);
055            return files;
056        }
057    
058        /**
059         * Recursively list files matching a filter
060         * @param filter list files matching this filter
061         * @param files add matching files to this list
062         * @see #listFilesRecursively(FilenameFilter)
063         */
064        public void listFilesRecursively(FilenameFilter filter, List files) {
065            String[] names = list();
066            if(names==null) return;
067            for (int i=0; i<names.length; i++) {
068                File file = new File(this,names[i]);
069                if (filter.accept(this,names[i]))
070                    files.add(file);
071                if (file.isDirectory())
072                    file.listFilesRecursively(filter,files);
073            }
074        }
075    
076        /**
077         * Gets a path relative to a parent directory of the file. 
078         *
079         * @param parent the directory to give a path relative to
080         * @return a path relative to parent, or getPath() if parent is
081         * not a parent of the file.
082         */
083        public String getRelativePath(File parent) throws IOException {
084            String parentPath = parent.getCanonicalPath();
085            String path = getCanonicalPath();
086            if (path.startsWith(parentPath)) {
087                return path.substring(parentPath.length()+1);
088            } else {
089                return getPath();
090            }
091        }
092    
093        public java.io.File[] listDirectories() {
094            return listFiles(Files.directoryFilter);
095        }
096    
097        public java.io.File[] listNonHiddenFiles() {
098            return listFiles(Files.nonHiddenFilter);
099        }
100    }
101