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

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

sites and interfaces now inside web directory - modified paths

  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 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: 4050 $
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 gsdl3_home, String site_name) {
133 return gsdl3_home + File.separatorChar + "web" +
134 File.separatorChar + "sites" +
135 File.separatorChar +site_name;
136 }
137
138 static public String interfaceHome(String gsdl3_home,
139 String interface_name) {
140 return gsdl3_home + File.separatorChar + + "web" +
141 File.separatorChar + "interfaces" +
142 File.separatorChar + interface_name;
143 }
144
145
146 /** returns the absolute path to a stylesheet
147 * stylesheets are looked for in the following order
148 * site-specific, interface-specific, default
149 * returns null if the file cannot be found
150 *
151 * this is not so good because sites may be on a different computer */
152 static public String stylesheetFile(ConfigVars config, String filename) {
153 // try site one first
154 String site_home = siteHome(config.gsdl3_home_, config.site_name_);
155 File stylesheet = new File(site_home +File.separatorChar+
156 "transform"+File.separatorChar+filename);
157 if (stylesheet.exists()) {
158 return stylesheet.getPath();
159 }
160 // try current interface
161 String interface_home = interfaceHome(config.gsdl3_home_,
162 config.interface_name_);
163 stylesheet = new File(interface_home+File.separatorChar+
164 "transform"+File.separatorChar+filename);
165 if (stylesheet.exists()) {
166 return stylesheet.getPath();
167 }
168 // try default interface
169 interface_home = interfaceHome(config.gsdl3_home_, "default");
170 stylesheet = new File(interface_home+File.separatorChar+
171 "transform"+File.separatorChar+filename);
172 if (stylesheet.exists()) {
173 return stylesheet.getPath();
174 }
175
176 // cant find it
177 return null;
178 }
179
180 /** returns the path to the stylesheet used to transform config file
181 * format statements*/
182 public static String configFileFormatStylesheet() {
183 URL url = ClassLoader.getSystemResource("config_format.xsl");
184 if (url == null) {
185 return "";
186 }
187 return url.getFile();
188 }
189
190 /** base directory for phind data */
191 public static String phindBaseDir(String site_home, String coll_name,
192 String phind_index) {
193 return site_home + File.separatorChar +
194 "collect" + File.separatorChar +
195 coll_name + File.separatorChar +
196 "index" + File.separatorChar +
197 "phind"+phind_index;
198 }
199
200 /** the gdbm database file -
201 * note, need to change extension depending on OS */
202 static public String GDBMDatabaseFile(String site_home,
203 String collection_name) {
204
205 return site_home + File.separatorChar +
206 "collect" + File.separatorChar +
207 collection_name + File.separatorChar +
208 "index" + File.separatorChar + "text" + File.separatorChar +
209 collection_name + ".ldb";
210
211 }
212
213 // some file utility methods
214
215 /** read in a file and encode it using base64
216 * encoded data returned as a String */
217 static public String base64EncodeFromFile(String in_filename) {
218 byte [] data=null;
219 try {
220 data = readFile(in_filename);
221 } catch (Exception e) {
222 System.err.println("GSFile.base64EncodeFromFile: couldn't read the file");
223 }
224 String encodedString = Base64.encode(data);
225 return encodedString;
226
227 }
228
229 /** decode some base64 data, and write it to the specified file */
230 static public boolean base64DecodeToFile(String data, String out_filename) {
231 try {
232 byte[] buffer=Base64.decode(data);
233 writeFile(buffer, out_filename);
234
235 } catch (Exception e) {
236 System.err.println("GSFile.base64DecodeToFile: file opening/closing errors"+e.getMessage());
237 return false;
238 }
239 return true;
240
241 }
242
243 /** read in a file to a byte array */
244 public static byte[] readFile(String filename) throws IOException {
245 File file = new File(filename);
246 BufferedInputStream bis = new BufferedInputStream(new
247 FileInputStream(file));
248 int bytes = (int) file.length();
249 byte[] buffer = new byte[bytes];
250 int readBytes = bis.read(buffer);
251 bis.close();
252 return buffer;
253 }
254
255 /** write a byte array to a file */
256 public static void writeFile(byte [] buffer, String filename) throws IOException {
257 File file = new File(filename);
258 BufferedOutputStream bos = new BufferedOutputStream(new
259 FileOutputStream(file));
260 bos.write(buffer);
261 bos.close();
262 }
263
264 public static boolean deleteFile(File f) {
265
266 if (f.isDirectory()) {
267 File[] files = f.listFiles();
268 for (int i=0; files!=null && i<files.length; i++) {
269 deleteFile(files[i]);
270 }
271
272 }
273 // delete the file or directory
274 return f.delete();
275 }
276
277 /** Recursively moves the contents of source file to destination,
278 * maintaining paths.
279 * @param source A File representing the directory whose contents you
280 * wish to move.
281 * @param destination A File representing the directory you wish to move
282 * files to.
283 * @return true if successful
284 */
285 public static boolean moveDirectory(File source, File destination) {
286
287 // first try rename
288 if (source.renameTo(destination)) {
289 return true;
290 }
291
292 // john T. said that this sometimes doesn't work, so you have to copy files manually
293
294 // copied from gatherer Utility class
295 File input[] = source.listFiles();
296 for(int i = 0; i < input.length; i++) {
297 File output = new File(destination, input[i].getName());
298 if (input[i].isDirectory()) {
299 moveDirectory(input[i], output);
300 } else {
301 // Copy the file
302 try {
303 output.getParentFile().mkdirs();
304 FileInputStream in = new FileInputStream(input[i]);
305 FileOutputStream out = new FileOutputStream(output);
306 int value = 0;
307 while((value = in.read()) != -1) {
308 out.write(value);
309 }
310 in.close();
311 out.close();
312 // Delete file
313 input[i].delete();
314 } catch (Exception e) {
315 System.err.println("GSFile.moveDirectory: exception: "+e.getMessage());
316 return false;
317 }
318
319 }
320 }
321 return true;
322 }
323
324}
Note: See TracBrowser for help on using the repository browser.