1 package org.catacomb.interlish.util;
2
3
4 import java.io.BufferedOutputStream;
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
12 import java.util.Enumeration;
13 import java.util.StringTokenizer;
14 import java.util.jar.JarEntry;
15 import java.util.jar.JarFile;
16 import java.util.jar.JarInputStream;
17
18 import org.catacomb.Root;
19 import org.catacomb.interlish.report.Logger;
20 import org.catacomb.interlish.report.PrintLogger;
21 import org.catacomb.report.E;
22
23
24 public class JUtil {
25
26 static Class rootClass;
27
28 static String fileSep;
29 static String rootPath;
30
31 static {
32 rootClass = (new Root()).getClass();
33 fileSep = "/";
34 rootPath = "org" + fileSep + "catacomb";
35 }
36
37
38
39 public static String getRelativeResource(Object obj, String path) {
40 return getRelativeResource(null, obj.getClass(), path);
41 }
42
43
44 public static String getRelativeResource(String s) {
45 return getRelativeResource(null, rootClass, s);
46 }
47
48 public static String getRelativeResource(Logger logger, Class cls, String path) {
49 String sret = null;
50 Logger myLogger = logger;
51 if (myLogger == null) {
52 myLogger = new PrintLogger();
53 }
54 try {
55 if (cls != null) {
56 InputStream fis = cls.getResourceAsStream(path);
57 sret = readInputStream(myLogger, fis);
58 } else {
59 InputStream fis = ClassLoader.getSystemResourceAsStream(path);
60 sret = readInputStream(myLogger, fis);
61 }
62
63 } catch (Exception ex) {
64 myLogger.log("ResourceAccess - cant get " + path + " " + ex);
65 ex.printStackTrace();
66 }
67 return sret;
68 }
69
70
71
72 public static String getXMLResource(String path) {
73 String sp = null;
74 if (path.endsWith(".xml") || path.indexOf(".") < 0) {
75 E.warning("getXMLReousrce should have a dot path, not " + path);
76 sp = path;
77 } else {
78
79
80 sp = path.replaceAll("\\.", fileSep) + ".xml";
81 }
82 return getResource(sp);
83 }
84
85
86 public static String getFileResource(String path, String fnm) {
87 String sp = path.replaceAll("\\.", fileSep) + fileSep + fnm;
88 return getResource(sp);
89 }
90
91
92 public static String getResource(String pathin) {
93 String path = pathin;
94 String sret = null;
95
96 Logger logger = new PrintLogger();
97
98 try {
99 if (path.startsWith(rootPath)) {
100 path = path.substring(rootPath.length()+1, path.length());
101
102
103 InputStream fis = rootClass.getResourceAsStream(path);
104 sret = readInputStream(logger, fis);
105
106 } else {
107
108 InputStream fis = ClassLoader.getSystemResourceAsStream(path);
109
110 sret = readInputStream(logger, fis);
111 }
112
113 } catch (Exception ex) {
114 E.error("ResourceAccess - cant get " + path + " " + ex);
115 ex.printStackTrace();
116 }
117 return sret;
118 }
119
120
121 private static String readInputStream(Logger logger, InputStream fis)
122 throws NullPointerException, IOException {
123 String sret = null;
124
125 InputStreamReader insr = new InputStreamReader(fis);
126 BufferedReader fr = new BufferedReader(insr);
127
128 StringBuffer sb = new StringBuffer();
129 while (fr.ready()) {
130 sb.append(fr.readLine());
131 sb.append("\n");
132 }
133 fr.close();
134 sret = sb.toString();
135
136 return sret;
137 }
138
139
140
141 public static void copyBinaryResource(Logger loggerin, String respathin, File dest) {
142 Logger logger = loggerin;
143 String respath = respathin;
144 if (dest.exists()) {
145
146 return;
147 }
148
149 if (logger == null) {
150 logger = new PrintLogger();
151 }
152
153
154
155
156
157 try {
158 if (respath.startsWith(rootPath)) {
159 respath = respath.substring(rootPath.length()+1,respath.length());
160 }
161 InputStream in = rootClass.getResourceAsStream(respath);
162
163 OutputStream out = new FileOutputStream(dest);
164 byte[] buf = new byte[1024];
165 int len;
166 while ((len = in.read(buf)) > 0) {
167 out.write(buf, 0, len);
168 }
169 in.close();
170 out.close();
171 } catch (Exception ex) {
172 E.warning("ResourceAccess - cant get " + respath + " " + ex);
173 ex.printStackTrace();
174 }
175 }
176
177
178
179
180
181
182 public static void extractResources(Logger logger, String pathin, File dest) {
183 String path = pathin;
184 path = path.replaceAll("\\.", fileSep);
185 String sl = getFileResource(path, "_files.txt");
186 StringTokenizer st = new StringTokenizer(sl, " \n\r\t");
187 while (st.hasMoreTokens()) {
188 String tok = st.nextToken().trim();
189 if (tok.length() > 0) {
190
191 String respath = path + "/" + tok;
192 File destfile = new File(dest, tok);
193 copyBinaryResource(logger, respath, destfile);
194 }
195 }
196
197
198 String sld = getFileResource(path, "_directories.txt");
199 StringTokenizer std = new StringTokenizer(sld, " \n\r\t");
200 while (std.hasMoreTokens()) {
201 String tok = std.nextToken().trim();
202 if (tok.length() > 0) {
203 File fsub = new File(dest, tok);
204 fsub.mkdir();
205 extractResources(logger, path + fileSep + tok, fsub);
206 }
207 }
208 }
209
210
211
212 public static Object newInstance(String sin) {
213 String s = sin;
214 Object ret = null;
215
216 if (s.startsWith("org.")) {
217
218 } else {
219 s = "org.catacomb." + s;
220 }
221
222 try {
223 Class<?> c = Class.forName(s);
224 ret = c.newInstance();
225 } catch (Exception ex) {
226 E.error("cant instantiate " + s + " " + ex);
227 ex.printStackTrace();
228 }
229 return ret;
230 }
231
232
233
234 public static String shortClassName(Object ov) {
235 String cnm = ov.getClass().getName();
236 cnm = cnm.substring(cnm.lastIndexOf(".") + 1, cnm.length());
237 return cnm;
238 }
239
240
241 public static void extractMissingResources(String path, File dir) {
242 extractResources(new PrintLogger(), path, dir);
243 }
244
245
246
247
248
249 public static void unpackJar(File fjar, File fout) {
250 try {
251 JarFile jf = new JarFile(fjar);
252 Enumeration en = jf.entries();
253
254 while (en.hasMoreElements()) {
255 JarEntry je = (JarEntry) en.nextElement();
256 java.io.File f = new File(fout, je.getName());
257 if (je.isDirectory()) {
258 f.mkdirs();
259 continue;
260
261 } else {
262
263
264 if (f.getPath().indexOf("META-INF") >= 0) {
265
266 } else {
267 f.getParentFile().mkdirs();
268 java.io.InputStream is = jf.getInputStream(je);
269 java.io.FileOutputStream fos = new FileOutputStream(f);
270
271
272 while (is.available() > 0) {
273 fos.write(is.read());
274 }
275 fos.close();
276 is.close();
277 }
278 }
279 }
280
281
282
283
284
285 } catch (Exception ex) {
286 E.error("cant unpack " + fjar + " : " + ex);
287 }
288 }
289
290
291
292
293
294 public static void extractJarResources(Object base, String jarName, File destDirectory) {
295 try {
296 InputStream ins = base.getClass().getResourceAsStream(jarName);
297 JarInputStream jins = new JarInputStream(ins);
298 while (jins.available() > 0) {
299 JarEntry je = jins.getNextJarEntry();
300 if (je != null) {
301
302 File f = new File(destDirectory, je.getName());
303 if (je.isDirectory()) {
304 f.mkdirs();
305
306 } else if (f.getPath().indexOf("META-INF") >= 0) {
307
308
309 } else {
310 f.getParentFile().mkdirs();
311
312
313 int nb = (int)(je.getSize());
314
315 byte[] ba = new byte[nb];
316 int nread = 0;
317 while (nread < nb) {
318 nread += jins.read(ba, nread, nb-nread);
319 }
320 OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
321 os.write(ba);
322 os.flush();
323 }
324 }
325 }
326
327
328 } catch (Exception ex) {
329 E.error("cant extract resources - " + ex);
330 ex.printStackTrace();
331 }
332 }
333
334
335
336 }