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

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

tidied up a bit

  • 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: 3941 $
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
51 /** collection directory path */
52 static public String collectDir(String site_home) {
53 return site_home+File.separatorChar+"collect";
54 }
55
56
57 /** collection config file path*/
58 static public String collectionConfigFile(String site_home,
59 String collection_name) {
60 return site_home+File.separatorChar+"collect"+
61 File.separatorChar+collection_name+
62 File.separatorChar+"etc"+
63 File.separatorChar+"collectionConfig.xml";
64
65 }
66
67 /** collection build config file path*/
68 static public String collectionBuildConfigFile(String site_home,
69 String collection_name ) {
70 return site_home+File.separatorChar+"collect"+
71 File.separatorChar+collection_name+
72 File.separatorChar+"index"+
73 File.separatorChar+"buildConfig.xml";
74 }
75 /** collection build config file path*/
76 static public String collectionBuildConfigFileBuilding(String site_home,
77 String collection_name ) {
78 return site_home+File.separatorChar+"collect"+
79 File.separatorChar+collection_name+
80 File.separatorChar+"building"+
81 File.separatorChar+"buildConfig.xml";
82 }
83
84 /** XML Transform directory path */
85 static public String xmlTransformDir(String interface_home) {
86 return interface_home+File.separatorChar+"transform";
87 }
88
89 /** collection base directory path */
90 static public String collectionBaseDir(String site_home,
91 String collection_name) {
92 return site_home+File.separatorChar+"collect"+
93 File.separatorChar+collection_name;
94 }
95 /** collection building directory path */
96 static public String collectionBuildDir(String site_home,
97 String collection_name) {
98 return collectionBaseDir(site_home, collection_name) +
99 File.separatorChar+"building";
100 }
101 /** collection building directory path */
102 static public String collectionIndexDir(String site_home,
103 String collection_name) {
104 return collectionBaseDir(site_home, collection_name) +
105 File.separatorChar+"index";
106 }
107
108 /** text path (for doc retrieval) relative to collectionBaseDir */
109 static public String collectionTextPath(String collection_name) {
110 return "index"+File.separatorChar+"text"+File.separatorChar+
111 collection_name;
112 }
113
114 /** index path (for querying) relative to collectionBaseDir */
115 static public String collectionIndexPath(String collection_name,
116 String index_name) {
117 return "index"+File.separatorChar+index_name+File.separatorChar+
118 collection_name;
119 }
120
121 /** absolute path for an associated file */
122 static public String assocFileAbsolutePath(String site_home,
123 String collection_name,
124 String assoc_file_path,
125 String filename) {
126 return collectionBaseDir(site_home, collection_name)+
127 File.separatorChar+"index"+File.separatorChar+
128 "assoc"+File.separatorChar+assoc_file_path+
129 File.separatorChar+filename;
130 }
131
132 static public String siteHome(String gsdl_home, String site_name) {
133 return gsdl_home + File.separatorChar + "sites" +
134 File.separatorChar +site_name;
135 }
136
137 static public String interfaceHome(String gsdl_home,
138 String interface_name) {
139 return gsdl_home + File.separatorChar + "interfaces" +
140 File.separatorChar + interface_name;
141 }
142
143
144 /** returns the absolute path to a stylesheet
145 * stylesheets are looked for in the following order
146 * site-specific, interface-specific, default
147 * returns null if the file cannot be found
148 *
149 * this is not so good because sites may be on a different computer */
150 static public String stylesheetFile(ConfigVars config, String filename) {
151 // try site one first
152 String site_home = siteHome(config.gsdl3_home_, config.site_name_);
153 File stylesheet = new File(site_home +File.separatorChar+
154 "transform"+File.separatorChar+filename);
155 if (stylesheet.exists()) {
156 return stylesheet.getPath();
157 }
158 // try current interface
159 String interface_home = interfaceHome(config.gsdl3_home_,
160 config.interface_name_);
161 stylesheet = new File(interface_home+File.separatorChar+
162 "transform"+File.separatorChar+filename);
163 if (stylesheet.exists()) {
164 return stylesheet.getPath();
165 }
166 // try default interface
167 interface_home = interfaceHome(config.gsdl3_home_, "default");
168 stylesheet = new File(interface_home+File.separatorChar+
169 "transform"+File.separatorChar+filename);
170 if (stylesheet.exists()) {
171 return stylesheet.getPath();
172 }
173
174 // cant find it
175 return null;
176 }
177
178 /** returns the path to the stylesheet used to transform config file
179 * format statements*/
180 public static String configFileFormatStylesheet() {
181 URL url = ClassLoader.getSystemResource("config_format.xsl");
182 if (url == null) {
183 return "";
184 }
185 return url.getFile();
186 }
187
188 /** language specific files directory */
189 public static String translateFile(ConfigVars config, String lang) {
190
191 return config.gsdl3_home_+File.separatorChar+
192 "interfaces"+File.separatorChar+
193 "translate"+File.separatorChar+lang+".xml";
194 }
195 /** base directory for phind data */
196 public static String phindBaseDir(String site_home, String coll_name,
197 String phind_index) {
198 return site_home + File.separatorChar +
199 "collect" + File.separatorChar +
200 coll_name + File.separatorChar +
201 "index" + File.separatorChar +
202 "phind"+phind_index;
203 }
204
205 /** the gdbm database file -
206 * note, need to change extension depending on OS */
207 static public String GDBMDatabaseFile(String site_home,
208 String collection_name) {
209
210 return site_home + File.separatorChar +
211 "collect" + File.separatorChar +
212 collection_name + File.separatorChar +
213 "index" + File.separatorChar + "text" + File.separatorChar +
214 collection_name + ".ldb";
215
216 }
217
218 // some file utility methods
219
220 /** read in a file and encode it using base64
221 * encoded data returned as a String */
222 static public String base64EncodeFromFile(String in_filename) {
223 byte [] data=null;
224 try {
225 data = readFile(in_filename);
226 } catch (Exception e) {
227 System.err.println("GSFile.base64EncodeFromFile: couldn't read the file");
228 }
229 String encodedString = Base64.encode(data);
230 return encodedString;
231
232 }
233
234 /** decode some base64 data, and write it to the specified file */
235 static public boolean base64DecodeToFile(String data, String out_filename) {
236 try {
237 byte[] buffer=Base64.decode(data);
238 writeFile(buffer, out_filename);
239
240 } catch (Exception e) {
241 System.err.println("GSFile.base64DecodeToFile: file opening/closing errors"+e.getMessage());
242 return false;
243 }
244 return true;
245
246 }
247
248 /** read in a file to a byte array */
249 public static byte[] readFile(String filename) throws IOException {
250 File file = new File(filename);
251 BufferedInputStream bis = new BufferedInputStream(new
252 FileInputStream(file));
253 int bytes = (int) file.length();
254 byte[] buffer = new byte[bytes];
255 int readBytes = bis.read(buffer);
256 bis.close();
257 return buffer;
258 }
259
260 /** write a byte array to a file */
261 public static void writeFile(byte [] buffer, String filename) throws IOException {
262 File file = new File(filename);
263 BufferedOutputStream bos = new BufferedOutputStream(new
264 FileOutputStream(file));
265 bos.write(buffer);
266 bos.close();
267 }
268
269 public static boolean deleteFile(File f) {
270
271 if (f.isDirectory()) {
272 File[] files = f.listFiles();
273 for (int i=0; files!=null && i<files.length; i++) {
274 deleteFile(files[i]);
275 }
276
277 }
278 // delete the file or directory
279 return f.delete();
280 }
281
282 /** Recursively moves the contents of source file to destination,
283 * maintaining paths.
284 * @param source A File representing the directory whose contents you
285 * wish to move.
286 * @param destination A File representing the directory you wish to move
287 * files to.
288 * @return true if successful
289 */
290 public static boolean moveDirectory(File source, File destination) {
291
292 // first try rename
293 if (source.renameTo(destination)) {
294 return true;
295 }
296
297 // john T. said that this sometimes doesn't work, so you have to copy files manually
298
299 // copied from gatherer Utility class
300 File input[] = source.listFiles();
301 for(int i = 0; i < input.length; i++) {
302 File output = new File(destination, input[i].getName());
303 if (input[i].isDirectory()) {
304 moveDirectory(input[i], output);
305 } else {
306 // Copy the file
307 try {
308 output.getParentFile().mkdirs();
309 FileInputStream in = new FileInputStream(input[i]);
310 FileOutputStream out = new FileOutputStream(output);
311 int value = 0;
312 while((value = in.read()) != -1) {
313 out.write(value);
314 }
315 in.close();
316 out.close();
317 // Delete file
318 input[i].delete();
319 } catch (Exception e) {
320 System.err.println("GSFile.moveDirectory: exception: "+e.getMessage());
321 return false;
322 }
323
324 }
325 }
326 return true;
327 }
328
329}
Note: See TracBrowser for help on using the repository browser.