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

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

new function to find a sysstem wide config format xslt

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