1 11 package org.eclipse.core.internal.runtime; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Map ; 18 import org.eclipse.core.runtime.*; 19 import org.osgi.framework.Bundle; 20 21 public class FindSupport { 25 public static final String PROP_NL = "osgi.nl"; public static final String PROP_OS = "osgi.os"; public static final String PROP_WS = "osgi.ws"; public static final String PROP_ARCH = "osgi.arch"; 31 private static String [] NL_JAR_VARIANTS = buildNLVariants(Activator.getContext() == null ? System.getProperty(PROP_NL) : Activator.getContext().getProperty(PROP_NL)); 32 33 private static String [] buildNLVariants(String nl) { 34 ArrayList result = new ArrayList (); 35 IPath base = new Path("nl"); 37 IPath path = new Path(nl.replace('_', '/')); 38 while (path.segmentCount() > 0) { 39 result.add(base.append(path).toString()); 40 if (path.segmentCount() > 1) 42 result.add(base.append(path.toString().replace('/', '_')).toString()); 43 path = path.removeLastSegments(1); 44 } 45 46 return (String []) result.toArray(new String [result.size()]); 47 } 48 49 52 public static URL find(Bundle bundle, IPath path) { 53 return find(bundle, path, null); 54 } 55 56 59 public static URL find(Bundle b, IPath path, Map override) { 60 return find(b, path, override, null); 61 } 62 63 66 public static URL [] findEntries(Bundle bundle, IPath path) { 67 return findEntries(bundle, path, null); 68 } 69 70 73 public static URL [] findEntries(Bundle bundle, IPath path, Map override) { 74 ArrayList results = new ArrayList (1); 75 find(bundle, path, override, results); 76 return (URL []) results.toArray(new URL [results.size()]); 77 } 78 79 private static URL find(Bundle b, IPath path, Map override, ArrayList multiple) { 80 if (path == null) 81 return null; 82 83 URL result = null; 84 85 if (path.isEmpty() || path.isRoot()) { 87 result = findInPlugin(b, Path.EMPTY, multiple); 91 if (result == null || multiple != null) 92 result = findInFragments(b, Path.EMPTY, multiple); 93 return result; 94 } 95 96 String first = path.segment(0); 98 if (first.charAt(0) != '$') { 99 result = findInPlugin(b, path, multiple); 100 if (result == null || multiple != null) 101 result = findInFragments(b, path, multiple); 102 return result; 103 } 104 105 IPath rest = path.removeFirstSegments(1); 107 if (first.equalsIgnoreCase("$nl$")) return findNL(b, rest, override, multiple); 109 if (first.equalsIgnoreCase("$os$")) return findOS(b, rest, override, multiple); 111 if (first.equalsIgnoreCase("$ws$")) return findWS(b, rest, override, multiple); 113 if (first.equalsIgnoreCase("$files$")) return null; 115 116 return null; 117 } 118 119 private static URL findOS(Bundle b, IPath path, Map override, ArrayList multiple) { 120 String os = null; 121 if (override != null) 122 try { 123 os = (String ) override.get("$os$"); } catch (ClassCastException e) { 126 } 128 if (os == null) 129 os = Activator.getContext().getProperty(PROP_OS); 131 if (os.length() == 0) 132 return null; 133 134 String osArch = null; 136 if (override != null) 137 try { 138 osArch = (String ) override.get("$arch$"); } catch (ClassCastException e) { 141 } 143 if (osArch == null) 144 osArch = Activator.getContext().getProperty(PROP_ARCH); 146 if (osArch.length() == 0) 147 return null; 148 149 URL result = null; 150 IPath base = new Path("os").append(os).append(osArch); while (base.segmentCount() != 1) { 153 IPath filePath = base.append(path); 154 result = findInPlugin(b, filePath, multiple); 155 if (result != null && multiple == null) 156 return result; 157 result = findInFragments(b, filePath, multiple); 158 if (result != null && multiple == null) 159 return result; 160 base = base.removeLastSegments(1); 161 } 162 result = findInPlugin(b, path, multiple); 165 if (result != null && multiple == null) 166 return result; 167 return findInFragments(b, path, multiple); 168 } 169 170 private static URL findWS(Bundle b, IPath path, Map override, ArrayList multiple) { 171 String ws = null; 172 if (override != null) 173 try { 174 ws = (String ) override.get("$ws$"); } catch (ClassCastException e) { 177 } 179 if (ws == null) 180 ws = Activator.getContext().getProperty(PROP_WS); 182 IPath filePath = new Path("ws").append(ws).append(path); URL result = findInPlugin(b, filePath, multiple); 186 if (result != null && multiple == null) 187 return result; 188 result = findInFragments(b, filePath, multiple); 189 if (result != null && multiple == null) 190 return result; 191 result = findInPlugin(b, path, multiple); 194 if (result != null && multiple == null) 195 return result; 196 return findInFragments(b, path, multiple); 197 } 198 199 private static URL findNL(Bundle b, IPath path, Map override, ArrayList multiple) { 200 String nl = null; 201 String [] nlVariants = null; 202 if (override != null) 203 try { 204 nl = (String ) override.get("$nl$"); } catch (ClassCastException e) { 207 } 209 nlVariants = nl == null ? NL_JAR_VARIANTS : buildNLVariants(nl); 210 if (nl != null && nl.length() == 0) 211 return null; 212 213 URL result = null; 214 for (int i = 0; i < nlVariants.length; i++) { 215 IPath filePath = new Path(nlVariants[i]).append(path); 216 result = findInPlugin(b, filePath, multiple); 217 if (result != null && multiple == null) 218 return result; 219 result = findInFragments(b, filePath, multiple); 220 if (result != null && multiple == null) 221 return result; 222 } 223 result = findInPlugin(b, path, multiple); 226 if (result != null && multiple == null) 227 return result; 228 return findInFragments(b, path, multiple); 229 } 230 231 private static URL findInPlugin(Bundle b, IPath filePath, ArrayList multiple) { 232 URL result = b.getEntry(filePath.toString()); 233 if (result != null && multiple != null) 234 multiple.add(result); 235 return result; 236 } 237 238 private static URL findInFragments(Bundle b, IPath filePath, ArrayList multiple) { 239 Activator activator = Activator.getDefault(); 240 if (activator == null) 241 return null; 242 Bundle[] fragments = activator.getFragments(b); 243 if (fragments == null) 244 return null; 245 246 if (multiple != null) 247 multiple.ensureCapacity(fragments.length + 1); 248 249 for (int i = 0; i < fragments.length; i++) { 250 URL fileURL = fragments[i].getEntry(filePath.toString()); 251 if (fileURL != null) { 252 if (multiple == null) 253 return fileURL; 254 multiple.add(fileURL); 255 } 256 } 257 return null; 258 } 259 260 263 public static final InputStream openStream(Bundle bundle, IPath file, boolean substituteArgs) throws IOException { 264 URL url = null; 265 if (!substituteArgs) { 266 url = findInPlugin(bundle, file, null); 267 if (url == null) 268 url = findInFragments(bundle, file, null); 269 } else { 270 url = FindSupport.find(bundle, file); 271 } 272 if (url != null) 273 return url.openStream(); 274 throw new IOException ("Cannot find " + file.toString()); } 276 } 277 | Popular Tags |