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

Last change on this file since 3489 was 3489, checked in by kjdon, 22 years ago

config file names changed

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