1 package net.sf.jour.processor;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Enumeration;
6 import java.util.NoSuchElementException;
7
8 public class DirectoryInputSource implements InputSource {
9
10 private File dir;
11
12 private String baseName;
13
14 public DirectoryInputSource(File dir) throws IOException {
15 this.dir = dir;
16 baseName = dir.getCanonicalPath();
17 }
18
19 public Enumeration getEntries() {
20 return new DirectoryEnumeration(dir);
21 }
22
23 private class DirectoryEnumeration implements Enumeration {
24
25 File[] files;
26
27 int processing;
28
29 Enumeration child = null;
30
31 DirectoryEnumeration(File dir) {
32 files = dir.listFiles();
33 if (files == null) {
34 throw new Error(dir.getAbsolutePath() + " path does not denote a directory");
35 }
36 processing = 0;
37 }
38
39 public boolean hasMoreElements() {
40 return ((child != null) && (child.hasMoreElements())) || (processing < files.length);
41 }
42
43 public Object nextElement() {
44 if (child != null) {
45 try {
46 return child.nextElement();
47 } catch (NoSuchElementException e) {
48 child = null;
49 }
50 }
51 if (processing >= files.length) {
52 throw new NoSuchElementException();
53 }
54 File next = files[processing++];
55 if (next.isDirectory()) {
56 child = new DirectoryEnumeration(next);
57 }
58 return new FileEntry(next, DirectoryInputSource.this.baseName);
59 }
60
61 }
62
63 public void close() {
64
65 }
66
67 }