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 | * GSFiles - utility class for Greenstone.
|
---|
32 | *
|
---|
33 | * contains File creation methods.
|
---|
34 | *
|
---|
35 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
36 | * @version $Revision: 3362 $
|
---|
37 | * @see File
|
---|
38 | */
|
---|
39 | public class GSFile {
|
---|
40 |
|
---|
41 | /** creates a File for the site config file */
|
---|
42 | static public File siteConfigFile(String site_home) {
|
---|
43 | return new File(site_home + File.separatorChar+"sitecfg.xml");
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 | /** creates a File for the collection directory */
|
---|
48 | static public File collectDirFile(String site_home) {
|
---|
49 | return new File(site_home+File.separatorChar+"collect");
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /** creates a File for the collection config file */
|
---|
54 | static public File collectionConfigFile(String site_home,
|
---|
55 | String collection_name) {
|
---|
56 | return new File(site_home+File.separatorChar+"collect"+
|
---|
57 | File.separatorChar+collection_name+
|
---|
58 | File.separatorChar+"etc"+
|
---|
59 | File.separatorChar+"collectcfg.xml");
|
---|
60 |
|
---|
61 | }
|
---|
62 |
|
---|
63 | /** creates a File for the collection build config file */
|
---|
64 | static public File collectionBuildConfigFile(String site_home,
|
---|
65 | String collection_name ) {
|
---|
66 | return new File(site_home+File.separatorChar+"collect"+
|
---|
67 | File.separatorChar+collection_name+
|
---|
68 | File.separatorChar+"index"+
|
---|
69 | File.separatorChar+"buildcfg.xml");
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** returns the directory for XML Transforms */
|
---|
73 | static public String xmlTransformDir(String gsdl_home) {
|
---|
74 | return gsdl_home+File.separatorChar+"transform";
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** returns the base directory name for a collection */
|
---|
78 | static public String collectionBaseDir(String site_home,
|
---|
79 | String collection_name) {
|
---|
80 | return site_home+File.separatorChar+"collect"+
|
---|
81 | File.separatorChar+collection_name;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** returns the text path (for doc retrieval) relative to collectionBaseDir */
|
---|
85 | static public String collectionTextPath(String collection_name) {
|
---|
86 | return "index"+File.separatorChar+"text"+File.separatorChar+
|
---|
87 | collection_name;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /** returns the index path (for querying) relative to collectionBaseDir */
|
---|
91 | static public String collectionIndexPath(String collection_name,
|
---|
92 | String index_name) {
|
---|
93 | return "index"+File.separatorChar+index_name+File.separatorChar+
|
---|
94 | collection_name;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** returns an absolute path for an associated file */
|
---|
98 | static public String assocFileAbsolutePath(String site_home,
|
---|
99 | String collection_name,
|
---|
100 | String assoc_file_path,
|
---|
101 | String filename) {
|
---|
102 | return collectionBaseDir(site_home, collection_name)+
|
---|
103 | File.separatorChar+"index"+File.separatorChar+
|
---|
104 | "assoc"+File.separatorChar+assoc_file_path+
|
---|
105 | File.separatorChar+filename;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /** returns the absolute path to a stylesheet
|
---|
109 | * stylesheets are looked for in the following order
|
---|
110 | * site-specific, ui specific, default
|
---|
111 | * returns null if the file cannot be found*/
|
---|
112 | /* static public String stylesheetPath(String gsdl_home, String display_home,
|
---|
113 | String site_home, String filename) {
|
---|
114 |
|
---|
115 | // try site one first
|
---|
116 | File stylesheet = new File(site_home+File.separatorChar+"transform"+
|
---|
117 | File.separatorChar+filename);
|
---|
118 | if (stylesheet.exists()) {
|
---|
119 | return stylesheet.getPath();
|
---|
120 | }
|
---|
121 | // try custom display
|
---|
122 | stylesheet = new File(display_home+File.separatorChar+"transform"+
|
---|
123 | File.separatorChar+filename);
|
---|
124 | if (stylesheet.exists()) {
|
---|
125 | return stylesheet.getPath();
|
---|
126 | }
|
---|
127 | // return the default one - should we check that it exists here?
|
---|
128 | stylesheet = new File(gsdl_home+File.separatorChar+"transform"+
|
---|
129 | File.separatorChar+filename);
|
---|
130 | if (stylesheet.exists()) {
|
---|
131 | return stylesheet.getPath();
|
---|
132 | }
|
---|
133 |
|
---|
134 | // cant find it
|
---|
135 | return null;
|
---|
136 | }
|
---|
137 | */
|
---|
138 |
|
---|
139 |
|
---|
140 | static public String stylesheetPath(ConfigVars config, String filename) {
|
---|
141 | // try site one first
|
---|
142 | File stylesheet = new File(config.sites_home_+File.separatorChar+
|
---|
143 | config.site_name_+File.separatorChar+
|
---|
144 | "transform"+File.separatorChar+filename);
|
---|
145 | if (stylesheet.exists()) {
|
---|
146 | return stylesheet.getPath();
|
---|
147 | }
|
---|
148 | // try custom display
|
---|
149 | stylesheet = new File(config.interfaces_home_+File.separatorChar+
|
---|
150 | config.interface_name_+File.separatorChar+
|
---|
151 | "transform"+File.separatorChar+filename);
|
---|
152 | if (stylesheet.exists()) {
|
---|
153 | return stylesheet.getPath();
|
---|
154 | }
|
---|
155 | // return the default one - should we check that it exists here?
|
---|
156 | stylesheet = new File(config.interfaces_home_+File.separatorChar+
|
---|
157 | "default"+File.separatorChar+
|
---|
158 | "transform"+File.separatorChar+filename);
|
---|
159 | if (stylesheet.exists()) {
|
---|
160 | return stylesheet.getPath();
|
---|
161 | }
|
---|
162 |
|
---|
163 | // cant find it
|
---|
164 | return null;
|
---|
165 | }
|
---|
166 | static public String base64EncodeFromFile(String in_filename) {
|
---|
167 | byte [] data=null;
|
---|
168 | try {
|
---|
169 | data = readFile(in_filename);
|
---|
170 | } catch (Exception e) {
|
---|
171 | System.out.println("couldn't read the file");
|
---|
172 | }
|
---|
173 | String encodedString = Base64.encode(data);
|
---|
174 | return encodedString;
|
---|
175 |
|
---|
176 | }
|
---|
177 | static public boolean base64DecodeToFile(String data, String out_filename) {
|
---|
178 | try {
|
---|
179 | byte[] buffer=Base64.decode(data);
|
---|
180 | writeFile(buffer, out_filename);
|
---|
181 |
|
---|
182 | } catch (Exception e) {
|
---|
183 | System.err.println("file opening/closing errors"+e.getMessage());
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 | return true;
|
---|
187 |
|
---|
188 | }
|
---|
189 |
|
---|
190 | public static byte[] readFile(String filename) throws IOException {
|
---|
191 | File file = new File(filename);
|
---|
192 | BufferedInputStream bis = new BufferedInputStream(new
|
---|
193 | FileInputStream(file));
|
---|
194 | int bytes = (int) file.length();
|
---|
195 | byte[] buffer = new byte[bytes];
|
---|
196 | int readBytes = bis.read(buffer);
|
---|
197 | bis.close();
|
---|
198 | return buffer;
|
---|
199 | }
|
---|
200 | public static void writeFile(byte [] buffer, String filename) throws IOException {
|
---|
201 | File file = new File(filename);
|
---|
202 | BufferedOutputStream bos = new BufferedOutputStream(new
|
---|
203 | FileOutputStream(file));
|
---|
204 | bos.write(buffer);
|
---|
205 | bos.close();
|
---|
206 | }
|
---|
207 |
|
---|
208 | // this assumes that lang stuff is in its own directory -
|
---|
209 | //interfaces_home/translate
|
---|
210 | public static String getLangPath(ConfigVars config, String lang) {
|
---|
211 |
|
---|
212 | return config.interfaces_home_+File.separatorChar+
|
---|
213 | "translate"+File.separatorChar+lang+".xml";
|
---|
214 | }
|
---|
215 |
|
---|
216 | public static File getLangFile(ConfigVars config, String lang) {
|
---|
217 |
|
---|
218 | return new File(config.interfaces_home_+File.separatorChar+
|
---|
219 | "translate"+File.separatorChar+lang+".xml");
|
---|
220 | }
|
---|
221 | }
|
---|