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 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import org.apache.soap.encoding.soapenc.Base64;
|
---|
23 | import java.io.BufferedInputStream;
|
---|
24 | import java.io.BufferedOutputStream;
|
---|
25 | import java.io.FileInputStream;
|
---|
26 | import java.io.FileOutputStream;
|
---|
27 | import java.io.InputStream;
|
---|
28 | import java.io.IOException;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * GSFile - utility class for Greenstone.
|
---|
32 | *
|
---|
33 | * all file paths are created here
|
---|
34 | *
|
---|
35 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
36 | * @version $Revision: 3440 $
|
---|
37 | * @see File
|
---|
38 | */
|
---|
39 |
|
---|
40 | public class GSFile {
|
---|
41 |
|
---|
42 | /** site config file path */
|
---|
43 | static public String siteConfigFile(String site_home) {
|
---|
44 | return site_home + File.separatorChar+"sitecfg.xml";
|
---|
45 |
|
---|
46 | }
|
---|
47 |
|
---|
48 | /** collection directory path */
|
---|
49 | static public String collectDir(String site_home) {
|
---|
50 | return site_home+File.separatorChar+"collect";
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | /** collection config file path*/
|
---|
55 | static public String collectionConfigFile(String site_home,
|
---|
56 | String collection_name) {
|
---|
57 | return site_home+File.separatorChar+"collect"+
|
---|
58 | File.separatorChar+collection_name+
|
---|
59 | File.separatorChar+"etc"+
|
---|
60 | File.separatorChar+"collectcfg.xml";
|
---|
61 |
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** collection build config file path*/
|
---|
65 | static public String collectionBuildConfigFile(String site_home,
|
---|
66 | String collection_name ) {
|
---|
67 | return site_home+File.separatorChar+"collect"+
|
---|
68 | File.separatorChar+collection_name+
|
---|
69 | File.separatorChar+"index"+
|
---|
70 | File.separatorChar+"buildcfg.xml";
|
---|
71 | }
|
---|
72 |
|
---|
73 | /** XML Transform directory path */
|
---|
74 | static public String xmlTransformDir(String interface_home) {
|
---|
75 | return interface_home+File.separatorChar+"transform";
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** collection base directory path */
|
---|
79 | static public String collectionBaseDir(String site_home,
|
---|
80 | String collection_name) {
|
---|
81 | return site_home+File.separatorChar+"collect"+
|
---|
82 | File.separatorChar+collection_name;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** text path (for doc retrieval) relative to collectionBaseDir */
|
---|
86 | static public String collectionTextPath(String collection_name) {
|
---|
87 | return "index"+File.separatorChar+"text"+File.separatorChar+
|
---|
88 | collection_name;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** index path (for querying) relative to collectionBaseDir */
|
---|
92 | static public String collectionIndexPath(String collection_name,
|
---|
93 | String index_name) {
|
---|
94 | return "index"+File.separatorChar+index_name+File.separatorChar+
|
---|
95 | collection_name;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** absolute path for an associated file */
|
---|
99 | static public String assocFileAbsolutePath(String site_home,
|
---|
100 | String collection_name,
|
---|
101 | String assoc_file_path,
|
---|
102 | String filename) {
|
---|
103 | return collectionBaseDir(site_home, collection_name)+
|
---|
104 | File.separatorChar+"index"+File.separatorChar+
|
---|
105 | "assoc"+File.separatorChar+assoc_file_path+
|
---|
106 | File.separatorChar+filename;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** returns the absolute path to a stylesheet
|
---|
110 | * stylesheets are looked for in the following order
|
---|
111 | * site-specific, interface-specific, default
|
---|
112 | * returns null if the file cannot be found */
|
---|
113 | static public String stylesheetFile(ConfigVars config, String filename) {
|
---|
114 | // try site one first
|
---|
115 | File stylesheet = new File(config.sites_home_+File.separatorChar+
|
---|
116 | config.site_name_+File.separatorChar+
|
---|
117 | "transform"+File.separatorChar+filename);
|
---|
118 | if (stylesheet.exists()) {
|
---|
119 | return stylesheet.getPath();
|
---|
120 | }
|
---|
121 | // try current interface
|
---|
122 | stylesheet = new File(config.interfaces_home_+File.separatorChar+
|
---|
123 | config.interface_name_+File.separatorChar+
|
---|
124 | "transform"+File.separatorChar+filename);
|
---|
125 | if (stylesheet.exists()) {
|
---|
126 | return stylesheet.getPath();
|
---|
127 | }
|
---|
128 | // try default interface
|
---|
129 | stylesheet = new File(config.interfaces_home_+File.separatorChar+
|
---|
130 | "default"+File.separatorChar+
|
---|
131 | "transform"+File.separatorChar+filename);
|
---|
132 | if (stylesheet.exists()) {
|
---|
133 | return stylesheet.getPath();
|
---|
134 | }
|
---|
135 |
|
---|
136 | // cant find it
|
---|
137 | return null;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /** language specific files directory */
|
---|
142 | public static String translateFile(ConfigVars config, String lang) {
|
---|
143 |
|
---|
144 | return config.interfaces_home_+File.separatorChar+
|
---|
145 | "translate"+File.separatorChar+lang+".xml";
|
---|
146 | }
|
---|
147 | /** base directory for phind data */
|
---|
148 | public static String phindBaseDir(String site_home, String coll_name,
|
---|
149 | String phind_index) {
|
---|
150 | return site_home + File.separatorChar +
|
---|
151 | "collect" + File.separatorChar +
|
---|
152 | coll_name + File.separatorChar +
|
---|
153 | "index" + File.separatorChar +
|
---|
154 | "phind"+phind_index;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /** the gdbm database file -
|
---|
158 | * note, need to change extension depending on OS */
|
---|
159 | static public String GDBMDatabaseFile(String site_home,
|
---|
160 | String collection_name) {
|
---|
161 |
|
---|
162 | return site_home + File.separatorChar +
|
---|
163 | "collect" + File.separatorChar +
|
---|
164 | collection_name + File.separatorChar +
|
---|
165 | "index" + File.separatorChar + "text" + File.separatorChar +
|
---|
166 | collection_name + ".ldb";
|
---|
167 |
|
---|
168 | }
|
---|
169 |
|
---|
170 | // some file utility methods
|
---|
171 |
|
---|
172 | /** read in a file and encode it using base64
|
---|
173 | * encoded data returned as a String */
|
---|
174 | static public String base64EncodeFromFile(String in_filename) {
|
---|
175 | byte [] data=null;
|
---|
176 | try {
|
---|
177 | data = readFile(in_filename);
|
---|
178 | } catch (Exception e) {
|
---|
179 | System.out.println("couldn't read the file");
|
---|
180 | }
|
---|
181 | String encodedString = Base64.encode(data);
|
---|
182 | return encodedString;
|
---|
183 |
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** decode some base64 data, and write it to the specified file */
|
---|
187 | static public boolean base64DecodeToFile(String data, String out_filename) {
|
---|
188 | try {
|
---|
189 | byte[] buffer=Base64.decode(data);
|
---|
190 | writeFile(buffer, out_filename);
|
---|
191 |
|
---|
192 | } catch (Exception e) {
|
---|
193 | System.err.println("file opening/closing errors"+e.getMessage());
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 | return true;
|
---|
197 |
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** read in a file to a byte array */
|
---|
201 | public static byte[] readFile(String filename) throws IOException {
|
---|
202 | File file = new File(filename);
|
---|
203 | BufferedInputStream bis = new BufferedInputStream(new
|
---|
204 | FileInputStream(file));
|
---|
205 | int bytes = (int) file.length();
|
---|
206 | byte[] buffer = new byte[bytes];
|
---|
207 | int readBytes = bis.read(buffer);
|
---|
208 | bis.close();
|
---|
209 | return buffer;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** write a byte array to a file */
|
---|
213 | public static void writeFile(byte [] buffer, String filename) throws IOException {
|
---|
214 | File file = new File(filename);
|
---|
215 | BufferedOutputStream bos = new BufferedOutputStream(new
|
---|
216 | FileOutputStream(file));
|
---|
217 | bos.write(buffer);
|
---|
218 | bos.close();
|
---|
219 | }
|
---|
220 |
|
---|
221 | }
|
---|