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: 3284 $
|
---|
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 | static public String base64EncodeFromFile(String in_filename) {
|
---|
109 | byte [] data=null;
|
---|
110 | try {
|
---|
111 | data = readFile(in_filename);
|
---|
112 | } catch (Exception e) {
|
---|
113 | System.out.println("couldn't read the file");
|
---|
114 | }
|
---|
115 | String encodedString = Base64.encode(data);
|
---|
116 | return encodedString;
|
---|
117 |
|
---|
118 | }
|
---|
119 | static public boolean base64DecodeToFile(String data, String out_filename) {
|
---|
120 | try {
|
---|
121 | byte[] buffer=Base64.decode(data);
|
---|
122 | writeFile(buffer, out_filename);
|
---|
123 |
|
---|
124 | } catch (Exception e) {
|
---|
125 | System.err.println("file opening/closing errors"+e.getMessage());
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 | return true;
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static byte[] readFile(String filename) throws IOException {
|
---|
133 | File file = new File(filename);
|
---|
134 | BufferedInputStream bis = new BufferedInputStream(new
|
---|
135 | FileInputStream(file));
|
---|
136 | int bytes = (int) file.length();
|
---|
137 | byte[] buffer = new byte[bytes];
|
---|
138 | int readBytes = bis.read(buffer);
|
---|
139 | bis.close();
|
---|
140 | return buffer;
|
---|
141 | }
|
---|
142 | public static void writeFile(byte [] buffer, String filename) throws IOException {
|
---|
143 | File file = new File(filename);
|
---|
144 | BufferedOutputStream bos = new BufferedOutputStream(new
|
---|
145 | FileOutputStream(file));
|
---|
146 | bos.write(buffer);
|
---|
147 | bos.close();
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|