updated readme
[debian/openrocket] / src / net / sf / openrocket / file / DirectoryIterator.java
1 package net.sf.openrocket.file;
2
3 import java.io.File;
4 import java.io.FileFilter;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.util.Iterator;
9 import java.util.NoSuchElementException;
10
11 import net.sf.openrocket.logging.LogHelper;
12 import net.sf.openrocket.startup.Application;
13 import net.sf.openrocket.util.JarUtil;
14 import net.sf.openrocket.util.Pair;
15
16 public abstract class DirectoryIterator implements Iterator<Pair<String, InputStream>> {
17
18         private static final LogHelper logger = Application.getLogger();
19
20         private Pair<String, InputStream> next = null;
21
22         @Override
23         public boolean hasNext() {
24                 if (next != null)
25                         return true;
26                 
27                 next = findNext();
28                 return (next != null);
29         }
30         
31
32         @Override
33         public Pair<String, InputStream> next() {
34                 if (next == null) {
35                         next = findNext();
36                 }
37                 if (next == null) {
38                         throw new NoSuchElementException("No more files");
39                 }
40                 
41                 Pair<String, InputStream> n = next;
42                 next = null;
43                 return n;
44         }
45         
46
47         @Override
48         public void remove() {
49                 throw new UnsupportedOperationException("remove() not supported");
50         }
51         
52
53         
54         /**
55          * Closes the resources related to this iterator.  This method should be
56          * overridden if the iterator needs to close any resources of its own, but
57          * must call this method as well.
58          */
59         public void close() {
60                 if (next != null) {
61                         try {
62                                 next.getV().close();
63                         } catch (IOException e) {
64                                 logger.error("Error closing file " + next.getU());
65                         }
66                         next = null;
67                 }
68         }
69         
70         /**
71          * Return the next pair of file name and InputStream.
72          * 
73          * @return      a pair with the file name and input stream reading the file.
74          */
75         protected abstract Pair<String, InputStream> findNext();
76         
77         
78         
79         /**
80          * Return a DirectoryIterator for a directory that can be located either
81          * within the containing JAR file, in the classpath or in the current directory
82          * (searched in this order).  The first place that contains matching files
83          * will be iterated through.
84          * 
85          * @param directory             the directory to search for.
86          * @param filter                the filter for matching files in the directory.
87          * @return                              a DirectoryIterator for iterating through the files in the
88          *                                              directory, or <code>null</code> if no directory containing
89          *                                              matching files can be found.
90          */
91         public static DirectoryIterator findDirectory(String directory, FileFilter filter) {
92                 DirectoryIterator iterator = null;
93                 
94                 // Try to load from containing JAR file
95                 File jarFile = JarUtil.getCurrentJarFile();
96                 if (jarFile != null) {
97                         try {
98                                 iterator = new ZipDirectoryIterator(jarFile, directory, filter);
99                                 if (iterator.hasNext()) {
100                                         return iterator;
101                                 }
102                                 iterator.close();
103                         } catch (IOException e) {
104                                 logger.error("Error opening containing JAR file " + jarFile, e);
105                         }
106                 }
107                 
108
109                 // Try to find directory as a system resource
110                 URL url = ClassLoader.getSystemResource(directory);
111                 if (url != null) {
112                         try {
113                                 File dir = JarUtil.urlToFile(url);
114                                 iterator = new RegularDirectoryIterator(dir, filter);
115                                 if (iterator.hasNext()) {
116                                         return iterator;
117                                 }
118                                 iterator.close();
119                         } catch (Exception e1) {
120                                 logger.error("Error opening directory from URL " + url);
121                         }
122                 }
123                 
124                 
125                 // Try to open directory as such
126                 try {
127                         iterator = new RegularDirectoryIterator(new File(directory), filter);
128                         if (iterator.hasNext()) {
129                                 return iterator;
130                         }
131                         iterator.close();
132                 } catch (IOException e) {
133                         logger.error("Error opening directory " + directory);
134                 }
135                         
136                 return null;
137         }
138         
139 }