annotate servlet/src/digilib/io/FileOps.java @ 339:6d2032b6121d gen2_1

new directory and cache work
author robcast
date Wed, 17 Nov 2004 18:17:34 +0100
parents 0ff3ede32060
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
1 /*
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
2 * FileOps -- Utility class for file operations
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
3 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
4 * Digital Image Library servlet components
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
5 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
6 * Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
7 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
8 * This program is free software; you can redistribute it and/or modify it
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
9 * under the terms of the GNU General Public License as published by the Free
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
10 * Software Foundation; either version 2 of the License, or (at your option)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
11 * any later version.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
12 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
13 * Please read license.txt for the full details. A copy of the GPL may be found
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
14 * at http://www.gnu.org/copyleft/lgpl.html
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
15 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
16 * You should have received a copy of the GNU General Public License along with
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
18 * Place, Suite 330, Boston, MA 02111-1307 USA
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
19 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
20 */
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
21
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
22 package digilib.io;
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
23
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
24 import java.io.File;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
25 import java.io.FileFilter;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
26 import java.util.ArrayList;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
27 import java.util.Arrays;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
28 import java.util.HashMap;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
29 import java.util.Iterator;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
30 import java.util.List;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
31 import java.util.Map;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
32 import java.util.StringTokenizer;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
33
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
34 import org.apache.log4j.Logger;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
35
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
36 public class FileOps {
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
37
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
38 private static Logger logger = Logger.getLogger(FileOps.class);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
39
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
40 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
41 * Array of file extensions and corresponding mime-types.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
42 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
43 private static String[][] ft = { { "jpg", "image/jpeg" },
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
44 { "jpeg", "image/jpeg" }, { "jp2", "image/jp2" },
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
45 { "png", "image/png" }, { "gif", "image/gif" },
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
46 { "tif", "image/tiff" }, { "tiff", "image/tiff" },
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
47 { "txt", "text/plain" }, { "html", "text/html" },
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
48 { "htm", "text/html" }, { "xml", "text/xml" },
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
49 { "svg", "image/svg+xml" } };
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
50
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
51 public static Map fileTypes;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
52
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
53 public static List imageExtensions;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
54
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
55 public static List textExtensions;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
56
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
57 public static List svgExtensions;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
58
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
59 public static final int CLASS_NONE = -1;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
60
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
61 public static final int CLASS_IMAGE = 0;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
62
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
63 public static final int CLASS_TEXT = 1;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
64
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
65 public static final int CLASS_SVG = 2;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
66
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
67 public static final int NUM_CLASSES = 3;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
68
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
69 public static int[] fileClasses = {};
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
70
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
71 public static int[] fcIndexes = {};
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
72
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
73 public static final Integer HINT_BASEDIRS = new Integer(1);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
74
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
75 public static final Integer HINT_FILEEXT = new Integer(2);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
76
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
77 public static final Integer HINT_DIRS = new Integer(3);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
78
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
79 public static File[] baseDirs = {};
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
80
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
81 public static DocuDirCache cache;
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
82
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
83 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
84 * static initializer for FileOps
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
85 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
86 static {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
87 fileTypes = new HashMap();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
88 imageExtensions = new ArrayList();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
89 textExtensions = new ArrayList();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
90 svgExtensions = new ArrayList();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
91 // iterate through file types in ft and fill the Map and Lists
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
92 for (int i = 0; i < ft.length; i++) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
93 String ext = ft[i][0];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
94 String mt = ft[i][1];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
95 fileTypes.put(ext, mt);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
96 if (classForMimetype(mt) == CLASS_IMAGE) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
97 imageExtensions.add(ext);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
98 } else if (classForMimetype(mt) == CLASS_TEXT) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
99 textExtensions.add(ext);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
100 } else if (classForMimetype(mt) == CLASS_SVG) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
101 svgExtensions.add(ext);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
102 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
103 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
104 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
105
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
106 /** sets the array of actually used file classes */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
107 public static void setFileClasses(int[] fc) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
108 fileClasses = fc;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
109 fcIndexes = new int[NUM_CLASSES];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
110 for (int i = 0; i < fc.length; i++) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
111 fcIndexes[fc[i]] = i;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
112 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
113 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
114
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
115 /** returns the array of actually used file classes */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
116 public static int[] getFileClasses() {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
117 return fileClasses;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
118 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
119
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
120 /** returns an element from the array of actally used file classes */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
121 public static int getFileClass(int idx) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
122 try {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
123 return fileClasses[idx];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
124 } catch (Exception e) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
125 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
126 return CLASS_NONE;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
127 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
128
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
129 /** Returns the index number for the given file class.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
130 * @param fc
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
131 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
132 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
133 public static int getFCindex(int fc) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
134 try {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
135 return fcIndexes[fc];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
136 } catch (Exception e) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
137 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
138 return -1;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
139 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
140
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
141 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
142 * returns the file class for a mime-type
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
143 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
144 * @param mt
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
145 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
146 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
147 public static int classForMimetype(String mt) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
148 if (mt == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
149 return CLASS_NONE;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
150 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
151 if (mt.startsWith("image/svg")) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
152 return CLASS_SVG;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
153 } else if (mt.startsWith("image")) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
154 return CLASS_IMAGE;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
155 } else if (mt.startsWith("text")) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
156 return CLASS_TEXT;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
157 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
158 return CLASS_NONE;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
159 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
160
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
161 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
162 * get the mime type for a file format (by extension)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
163 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
164 public static String mimeForFile(File f) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
165 return (String) fileTypes.get(extname(f.getName().toLowerCase()));
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
166 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
167
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
168 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
169 * get the file class for the filename (by extension)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
170 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
171 * @param fn
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
172 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
173 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
174 public static int classForFilename(String fn) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
175 String mt = (String) fileTypes.get(extname(fn).toLowerCase());
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
176 return classForMimetype(mt);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
177 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
178
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
179 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
180 * get the file class for the file (by extension)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
181 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
182 * @param fn
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
183 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
184 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
185 public static int classForFile(File f) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
186 return classForFilename(f.getName());
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
187 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
188
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
189 public static Iterator getImageExtensionIterator() {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
190 return imageExtensions.iterator();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
191 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
192
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
193 public static Iterator getTextExtensionIterator() {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
194 return textExtensions.iterator();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
195 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
196
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
197 public static Iterator getSVGExtensionIterator() {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
198 return svgExtensions.iterator();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
199 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
200
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
201 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
202 * convert a string with a list of pathnames into an array of strings using
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
203 * the system's path separator string
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
204 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
205 public static String[] pathToArray(String paths) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
206 // split list into directories
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
207 StringTokenizer dirs = new StringTokenizer(paths, File.pathSeparator);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
208 int n = dirs.countTokens();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
209 if (n < 1) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
210 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
211 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
212 // add directories into array
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
213 String[] pathArray = new String[n];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
214 for (int i = 0; i < n; i++) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
215 String s = dirs.nextToken();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
216 // make shure the dir name ends with a directory separator
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
217 if (s.endsWith(File.separator)) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
218 pathArray[i] = s;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
219 } else {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
220 pathArray[i] = s + File.separator;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
221 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
222 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
223 return pathArray;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
224 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
225
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
226 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
227 * Extract the base of a file name (sans extension).
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
228 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
229 * Returns the filename without the extension. The extension is the part
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
230 * behind the last dot in the filename. If the filename has no dot the full
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
231 * file name is returned.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
232 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
233 * @param fn
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
234 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
235 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
236 public static String basename(String fn) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
237 if (fn == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
238 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
239 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
240 int i = fn.lastIndexOf('.');
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
241 if (i > 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
242 return fn.substring(0, i);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
243 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
244 return fn;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
245 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
246
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
247 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
248 * Extract the base of a file name (sans extension).
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
249 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
250 * Returns the filename without the extension. The extension is the part
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
251 * behind the last dot in the filename. If the filename has no dot the full
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
252 * file name is returned.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
253 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
254 * @param f
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
255 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
256 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
257 public static String basename(File f) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
258 if (f == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
259 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
260 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
261 return basename(f.getName());
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
262 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
263
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
264 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
265 * Extract the extension of a file name.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
266 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
267 * Returns the extension of a file name. The extension is the part behind
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
268 * the last dot in the filename. If the filename has no dot the empty string
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
269 * is returned.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
270 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
271 * @param fn
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
272 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
273 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
274 public static String extname(String fn) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
275 if (fn == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
276 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
277 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
278 int i = fn.lastIndexOf('.');
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
279 if (i > 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
280 return fn.substring(i + 1);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
281 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
282 return "";
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
283 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
284
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
285 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
286 * Extract the parent directory of a (digilib) path name.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
287 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
288 * Returns the parent directory of a path name. The parent is the part
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
289 * before the last slash in the path name. If the path name has no slash the
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
290 * empty string is returned.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
291 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
292 * @param fn
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
293 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
294 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
295 public static String dlParent(String fn) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
296 if (fn == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
297 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
298 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
299 int i = fn.lastIndexOf('/');
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
300 if (i > 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
301 return fn.substring(0, i);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
302 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
303 return "";
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
304 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
305
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
306 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
307 * Extract the dir/file name of a (digilib) path name.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
308 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
309 * The file/dir name is the part after the last slash in the path name. If
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
310 * the path name has no slash the same string is returned.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
311 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
312 * @param path
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
313 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
314 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
315 public static String dlName(String path) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
316 if (path == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
317 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
318 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
319 int i = path.lastIndexOf('/');
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
320 if (i > 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
321 return path.substring(i+1);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
322 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
323 return path;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
324 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
325
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
326 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
327 * Normalize a path name.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
328 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
329 * Removes leading and trailing slashes. Returns null if there is other
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
330 * unwanted stuff in the path name.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
331 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
332 * @param pathname
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
333 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
334 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
335 public static String normalName(String pathname) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
336 if (pathname == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
337 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
338 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
339 // upper-dir references are unwanted
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
340 if (pathname.indexOf("../") >= 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
341 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
342 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
343 int a = 0;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
344 int e = pathname.length() - 1;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
345 if (e < 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
346 return pathname;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
347 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
348 // leading and trailing "/" are removed
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
349 while ((a <= e) && (pathname.charAt(a) == '/')) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
350 a++;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
351 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
352 while ((a < e) && (pathname.charAt(e) == '/')) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
353 e--;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
354 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
355 return pathname.substring(a, e + 1);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
356 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
357
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
358 public static StringTokenizer dlPathIterator(String path) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
359 return new StringTokenizer(path, "/");
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
360 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
361
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
362
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
363 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
364 * FileFilter for general files
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
365 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
366 static class ReadableFileFilter implements FileFilter {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
367
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
368 public boolean accept(File f) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
369 return f.canRead();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
370 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
371 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
372
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
373 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
374 * FileFilter for image types (helper class for getFile)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
375 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
376 static class ImageFileFilter implements FileFilter {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
377
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
378 public boolean accept(File f) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
379 return (classForFilename(f.getName()) == CLASS_IMAGE);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
380 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
381 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
382
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
383 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
384 * FileFilter for text types (helper class for getFile)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
385 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
386 static class TextFileFilter implements FileFilter {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
387
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
388 public boolean accept(File f) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
389 return (classForFilename(f.getName()) == CLASS_TEXT);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
390 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
391 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
392
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
393 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
394 * FileFilter for svg types (helper class for getFile).
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
395 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
396 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
397 static class SVGFileFilter implements FileFilter {
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
398
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
399 public boolean accept(File f) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
400 return (classForFilename(f.getName()) == CLASS_SVG);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
401 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
402 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
403
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
404 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
405 * Factory for FileFilters (image or text).
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
406 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
407 * @param fileClass
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
408 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
409 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
410 public static FileFilter filterForClass(int fileClass) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
411 if (fileClass == CLASS_IMAGE) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
412 return new ImageFileFilter();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
413 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
414 if (fileClass == CLASS_TEXT) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
415 return new TextFileFilter();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
416 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
417 if (fileClass == CLASS_SVG) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
418 return new SVGFileFilter();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
419 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
420 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
421 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
422
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
423 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
424 * Factory for DocuDirents based on file class.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
425 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
426 * Returns an ImageFileset, TextFile or SVGFile.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
427 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
428 * @param fileClass
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
429 * @param file
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
430 * @param parent
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
431 * @param hints
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
432 * optional additional parameters
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
433 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
434 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
435 public static DigiDirent fileForClass(int fileClass, File file,
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
436 DigiDirectory parent, Map hints) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
437 // what class of file do we have?
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
438 if (fileClass == CLASS_IMAGE) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
439 // image file
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
440 return new ImageFileset(file, parent, hints);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
441 } else if (fileClass == CLASS_TEXT) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
442 // text file
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
443 return new TextFile(file, parent);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
444 } else if (fileClass == CLASS_SVG) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
445 // text file
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
446 return new SVGFile(file, parent);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
447 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
448 // anything else is a generic dir or file
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
449 if (file.isDirectory()) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
450 return getCachedDirectory(file, null, parent);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
451 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
452 return new DigiDirent(file.getName(), parent);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
453 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
454
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
455 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
456 * Filters a list of Files through a FileFilter.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
457 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
458 * @param files
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
459 * @param filter
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
460 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
461 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
462 public static File[] listFiles(File[] files, FileFilter filter) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
463 if (files == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
464 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
465 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
466 File[] ff = new File[files.length];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
467 int ffi = 0;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
468 for (int i = 0; i < files.length; i++) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
469 if (filter.accept(files[i])) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
470 ff[ffi] = files[i];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
471 ffi++;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
472 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
473 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
474 File[] fff = new File[ffi];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
475 System.arraycopy(ff, 0, fff, 0, ffi);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
476 return fff;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
477 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
478
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
479 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
480 * Returns the closest matching file out of an array.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
481 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
482 * Compares the files sans extensions if no direct match is found. Returns
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
483 * null if no match is found.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
484 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
485 * @param fn
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
486 * @param files
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
487 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
488 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
489 public static File findFile(File fn, File[] files) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
490 // try the same filename as the original
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
491 int fileIdx = Arrays.binarySearch(files, fn);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
492 if (fileIdx >= 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
493 return files[fileIdx];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
494 } else {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
495 // try closest matches without extension
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
496 String fb = FileOps.basename(fn);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
497 fileIdx = -fileIdx - 1;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
498 if ((fileIdx < files.length)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
499 && (FileOps.basename(files[fileIdx]).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
500 // idx ok
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
501 return files[fileIdx];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
502 } else if ((fileIdx > 0)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
503 && (FileOps.basename(files[fileIdx - 1]).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
504 // idx-1 ok
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
505 return files[fileIdx - 1];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
506 } else if ((fileIdx + 1 < files.length)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
507 && (FileOps.basename(files[fileIdx + 1]).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
508 // idx+1 ok
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
509 return files[fileIdx + 1];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
510 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
511 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
512 // unknown
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
513 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
514 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
515
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
516 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
517 * Returns the closest matching file out of an array.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
518 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
519 * Compares the files sans extensions if no direct match is found. Returns
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
520 * null if no match is found.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
521 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
522 * @param fn
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
523 * @param files
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
524 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
525 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
526 public static String findFilename(String fn, String[] files) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
527 // try the same filename as the original
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
528 int fileIdx = Arrays.binarySearch(files, fn);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
529 if (fileIdx >= 0) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
530 return files[fileIdx];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
531 } else {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
532 // try closest matches without extension
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
533 String fb = FileOps.basename(fn);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
534 fileIdx = -fileIdx - 1;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
535 if ((fileIdx < files.length)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
536 && (FileOps.basename(files[fileIdx]).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
537 // idx ok
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
538 return files[fileIdx];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
539 } else if ((fileIdx > 0)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
540 && (FileOps.basename(files[fileIdx - 1]).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
541 // idx-1 ok
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
542 return files[fileIdx - 1];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
543 } else if ((fileIdx + 1 < files.length)
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
544 && (FileOps.basename(files[fileIdx + 1]).equals(fb))) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
545 // idx+1 ok
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
546 return files[fileIdx + 1];
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
547 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
548 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
549 // unknown
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
550 return null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
551 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
552 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
553 * Returns a File for a base directory and a digilib-path.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
554 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
555 * @param basedir
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
556 * @param dlpath
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
557 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
558 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
559 public static File getRealFile(File basedir, String dlpath) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
560 // does this work on all platforms??
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
561 return new File(basedir, dlpath);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
562 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
563
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
564 /** Returns a File for a digilib-path.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
565 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
566 * The file is assumed to be in the first base directory.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
567 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
568 * @param dlpath
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
569 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
570 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
571 public static File getRealFile(String dlpath) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
572 // does this work on all platforms??
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
573 return new File(baseDirs[0], dlpath);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
574 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
575
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
576 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
577 * Creates a new empty hints Map.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
578 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
579 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
580 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
581 public static Map newHints() {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
582 Map m = new HashMap();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
583 return m;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
584 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
585
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
586 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
587 * Creates a new hints Map with the given first element.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
588 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
589 * @param type
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
590 * @param value
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
591 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
592 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
593 public static Map newHints(Integer type, Object value) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
594 Map m = new HashMap();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
595 if (type != null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
596 m.put(type, value);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
597 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
598 return m;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
599 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
600
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
601 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
602 * @return Returns the baseDirs.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
603 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
604 public static File[] getBaseDirs() {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
605 return baseDirs;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
606 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
607
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
608 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
609 * @param baseDirs
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
610 * The baseDirs to set.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
611 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
612 public static void setBaseDirs(File[] baseDirs) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
613 FileOps.baseDirs = baseDirs;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
614 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
615
339
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
616
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
617 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
618 * Returns a DigiDirectory instance that is guaranteed to be unique in the
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
619 * cache.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
620 *
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
621 * @param dir
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
622 * @param parent
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
623 * @return
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
624 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
625 public static DigiDirectory getCachedDirectory(File dir, String dlpath, DigiDirectory parent) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
626 if (dir == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
627 dir = FileOps.getRealFile(dlpath);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
628 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
629 DigiDirectory dd = null;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
630 if (parent == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
631 // create a new parent by starting at the root
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
632 StringBuffer ps = new StringBuffer();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
633 DigiDirectory p = cache.getRootDir();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
634 // walk the path
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
635 for (StringTokenizer i = dlPathIterator(dlpath); i.hasMoreTokens();) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
636 p.check();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
637 String dn = i.nextToken();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
638 ps.append("/");
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
639 ps.append(dn);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
640 DigiDirectory d = cache.get(dn);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
641 if (d == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
642 dd = new DigiDirectory(FileOps.getRealFile(dn), ps.toString(), p);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
643 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
644 if (d.getParent() != p) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
645 logger.warn("digidirectory "+d.getDLPath()+" has wrong parent: "+p.getDLPath());
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
646 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
647 p = d;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
648 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
649 } else {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
650 if (dlpath == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
651 dlpath = parent.getDLPath() + "/" + dir.getName();
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
652 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
653 dd = cache.get(dlpath);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
654 if (dd == null) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
655 dd = new DigiDirectory(dir, dlpath, parent);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
656 } else {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
657 logger.debug("reusing directory:" + dlpath);
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
658 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
659 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
660 return dd;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
661 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
662
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
663 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
664 * @return Returns the cache.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
665 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
666 public static DocuDirCache getCache() {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
667 return cache;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
668 }
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
669 /**
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
670 * @param cache The cache to set.
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
671 */
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
672 public static void setCache(DocuDirCache cache) {
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
673 FileOps.cache = cache;
6d2032b6121d new directory and cache work
robcast
parents: 1
diff changeset
674 }
1
0ff3ede32060 Initial revision
robcast
parents:
diff changeset
675 }