001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library 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 GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model.impl;
021
022import java.io.File;
023import java.io.FileFilter;
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public class DirectoryStack {
036
037    private static final Log log = LogFactory.getLog(DirectoryStack.class);
038
039    protected final List<File> dirs;
040
041    public DirectoryStack() {
042        dirs = new ArrayList<File>();
043    }
044
045    public DirectoryStack(List<File> entries) {
046        this();
047        dirs.addAll(entries);
048    }
049
050    public List<File> getDirectories() {
051        return dirs;
052    }
053
054    public boolean isEmpty() {
055        return dirs.isEmpty();
056    }
057
058    public void addDirectory(File dir) throws IOException {
059        dirs.add(dir.getCanonicalFile());
060    }
061
062    /**
063     * Gets the file given its name in this virtual directory.
064     * <p>
065     * The canonical file is returned if any file is found
066     *
067     * @param name the file name to lookup
068     * @return the file in the canonical form
069     * @throws IOException
070     */
071    public File getFile(String name) throws IOException {
072        for (File entry : dirs) {
073            File file = new File(entry, name);
074            if (file.exists()) {
075                return file.getCanonicalFile();
076            }
077        }
078        return null;
079    }
080
081    public File[] listFiles() {
082        List<File> result = new ArrayList<File>();
083        for (File entry : dirs) {
084            File[] files = entry.listFiles();
085            result.addAll(Arrays.asList(files));
086        }
087        return result.toArray(new File[result.size()]);
088    }
089
090    public File[] listFiles(FileFilter filter) {
091        List<File> result = new ArrayList<File>();
092        for (File entry : dirs) {
093            File[] files = entry.listFiles(filter);
094            result.addAll(Arrays.asList(files));
095        }
096        return result.toArray(new File[result.size()]);
097    }
098
099    public static void main(String[] args) {
100        try {
101            DirectoryStack vd = new DirectoryStack();
102            vd.addDirectory(new File("/home/bstefanescu/Desktop"));
103            vd.addDirectory(new File("/home/bstefanescu/src"));
104
105            for (File file : vd.listFiles()) {
106                System.out.println("> " + file);
107            }
108            System.out.println("dummy: " + vd.getFile("dummy"));
109            System.out.println("dev: " + vd.getFile("dev"));
110        } catch (IOException e) {
111            log.error(e, e);
112        }
113    }
114
115}