Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/FileOps.java @ 56:2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
Fix Error accessing files when only one document path was specified.
(ROC)
author | robcast |
---|---|
date | Tue, 27 Aug 2002 12:29:07 +0200 |
parents | 0ff3ede32060 |
children | 5ea1999befd4 |
rev | line source |
---|---|
1 | 1 /* FileOps -- Utility class for file operations |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de) | |
6 | |
7 This program is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 */ | |
20 | |
21 package digilib.io; | |
22 | |
23 import java.io.*; | |
24 import java.util.*; | |
25 | |
26 import digilib.*; | |
27 | |
28 | |
29 public class FileOps { | |
30 | |
31 private Utils util = null; | |
32 public static String[] fileTypes = { | |
33 "jpg", "image/jpeg", | |
34 "jpeg", "image/jpeg", | |
35 "png", "image/png", | |
36 "gif", "image/gif", | |
37 "tif", "image/tiff", | |
38 "tiff", "image/tiff"}; | |
39 | |
40 public FileOps() { | |
41 util = new Utils(); | |
42 } | |
43 | |
44 public FileOps(Utils u) { | |
45 util = u; | |
46 } | |
47 | |
48 public void setUtils(Utils u) { | |
49 util = u; | |
50 } | |
51 | |
52 | |
53 /** | |
54 * get the mime type for a file format (by extension) | |
55 */ | |
56 public static String mimeForFile(File f) { | |
57 String fn = f.getName(); | |
58 for (int i = 0; i < fileTypes.length; i += 2) { | |
59 if (fn.toLowerCase().endsWith(fileTypes[i])) { | |
60 return fileTypes[i+1]; | |
61 } | |
62 } | |
63 return null; | |
64 } | |
65 | |
66 /** | |
67 * get a filehandle for a file or directory name | |
68 * returns File number n if fn is directory (starts with 1) | |
69 */ | |
70 public File getFile(String fn, int n) throws FileOpException { | |
71 util.dprintln(4, "getFile ("+fn+", "+n+")"); | |
72 | |
73 File f = new File(fn); | |
74 // if fn is a file name then return file | |
75 if (f.isFile()) { | |
76 return f; | |
77 } | |
78 // if fn is a directory name then open directory | |
79 if (f.isDirectory()) { | |
80 File[] fl = f.listFiles(new ImgFileFilter()); | |
81 Arrays.sort(fl); | |
82 if ((n > 0) && (n <= fl.length)) { | |
83 return fl[n - 1]; | |
84 } | |
85 } | |
86 throw new FileOpException("Unable to find file: "+fn); | |
87 } | |
88 | |
89 /** | |
90 * get the number of files in a directory | |
91 * (almost the same as getFile) | |
92 * returns 0 in case of problems | |
93 */ | |
94 public int getNumFiles(String fn) throws FileOpException { | |
95 util.dprintln(4, "getNumFiles ("+fn+")"); | |
96 | |
97 File f = new File(fn); | |
98 // if fn is a file name then return 1 | |
99 if (f.isFile()) { | |
100 return 1; | |
101 } | |
102 // if fn is a directory name then return the number of files | |
103 if (f.isDirectory()) { | |
104 return f.listFiles(new ImgFileFilter()).length; | |
105 } | |
106 // then fn must be something strange... | |
107 return 0; | |
108 } | |
109 | |
110 | |
111 /** | |
112 * get a filehandle for a file or directory name out of a list | |
113 * dirs is a list of base directories, fn is the appended file/dirname | |
114 * searches dirs until fn exists (backwards if fwd is false) | |
115 * returns File number n if fn is directory (starts with 1) | |
116 */ | |
117 public File getFileVariant(String[] dirs, String fn, int n, boolean fwd) throws FileOpException { | |
118 util.dprintln(4, "getVariantFile ("+dirs+", "+fn+", "+n+")"); | |
119 | |
120 File f = null; | |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
121 int nvar = dirs.length; |
1 | 122 |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
123 for (int i = 0; i < nvar; i++) { |
1 | 124 try { |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
125 f = getFile(dirs[(fwd) ? i : (nvar-i-1)]+fn, n); |
1 | 126 } catch (FileOpException e) { |
127 f = null; | |
128 } | |
129 if (f != null) { | |
130 return f; | |
131 } | |
132 } | |
133 throw new FileOpException("Unable to find file: "+fn); | |
134 } | |
135 | |
136 /** | |
137 * get the number of files in a directory | |
138 * (almost the same as getFileVariant) | |
139 * returns 0 in case of problems | |
140 */ | |
141 public int getNumFilesVariant(String[] dirs, String fn, boolean fwd) throws FileOpException { | |
142 util.dprintln(4, "getNumFilesVariant ("+dirs+", "+fn+")"); | |
143 | |
144 int nf = 0; | |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
145 int nvar = dirs.length; |
1 | 146 |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
147 for (int i = 0; i < nvar; i++) { |
1 | 148 try { |
56
2ea78a56ecf8
Use system specific pathSeparator for documents paths (; on Win).
robcast
parents:
1
diff
changeset
|
149 nf = getNumFiles(dirs[(fwd) ? i : (nvar-i-1)]+fn); |
1 | 150 } catch (FileOpException e) { |
151 nf = 0; | |
152 } | |
153 if (nf > 0) { | |
154 return nf; | |
155 } | |
156 } | |
157 return 0; | |
158 } | |
159 | |
160 /** | |
161 * FileFilter for image types (helper class for getFile) | |
162 */ | |
163 private class ImgFileFilter implements FileFilter { | |
164 | |
165 public boolean accept(File f) { | |
166 if (f.isFile()) { | |
167 return (mimeForFile(f) != null); | |
168 } else { | |
169 return false; | |
170 } | |
171 } | |
172 } | |
173 | |
174 } |