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

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

now have interface config file which lives in the directory for each interface - have a method to discover its path

  • 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: 4245 $
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(ConfigVars config, String filename) {
158 // try site one first
159 String site_home = siteHome(config.gsdl3_home_, config.site_name_);
160 File stylesheet = new File(site_home +File.separatorChar+
161 "transform"+File.separatorChar+filename);
162 if (stylesheet.exists()) {
163 return stylesheet.getPath();
164 }
165 // try current interface
166 String interface_home = interfaceHome(config.gsdl3_home_,
167 config.interface_name_);
168 stylesheet = new File(interface_home+File.separatorChar+
169 "transform"+File.separatorChar+filename);
170 if (stylesheet.exists()) {
171 return stylesheet.getPath();
172 }
173 // try default interface
174 interface_home = interfaceHome(config.gsdl3_home_, "default");
175 stylesheet = new File(interface_home+File.separatorChar+
176 "transform"+File.separatorChar+filename);
177 if (stylesheet.exists()) {
178 return stylesheet.getPath();
179 }
180
181 // cant find it
182 return null;
183 }
184
185 /** returns the path to the stylesheet used to transform config file
186 * format statements*/
187 public static String configFileFormatStylesheet() {
188 URL url = ClassLoader.getSystemResource("config_format.xsl");
189 if (url == null) {
190 return "";
191 }
192 return url.getFile();
193 }
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.