1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.rcp.util;
17
18 import java.io.File;
19 import java.io.FileFilter;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.eclipse.core.runtime.FileLocator;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.Plugin;
28 import org.eclipse.osgi.service.datalocation.Location;
29 import org.osgi.framework.Bundle;
30 import org.seasar.framework.exception.IORuntimeException;
31 import org.seasar.framework.util.AssertionUtil;
32 import org.seasar.framework.util.ResourceUtil;
33 import org.seasar.uruma.util.PathUtil;
34
35
36
37
38
39
40 public class RcpResourceUtil {
41 private static final String SLASH = "/";
42
43 private static final String PROTCOL_JAR = "jar";
44
45 private static final String PROTCOL_FILE = "file";
46
47
48
49
50
51
52
53
54
55
56
57 public static URL getNativePluginPath(final Plugin uiPlugin)
58 throws IOException {
59 URL pluginUrl = uiPlugin.getBundle().getEntry(SLASH);
60 return FileLocator.resolve(pluginUrl);
61 }
62
63
64
65
66
67
68
69
70 public static String getBunldePath(final Bundle bundle) {
71 URL entry = bundle.getEntry(SLASH);
72 String pluginDirPath = null;
73 try {
74 pluginDirPath = FileLocator.resolve(entry).getPath();
75 } catch (IOException e) {
76 throw new IORuntimeException(e);
77 }
78 return removeHeadChar(pluginDirPath);
79 }
80
81
82
83
84
85
86
87
88
89
90
91 public static URL getLocalResourceUrl(final String path) throws IOException {
92 URL url = ResourceUtil.getClassLoader().getResource(path);
93 if (url != null) {
94 return FileLocator.resolve(url);
95 } else {
96 return null;
97 }
98 }
99
100
101
102
103
104
105
106
107
108
109 public static URL getLocalResourceUrlNoException(final String path) {
110 URL url = ResourceUtil.getClassLoader().getResource(path);
111 URL fileLocatorUrl = null;
112 try {
113 fileLocatorUrl = FileLocator.resolve(url);
114 } catch (Exception ignore) {
115 }
116 return fileLocatorUrl;
117 }
118
119
120
121
122
123
124
125
126
127
128 public static String getBundleRelativePath(final Bundle bundle,
129 final String path) {
130 AssertionUtil.assertNotNull("bundle", bundle);
131 AssertionUtil.assertNotNull("path", path);
132 try {
133 URL url = getLocalResourceUrl(path);
134 if (url != null) {
135 URL bundlePath = FileLocator.resolve(bundle.getEntry(SLASH));
136 return PathUtil.getRelativePath(bundlePath.getPath(), url
137 .getPath());
138 } else {
139 return null;
140 }
141 } catch (IOException ignore) {
142 return null;
143 }
144
145 }
146
147
148
149
150
151
152
153
154
155
156 public static List<File> findFileResources(final File baseDir,
157 final FileFilter filter) {
158 List<File> results = new ArrayList<File>();
159
160 if (baseDir.isDirectory()) {
161 findFileResources(baseDir, filter, results);
162 }
163
164 return results;
165 }
166
167 private static void findFileResources(final File parentDir,
168 final FileFilter filter, final List<File> results) {
169 File[] files = parentDir.listFiles();
170 for (int i = 0; i < files.length; i++) {
171 File file = files[i];
172
173 if (file.isDirectory()) {
174 findFileResources(file, filter, results);
175 } else if (file.isFile() && filter.accept(file)) {
176 results.add(file);
177 }
178 }
179 }
180
181
182
183
184
185
186 public static String getInstallPath() {
187 Location installLocation = Platform.getInstallLocation();
188 String path = installLocation.getURL().getFile();
189 return removeHeadChar(path);
190 }
191
192 private static String removeHeadChar(final String path) {
193 AssertionUtil.assertNotEmpty("path", path);
194 return path.substring(1, path.length() - 1);
195 }
196 }