001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model.impl;
023
024import java.io.File;
025import java.io.FileFilter;
026import java.io.IOException;
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.List;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class DirectoryStack {
038
039    private static final Log log = LogFactory.getLog(DirectoryStack.class);
040
041    protected final List<File> dirs;
042
043    public DirectoryStack() {
044        dirs = new ArrayList<File>();
045    }
046
047    public DirectoryStack(List<File> entries) {
048        this();
049        dirs.addAll(entries);
050    }
051
052    public List<File> getDirectories() {
053        return dirs;
054    }
055
056    public boolean isEmpty() {
057        return dirs.isEmpty();
058    }
059
060    public void addDirectory(File dir) throws IOException {
061        dirs.add(dir.getCanonicalFile());
062    }
063
064    /**
065     * Gets the file given its name in this virtual directory.
066     * <p>
067     * The canonical file is returned if any file is found
068     *
069     * @param name the file name to lookup
070     * @return the file in the canonical form
071     * @throws IOException
072     */
073    public File getFile(String name) throws IOException {
074        for (File entry : dirs) {
075            File file = new File(entry, name);
076            if (file.exists()) {
077                return file.getCanonicalFile();
078            }
079        }
080        return null;
081    }
082
083    public File[] listFiles() {
084        List<File> result = new ArrayList<File>();
085        for (File entry : dirs) {
086            File[] files = entry.listFiles();
087            result.addAll(Arrays.asList(files));
088        }
089        return result.toArray(new File[result.size()]);
090    }
091
092    public File[] listFiles(FileFilter filter) {
093        List<File> result = new ArrayList<File>();
094        for (File entry : dirs) {
095            File[] files = entry.listFiles(filter);
096            result.addAll(Arrays.asList(files));
097        }
098        return result.toArray(new File[result.size()]);
099    }
100
101    public static void main(String[] args) {
102        try {
103            DirectoryStack vd = new DirectoryStack();
104            vd.addDirectory(new File("/home/bstefanescu/Desktop"));
105            vd.addDirectory(new File("/home/bstefanescu/src"));
106
107            for (File file : vd.listFiles()) {
108                System.out.println("> " + file);
109            }
110            System.out.println("dummy: " + vd.getFile("dummy"));
111            System.out.println("dev: " + vd.getFile("dev"));
112        } catch (IOException e) {
113            log.error(e, e);
114        }
115    }
116
117}