01: /*
02: * JavuX - Java Component Set
03: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
04: * e-mail: ksadlocha@programics.com
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20:
21: package com.javujavu.javux.app;
22:
23: import java.io.File;
24: import java.io.IOException;
25: import java.net.URL;
26: import com.javujavu.javux.util.code.CodeUrl;
27:
28: public class JarPath {
29: public static String getJarPath() {
30: JarPath cp = new JarPath();
31: Class c = cp.getClass();
32: return getJarPath(c);
33: }
34:
35: public static String getJarPath(Class c) {
36: String cn = "/" + c.getName().replace('.', '/') + ".class";
37: URL u = c.getResource(cn);
38: if (u == null)
39: return null;
40: String s = u.toString();
41: if (s == null || !s.endsWith(cn))
42: return null;
43: s = s.substring(0, s.length() - cn.length());
44:
45: int i;
46: if ((i = s.indexOf("file:/")) != -1) {
47: s = s.substring(i + 6);
48: if (s.endsWith("!")) {
49: i = s.lastIndexOf('/');
50: if (i == -1)
51: return null;
52: s = s.substring(0, i);
53: } else
54: return null;
55: } else if ((i = s.indexOf(":/ZIP")) != -1) {
56: s = s.substring(i + 5);
57: if (s.endsWith("+")) {
58: i = s.lastIndexOf('\\');
59: if (i == -1)
60: return null;
61: s = s.substring(0, i);
62: } else
63: return null;
64: } else
65: return null;
66:
67: s = CodeUrl.urlDecode(s);
68:
69: if (s.charAt(1) != ':' && s.charAt(0) != '/')
70: s = "/" + s;
71:
72: File f = new File(s);
73: if (!f.isDirectory())
74: return null;
75:
76: try {
77: s = f.getCanonicalPath();
78: } catch (IOException e) {
79: }
80:
81: if (s.endsWith(File.separator))
82: s = s.substring(0, s.length() - 1);
83:
84: return s;
85: }
86: }
|