KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > file > DirectoryArchiveBrowser


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.file;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * Comment
33  *
34  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
35  * @version $Revision: 1958 $
36  */

37 public class DirectoryArchiveBrowser implements Iterator JavaDoc
38 {
39    private Iterator JavaDoc files;
40
41    public DirectoryArchiveBrowser(File JavaDoc file, ArchiveBrowser.Filter filter)
42    {
43       ArrayList JavaDoc list = new ArrayList JavaDoc();
44       try
45       {
46          create(list, file, filter);
47       }
48       catch (Exception JavaDoc e)
49       {
50          throw new RuntimeException JavaDoc(e);
51       }
52       files = list.iterator();
53    }
54
55    public static void create(List JavaDoc list, File JavaDoc dir, ArchiveBrowser.Filter filter) throws Exception JavaDoc
56    {
57       File JavaDoc[] files = dir.listFiles();
58       for (int i = 0; i < files.length; i++)
59       {
60          if (files[i].isDirectory())
61          {
62             create(list, files[i], filter);
63          }
64          else
65          {
66             if (filter.accept(files[i].getAbsolutePath()))
67             {
68                list.add(files[i]);
69             }
70          }
71       }
72    }
73
74    public boolean hasNext()
75    {
76       return files.hasNext();
77    }
78
79    public Object JavaDoc next()
80    {
81       File JavaDoc fp = (File JavaDoc) files.next();
82       try
83       {
84          return new FileInputStream JavaDoc(fp);
85       }
86       catch (FileNotFoundException JavaDoc e)
87       {
88          throw new RuntimeException JavaDoc(e);
89       }
90    }
91
92    public void remove()
93    {
94       throw new RuntimeException JavaDoc("Illegal operation call");
95    }
96
97
98 }
99
Popular Tags