| 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 | 0 | 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 | 0 | URL pluginUrl = uiPlugin.getBundle().getEntry(SLASH); |
| 60 | 0 | return FileLocator.resolve(pluginUrl); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
public static String getBunldePath(final Bundle bundle) { |
| 71 | 0 | URL entry = bundle.getEntry(SLASH); |
| 72 | 0 | String pluginDirPath = null; |
| 73 | |
try { |
| 74 | 0 | pluginDirPath = FileLocator.resolve(entry).getPath(); |
| 75 | 0 | } catch (IOException e) { |
| 76 | 0 | throw new IORuntimeException(e); |
| 77 | 0 | } |
| 78 | 0 | 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 | 0 | URL url = ResourceUtil.getClassLoader().getResource(path); |
| 93 | 0 | if (url != null) { |
| 94 | 0 | return FileLocator.resolve(url); |
| 95 | |
} else { |
| 96 | 0 | return null; |
| 97 | |
} |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public static URL getLocalResourceUrlNoException(final String path) { |
| 110 | 16 | URL url = ResourceUtil.getClassLoader().getResource(path); |
| 111 | 16 | URL fileLocatorUrl = null; |
| 112 | |
try { |
| 113 | 16 | fileLocatorUrl = FileLocator.resolve(url); |
| 114 | 16 | } catch (Exception ignore) { |
| 115 | 0 | } |
| 116 | 16 | 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 | 0 | AssertionUtil.assertNotNull("bundle", bundle); |
| 131 | 0 | AssertionUtil.assertNotNull("path", path); |
| 132 | |
try { |
| 133 | 0 | URL url = getLocalResourceUrl(path); |
| 134 | 0 | if (url != null) { |
| 135 | 0 | URL bundlePath = FileLocator.resolve(bundle.getEntry(SLASH)); |
| 136 | 0 | return PathUtil.getRelativePath(bundlePath.getPath(), url |
| 137 | |
.getPath()); |
| 138 | |
} else { |
| 139 | 0 | return null; |
| 140 | |
} |
| 141 | 0 | } catch (IOException ignore) { |
| 142 | 0 | 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 | 0 | List<File> results = new ArrayList<File>(); |
| 159 | |
|
| 160 | 0 | if (baseDir.isDirectory()) { |
| 161 | 0 | findFileResources(baseDir, filter, results); |
| 162 | |
} |
| 163 | |
|
| 164 | 0 | return results; |
| 165 | |
} |
| 166 | |
|
| 167 | |
private static void findFileResources(final File parentDir, |
| 168 | |
final FileFilter filter, final List<File> results) { |
| 169 | 0 | File[] files = parentDir.listFiles(); |
| 170 | 0 | for (int i = 0; i < files.length; i++) { |
| 171 | 0 | File file = files[i]; |
| 172 | |
|
| 173 | 0 | if (file.isDirectory()) { |
| 174 | 0 | findFileResources(file, filter, results); |
| 175 | 0 | } else if (file.isFile() && filter.accept(file)) { |
| 176 | 0 | results.add(file); |
| 177 | |
} |
| 178 | |
} |
| 179 | 0 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public static String getInstallPath() { |
| 187 | 0 | Location installLocation = Platform.getInstallLocation(); |
| 188 | 0 | String path = installLocation.getURL().getFile(); |
| 189 | 0 | return removeHeadChar(path); |
| 190 | |
} |
| 191 | |
|
| 192 | |
private static String removeHeadChar(final String path) { |
| 193 | 0 | AssertionUtil.assertNotEmpty("path", path); |
| 194 | 0 | return path.substring(1, path.length() - 1); |
| 195 | |
} |
| 196 | |
} |