source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSFile.java@ 4700

Last change on this file since 4700 was 4700, checked in by kjdon, 21 years ago

no longer use ConfigVars, so methods that used that no take all teh needed arguments individually

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1/*
2 * GSFile.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21import java.io.File;
22import org.apache.soap.encoding.soapenc.Base64;
23import java.io.BufferedInputStream;
24import java.io.BufferedOutputStream;
25import java.io.FileInputStream;
26import java.io.FileOutputStream;
27import java.io.InputStream;
28import java.io.IOException;
29import java.lang.ClassLoader; // to find files on the class path
30import java.net.URL;
31
32/**
33 * GSFile - utility class for Greenstone.
34 *
35 * all file paths are created here
36 * also has file utility methods
37 *
38 * @author <a href="mailto:[email protected]">Katherine Don</a>
39 * @version $Revision: 4700 $
40 * @see File
41 */
42
43public class GSFile {
44
45 /** site config file path */
46 static public String siteConfigFile(String site_home) {
47 return site_home + File.separatorChar+"siteConfig.xml";
48
49 }
50 /** interface config file path */
51 static public String interfaceConfigFile(String interface_home) {
52 return interface_home + File.separatorChar+"interfaceConfig.xml";
53
54 }
55
56 /** collection directory path */
57 static public String collectDir(String site_home) {
58 return site_home+File.separatorChar+"collect";
59 }
60
61
62 /** collection config file path*/
63 static public String collectionConfigFile(String site_home,
64 String collection_name) {
65 return site_home+File.separatorChar+"collect"+
66 File.separatorChar+collection_name+
67 File.separatorChar+"etc"+
68 File.separatorChar+"collectionConfig.xml";
69
70 }
71
72 /** collection build config file path*/
73 static public String collectionBuildConfigFile(String site_home,
74 String collection_name ) {
75 return site_home+File.separatorChar+"collect"+
76 File.separatorChar+collection_name+
77 File.separatorChar+"index"+
78 File.separatorChar+"buildConfig.xml";
79 }
80 /** collection build config file path*/
81 static public String collectionBuildConfigFileBuilding(String site_home,
82 String collection_name ) {
83 return site_home+File.separatorChar+"collect"+
84 File.separatorChar+collection_name+
85 File.separatorChar+"building"+
86 File.separatorChar+"buildConfig.xml";
87 }
88
89 /** XML Transform directory path */
90 static public String xmlTransformDir(String interface_home) {
91 return interface_home+File.separatorChar+"transform";
92 }
93
94 /** collection base directory path */
95 static public String collectionBaseDir(String site_home,
96 String collection_name) {
97 return site_home+File.separatorChar+"collect"+
98 File.separatorChar+collection_name;
99 }
100 /** collection building directory path */
101 static public String collectionBuildDir(String site_home,
102 String collection_name) {
103 return collectionBaseDir(site_home, collection_name) +
104 File.separatorChar+"building";
105 }
106 /** collection building directory path */
107 static public String collectionIndexDir(String site_home,
108 String collection_name) {
109 return collectionBaseDir(site_home, collection_name) +
110 File.separatorChar+"index";
111 }
112
113 /** text path (for doc retrieval) relative to collectionBaseDir */
114 static public String collectionTextPath(String collection_name) {
115 return "index"+File.separatorChar+"text"+File.separatorChar+
116 collection_name;
117 }
118
119 /** index path (for querying) relative to collectionBaseDir */
120 static public String collectionIndexPath(String collection_name,
121 String index_name) {
122 return "index"+File.separatorChar+index_name+File.separatorChar+
123 collection_name;
124 }
125
126 /** absolute path for an associated file */
127 static public String assocFileAbsolutePath(String site_home,
128 String collection_name,
129 String assoc_file_path,
130 String filename) {
131 return collectionBaseDir(site_home, collection_name)+
132 File.separatorChar+"index"+File.separatorChar+
133 "assoc"+File.separatorChar+assoc_file_path+
134 File.separatorChar+filename;
135 }
136
137 static public String siteHome(String gsdl3_home, String site_name) {
138 return gsdl3_home + File.separatorChar + "web" +
139 File.separatorChar + "sites" +
140 File.separatorChar +site_name;
141 }
142
143 static public String interfaceHome(String gsdl3_home,
144 String interface_name) {
145 return gsdl3_home + File.separatorChar + "web" +
146 File.separatorChar + "interfaces" +
147 File.separatorChar + interface_name;
148 }
149
150
151 /** returns the absolute path to a stylesheet
152 * stylesheets are looked for in the following order
153 * site-specific, interface-specific, default
154 * returns null if the file cannot be found
155 *
156 * this is not so good because sites may be on a different computer */
157 static public String stylesheetFile(String gsdl3_home,
158 String site_name,
159 String interface_name,
160 String filename) {
161 // try site one first
162 String site_home = siteHome(gsdl3_home, site_name);
163 File stylesheet = new File(site_home +File.separatorChar+
164 "transform"+File.separatorChar+filename);
165 if (stylesheet.exists()) {
166 return stylesheet.getPath();
167 }
168 // try current interface
169 String interface_home = interfaceHome(gsdl3_home,
170 interface_name);
171 stylesheet = new File(interface_home+File.separatorChar+
172 "transform"+File.separatorChar+filename);
173 if (stylesheet.exists()) {
174 return stylesheet.getPath();
175 }
176 // try default interface
177 interface_home = interfaceHome(gsdl3_home, "default");
178 stylesheet = new File(interface_home+File.separatorChar+
179 "transform"+File.separatorChar+filename);
180 if (stylesheet.exists()) {
181 return stylesheet.getPath();
182 }
183
184 // cant find it
185 return null;
186 }
187
188 /** returns the path to the stylesheet used to transform config file
189 * format statements*/
190 public static String configFileFormatStylesheet() {
191 URL url = ClassLoader.getSystemResource("config_format.xsl");
192 if (url == null) {
193 return "";
194 }
195 return url.getFile();
196 }
197
198 /** base directory for phind data */
199 public static String phindBaseDir(String site_home, String coll_name,
200 String phind_index) {
201 return site_home + File.separatorChar +
202 "collect" + File.separatorChar +
203 coll_name + File.separatorChar +
204 "index" + File.separatorChar +
205 "phind"+phind_index;
206 }
207
208 /** the gdbm database file -
209 * note, need to change extension depending on OS */
210 static public String GDBMDatabaseFile(String site_home,
211 String collection_name) {
212
213 return site_home + File.separatorChar +
214 "collect" + File.separatorChar +
215 collection_name + File.separatorChar +
216 "index" + File.separatorChar + "text" + File.separatorChar +
217 collection_name + ".ldb";
218
219 }
220
221 // some file utility methods
222
223 /** read in a file and encode it using base64
224 * encoded data returned as a String */
225 static public String base64EncodeFromFile(String in_filename) {
226 byte [] data=null;
227 try {
228 data = readFile(in_filename);
229 } catch (Exception e) {
230 System.err.println("GSFile.base64EncodeFromFile: couldn't read the file");
231 }
232 String encodedString = Base64.encode(data);
233 return encodedString;
234
235 }
236
237 /** decode some base64 data, and write it to the specified file */
238 static public boolean base64DecodeToFile(String data, String out_filename) {
239 try {
240 byte[] buffer=Base64.decode(data);
241 writeFile(buffer, out_filename);
242
243 } catch (Exception e) {
244 System.err.println("GSFile.base64DecodeToFile: file opening/closing errors"+e.getMessage());
245 return false;
246 }
247 return true;
248
249 }
250
251 /** read in a file to a byte array */
252 public static byte[] readFile(String filename) throws IOException {
253 File file = new File(filename);
254 BufferedInputStream bis = new BufferedInputStream(new
255 FileInputStream(file));
256 int bytes = (int) file.length();
257 byte[] buffer = new byte[bytes];
258 int readBytes = bis.read(buffer);
259 bis.close();
260 return buffer;
261 }
262
263 /** write a byte array to a file */
264 public static void writeFile(byte [] buffer, String filename) throws IOException {
265 File file = new File(filename);
266 BufferedOutputStream bos = new BufferedOutputStream(new
267 FileOutputStream(file));
268 bos.write(buffer);
269 bos.close();
270 }
271
272 public static boolean deleteFile(File f) {
273
274 if (f.isDirectory()) {
275 File[] files = f.listFiles();
276 for (int i=0; files!=null && i<files.length; i++) {
277 deleteFile(files[i]);
278 }
279
280 }
281 // delete the file or directory
282 return f.delete();
283 }
284
285 /** Recursively moves the contents of source file to destination,
286 * maintaining paths.
287 * @param source A File representing the directory whose contents you
288 * wish to move.
289 * @param destination A File representing the directory you wish to move
290 * files to.
291 * @return true if successful
292 */
293 public static boolean moveDirectory(File source, File destination) {
294
295 // first try rename
296 if (source.renameTo(destination)) {
297 return true;
298 }
299
300 // john T. said that this sometimes doesn't work, so you have to copy files manually
301
302 // copied from gatherer Utility class
303 File input[] = source.listFiles();
304 for(int i = 0; i < input.length; i++) {
305 File output = new File(destination, input[i].getName());
306 if (input[i].isDirectory()) {
307 moveDirectory(input[i], output);
308 } else {
309 // Copy the file
310 try {
311 output.getParentFile().mkdirs();
312 FileInputStream in = new FileInputStream(input[i]);
313 FileOutputStream out = new FileOutputStream(output);
314 int value = 0;
315 while((value = in.read()) != -1) {
316 out.write(value);
317 }
318 in.close();
319 out.close();
320 // Delete file
321 input[i].delete();
322 } catch (Exception e) {
323 System.err.println("GSFile.moveDirectory: exception: "+e.getMessage());
324 return false;
325 }
326
327 }
328 }
329 return true;
330 }
331
332}
Note: See TracBrowser for help on using the repository browser.