001 /* 002 * mesopotamia @mesopotamia.version@ 003 * Multilingual parser and repository. 004 * Copyright (C) 2005 Hammurapi Group 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2 of the License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 019 * 020 * URL: http://http://www.hammurapi.biz 021 * e-Mail: support@hammurapi.biz 022 */ 023 package org.mesopotamia; 024 025 import java.io.File; 026 import java.util.ArrayList; 027 import java.util.Collection; 028 import java.util.Iterator; 029 030 /** 031 * For a given start directory iterates recursivly over directory structure to 032 * provide Sources from a filesystem. 033 * 034 * @author sourceeater 035 * @revision $Revision: 1.2 $ 036 */ 037 public class FileSourceIterator implements SourceIterator { 038 039 private Iterator<FileSource> sourceIterator; 040 041 public FileSourceIterator(Collection<File> rootFiles) { 042 Iterator<File> it=rootFiles.iterator(); 043 while (it.hasNext()) { 044 File start=it.next(); 045 int startPos = start.getAbsolutePath().length()+1; 046 Collection<FileSource> sources=new ArrayList<FileSource>(); 047 enumerateFiles(startPos, start, sources); 048 sourceIterator=sources.iterator(); 049 } 050 } 051 052 private static void enumerateFiles(int startPos, File file, Collection<FileSource> receiver) { 053 if (file.isFile()) { 054 String path=file.getAbsolutePath(); 055 receiver.add(new FileSource(file, path.length()<startPos ? file.getName() : path.substring(startPos))); 056 } else if (file.isDirectory()) { 057 File[] fa=file.listFiles(); 058 for (int i=0; i<fa.length; i++) { 059 enumerateFiles(startPos, fa[i], receiver); 060 } 061 } else { 062 // Special file - do nothing. 063 } 064 } 065 066 public Source nextSource() { 067 if (sourceIterator.hasNext()) { 068 return sourceIterator.next(); 069 } 070 071 return null; 072 } // nextSource 073 074 } // FileSourceIterator