Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/FileOps.java @ 91:a398fc09ba71
New version 1.8b4.
DocuFile classes use new Collection classes and has-a instead of is-a
list and map relations.
author | robcast |
---|---|
date | Mon, 17 Mar 2003 15:59:12 +0100 |
parents | 997ba69afb81 |
children | f4a5cfe37469 |
comparison
equal
deleted
inserted
replaced
90:8058d3b3466a | 91:a398fc09ba71 |
---|---|
23 import java.io.*; | 23 import java.io.*; |
24 import java.util.*; | 24 import java.util.*; |
25 | 25 |
26 import digilib.*; | 26 import digilib.*; |
27 | 27 |
28 | |
29 public class FileOps { | 28 public class FileOps { |
30 | 29 |
31 private Utils util = null; | 30 private Utils util = null; |
32 public static String[] fileTypes = { | 31 public static String[] fileTypes = |
33 "jpg", "image/jpeg", | 32 { |
34 "jpeg", "image/jpeg", | 33 "jpg", |
35 "jp2", "image/jp2", | 34 "image/jpeg", |
36 "png", "image/png", | 35 "jpeg", |
37 "gif", "image/gif", | 36 "image/jpeg", |
38 "tif", "image/tiff", | 37 "jp2", |
39 "tiff", "image/tiff"}; | 38 "image/jp2", |
39 "png", | |
40 "image/png", | |
41 "gif", | |
42 "image/gif", | |
43 "tif", | |
44 "image/tiff", | |
45 "tiff", | |
46 "image/tiff" }; | |
40 | 47 |
41 public FileOps() { | 48 public static String[] fileExtensions = |
42 util = new Utils(); | 49 { "jpg", "jpeg", "jp2", "png", "gif", "tif", "tiff" }; |
43 } | |
44 | 50 |
45 public FileOps(Utils u) { | 51 public FileOps() { |
46 util = u; | 52 util = new Utils(); |
47 } | 53 } |
48 | 54 |
49 public void setUtils(Utils u) { | 55 public FileOps(Utils u) { |
50 util = u; | 56 util = u; |
51 } | 57 } |
52 | 58 |
59 public void setUtils(Utils u) { | |
60 util = u; | |
61 } | |
53 | 62 |
54 /** | 63 /** |
55 * get the mime type for a file format (by extension) | 64 * get the mime type for a file format (by extension) |
56 */ | 65 */ |
57 public static String mimeForFile(File f) { | 66 public static String mimeForFile(File f) { |
58 String fn = f.getName(); | 67 String fn = f.getName(); |
59 for (int i = 0; i < fileTypes.length; i += 2) { | 68 for (int i = 0; i < fileTypes.length; i += 2) { |
60 if (fn.toLowerCase().endsWith(fileTypes[i])) { | 69 if (fn.toLowerCase().endsWith(fileTypes[i])) { |
61 return fileTypes[i+1]; | 70 return fileTypes[i + 1]; |
62 } | 71 } |
63 } | 72 } |
64 return null; | 73 return null; |
65 } | 74 } |
66 | 75 |
67 /** | 76 public static Iterator getImageExtensionIterator() { |
68 * get a filehandle for a file or directory name | 77 return Arrays.asList(fileExtensions).iterator(); |
69 * returns File number n if fn is directory (starts with 1) | 78 } |
70 */ | |
71 public File getFile(String fn, int n) throws FileOpException { | |
72 util.dprintln(4, "getFile ("+fn+", "+n+")"); | |
73 | 79 |
74 File f = new File(fn); | 80 /** |
75 // if fn is a file name then return file | 81 * get a filehandle for a file or directory name |
76 if (f.isFile()) { | 82 * returns File number n if fn is directory (starts with 1) |
77 return f; | 83 */ |
78 } | 84 public File getFile(String fn, int n) throws FileOpException { |
79 // if fn is a directory name then open directory | 85 util.dprintln(4, "getFile (" + fn + ", " + n + ")"); |
80 if (f.isDirectory()) { | |
81 File[] fl = f.listFiles(new ImageFileFilter()); | |
82 Arrays.sort(fl); | |
83 if ((n > 0) && (n <= fl.length)) { | |
84 return fl[n - 1]; | |
85 } | |
86 } | |
87 throw new FileOpException("Unable to find file: "+fn); | |
88 } | |
89 | 86 |
90 /** | 87 File f = new File(fn); |
91 * get the number of files in a directory | 88 // if fn is a file name then return file |
92 * (almost the same as getFile) | 89 if (f.isFile()) { |
93 * returns 0 in case of problems | 90 return f; |
94 */ | 91 } |
95 public int getNumFiles(String fn) throws FileOpException { | 92 // if fn is a directory name then open directory |
96 util.dprintln(4, "getNumFiles ("+fn+")"); | 93 if (f.isDirectory()) { |
94 File[] fl = f.listFiles(new ImageFileFilter()); | |
95 Arrays.sort(fl); | |
96 if ((n > 0) && (n <= fl.length)) { | |
97 return fl[n - 1]; | |
98 } | |
99 } | |
100 throw new FileOpException("Unable to find file: " + fn); | |
101 } | |
97 | 102 |
98 File f = new File(fn); | 103 /** |
99 // if fn is a file name then return 1 | 104 * get the number of files in a directory |
100 if (f.isFile()) { | 105 * (almost the same as getFile) |
101 return 1; | 106 * returns 0 in case of problems |
102 } | 107 */ |
103 // if fn is a directory name then return the number of files | 108 public int getNumFiles(String fn) throws FileOpException { |
104 if (f.isDirectory()) { | 109 util.dprintln(4, "getNumFiles (" + fn + ")"); |
105 return f.listFiles(new ImageFileFilter()).length; | |
106 } | |
107 // then fn must be something strange... | |
108 return 0; | |
109 } | |
110 | 110 |
111 File f = new File(fn); | |
112 // if fn is a file name then return 1 | |
113 if (f.isFile()) { | |
114 return 1; | |
115 } | |
116 // if fn is a directory name then return the number of files | |
117 if (f.isDirectory()) { | |
118 return f.listFiles(new ImageFileFilter()).length; | |
119 } | |
120 // then fn must be something strange... | |
121 return 0; | |
122 } | |
111 | 123 |
112 /** | 124 /** |
113 * get a filehandle for a file or directory name out of a list | 125 * get a filehandle for a file or directory name out of a list |
114 * dirs is a list of base directories, fn is the appended file/dirname | 126 * dirs is a list of base directories, fn is the appended file/dirname |
115 * searches dirs until fn exists (backwards if fwd is false) | 127 * searches dirs until fn exists (backwards if fwd is false) |
116 * returns File number n if fn is directory (starts with 1) | 128 * returns File number n if fn is directory (starts with 1) |
117 */ | 129 */ |
118 public File getFileVariant(String[] dirs, String fn, int n, boolean fwd) throws FileOpException { | 130 public File getFileVariant(String[] dirs, String fn, int n, boolean fwd) |
119 util.dprintln(4, "getVariantFile ("+dirs+", "+fn+", "+n+")"); | 131 throws FileOpException { |
132 util.dprintln( | |
133 4, | |
134 "getVariantFile (" + dirs + ", " + fn + ", " + n + ")"); | |
120 | 135 |
121 File f = null; | 136 File f = null; |
122 int nvar = dirs.length; | 137 int nvar = dirs.length; |
123 | 138 |
124 for (int i = 0; i < nvar; i++) { | 139 for (int i = 0; i < nvar; i++) { |
125 try { | 140 try { |
126 f = getFile(dirs[(fwd) ? i : (nvar-i-1)]+fn, n); | 141 f = getFile(dirs[(fwd) ? i : (nvar - i - 1)] + fn, n); |
127 } catch (FileOpException e) { | 142 } catch (FileOpException e) { |
128 f = null; | 143 f = null; |
129 } | 144 } |
130 if (f != null) { | 145 if (f != null) { |
131 return f; | 146 return f; |
132 } | 147 } |
133 } | 148 } |
134 throw new FileOpException("Unable to find file: "+fn); | 149 throw new FileOpException("Unable to find file: " + fn); |
135 } | 150 } |
136 | 151 |
137 /** | 152 /** |
138 * get the number of files in a directory | 153 * get the number of files in a directory |
139 * (almost the same as getFileVariant) | 154 * (almost the same as getFileVariant) |
140 * returns 0 in case of problems | 155 * returns 0 in case of problems |
141 */ | 156 */ |
142 public int getNumFilesVariant(String[] dirs, String fn, boolean fwd) throws FileOpException { | 157 public int getNumFilesVariant(String[] dirs, String fn, boolean fwd) |
143 util.dprintln(4, "getNumFilesVariant ("+dirs+", "+fn+")"); | 158 throws FileOpException { |
159 util.dprintln(4, "getNumFilesVariant (" + dirs + ", " + fn + ")"); | |
144 | 160 |
145 int nf = 0; | 161 int nf = 0; |
146 int nvar = dirs.length; | 162 int nvar = dirs.length; |
147 | 163 |
148 for (int i = 0; i < nvar; i++) { | 164 for (int i = 0; i < nvar; i++) { |
149 try { | 165 try { |
150 nf = getNumFiles(dirs[(fwd) ? i : (nvar-i-1)]+fn); | 166 nf = getNumFiles(dirs[(fwd) ? i : (nvar - i - 1)] + fn); |
151 } catch (FileOpException e) { | 167 } catch (FileOpException e) { |
152 nf = 0; | 168 nf = 0; |
153 } | 169 } |
154 if (nf > 0) { | 170 if (nf > 0) { |
155 return nf; | 171 return nf; |
156 } | 172 } |
157 } | 173 } |
158 return 0; | 174 return 0; |
159 } | 175 } |
160 | 176 |
161 /** | 177 /** |
162 * FileFilter for image types (helper class for getFile) | 178 * FileFilter for image types (helper class for getFile) |
163 */ | 179 */ |
164 static class ImageFileFilter implements FileFilter { | 180 static class ImageFileFilter implements FileFilter { |
165 | 181 |
166 public boolean accept(File f) { | 182 public boolean accept(File f) { |
167 if (f.isFile()) { | 183 if (f.isFile()) { |
168 return (mimeForFile(f) != null); | 184 return (mimeForFile(f) != null); |
169 } else { | 185 } else { |
170 return false; | 186 return false; |
171 } | 187 } |
172 } | 188 } |
173 } | 189 } |
174 | 190 |
175 } | 191 } |