001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.instrumentation.file;
022:
023: import java.io.*;
024: import java.util.*;
025:
026: public class DefaultFilePathRoot implements FilePathRoot {
027:
028: private final String[] _rootDirs;
029: private final String _extension;
030:
031: public DefaultFilePathRoot(String[] rootDirs) {
032: this (rootDirs, "");
033: }
034:
035: public DefaultFilePathRoot(String[] rootDirs, String extension) {
036: _rootDirs = rootDirs;
037: _extension = extension;
038: }
039:
040: public Iterator files() {
041: return new FileSystemIterator(_rootDirs, _extension);
042: }
043:
044: public String[] rootDirs() {
045: return _rootDirs;
046: }
047:
048: private static class FileSystemIterator implements Iterator {
049: private final String _extension;
050: private LinkedList _stack = new LinkedList();
051:
052: public FileSystemIterator(String[] roots, String extension) {
053: _extension = extension;
054: for (int rootIdx = 0; rootIdx < roots.length; rootIdx++) {
055: File root = new File(roots[rootIdx]);
056: push(new FileWithRoot(root, root));
057: }
058: advanceQueue();
059: }
060:
061: public boolean hasNext() {
062: return !_stack.isEmpty();
063: }
064:
065: public Object next() {
066: FileWithRoot top = pop();
067: advanceQueue();
068: return top;
069: }
070:
071: public void remove() {
072: throw new UnsupportedOperationException();
073: }
074:
075: private void advanceQueue() {
076: while (!_stack.isEmpty() && !accept(peek())) {
077: FileWithRoot dir = pop();
078: if (!dir.file().isDirectory()) {
079: continue;
080: }
081: File[] children = dir.file().listFiles();
082: for (int childIdx = 0; childIdx < children.length; childIdx++) {
083: _stack.addFirst(new FileWithRoot(dir.root(),
084: children[childIdx]));
085: }
086: }
087: }
088:
089: private boolean accept(FileWithRoot file) {
090: return file.file().isFile()
091: && file.file().getName().endsWith(_extension);
092: }
093:
094: private void push(FileWithRoot root) {
095: _stack.addFirst(root);
096: }
097:
098: private FileWithRoot pop() {
099: return (FileWithRoot) _stack.removeFirst();
100: }
101:
102: private FileWithRoot peek() {
103: return (FileWithRoot) _stack.getFirst();
104: }
105: }
106: }
|