1 11 package org.eclipse.ui.internal.intro.impl.model.util; 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.IPath; 19 import org.eclipse.core.runtime.Path; 20 import org.eclipse.core.runtime.Platform; 21 import org.osgi.framework.Bundle; 22 23 public class FindSupport { 26 private static String [] NL_JAR_VARIANTS = buildNLVariants(Platform.getNL()); 27 28 private static String [] buildNLVariants(String nl) { 29 ArrayList result = new ArrayList (); 30 IPath base = new Path("nl"); 32 IPath path = new Path(nl.replace('_', '/')); 33 while (path.segmentCount() > 0) { 34 result.add(base.append(path).toString()); 35 if (path.segmentCount() > 1) 37 result.add(base.append(path.toString().replace('/', '_')).toString()); 38 path = path.removeLastSegments(1); 39 } 40 41 return (String []) result.toArray(new String [result.size()]); 42 } 43 44 47 public static URL find(Bundle bundle, IPath path) { 48 return find(bundle, path, null); 49 } 50 51 62 public static URL [] findEntries(Bundle bundle, IPath path) { 63 return findEntries(bundle, path, null); 64 } 65 66 78 public static URL [] findEntries(Bundle bundle, IPath path, Map override) { 79 ArrayList results = new ArrayList (1); 80 find(bundle, path, override, results); 81 return (URL []) results.toArray(new URL [results.size()]); 82 } 83 84 87 public static URL find(Bundle b, IPath path, Map override) { 88 return find(b, path, override, null); 89 } 90 91 private static URL find(Bundle b, IPath path, Map override, ArrayList multiple) { 92 if (path == null) 93 return null; 94 95 URL result = null; 96 97 if (path.isEmpty() || path.isRoot()) { 99 result = findInPlugin(b, Path.EMPTY, multiple); 103 if (result == null || multiple != null) 104 result = findInFragments(b, Path.EMPTY, multiple); 105 return result; 106 } 107 108 String first = path.segment(0); 110 if (first.charAt(0) != '$') { 111 result = findInPlugin(b, path, multiple); 112 if (result == null || multiple != null) 113 result = findInFragments(b, path, multiple); 114 return result; 115 } 116 117 IPath rest = path.removeFirstSegments(1); 119 if (first.equalsIgnoreCase("$nl$")) return findNL(b, rest, override, multiple); 121 if (first.equalsIgnoreCase("$os$")) return findOS(b, rest, override, multiple); 123 if (first.equalsIgnoreCase("$ws$")) return findWS(b, rest, override, multiple); 125 if (first.equalsIgnoreCase("$files$")) return null; 127 128 return null; 129 } 130 131 private static URL findOS(Bundle b, IPath path, Map override, ArrayList multiple) { 132 String os = null; 133 if (override != null) 134 try { 135 os = (String ) override.get("$os$"); } catch (ClassCastException e) { 138 } 140 if (os == null) 141 os = Platform.getOS(); 143 if (os.length() == 0) 144 return null; 145 146 String osArch = null; 148 if (override != null) 149 try { 150 osArch = (String ) override.get("$arch$"); } catch (ClassCastException e) { 153 } 155 if (osArch == null) 156 osArch = Platform.getOSArch(); 158 if (osArch.length() == 0) 159 return null; 160 161 URL result = null; 162 IPath base = new Path("os").append(os).append(osArch); while (base.segmentCount() != 1) { 165 IPath filePath = base.append(path); 166 result = findInPlugin(b, filePath, multiple); 167 if (result != null && multiple == null) 168 return result; 169 result = findInFragments(b, filePath, multiple); 170 if (result != null && multiple == null) 171 return result; 172 base = base.removeLastSegments(1); 173 } 174 result = findInPlugin(b, path, multiple); 177 if (result != null && multiple == null) 178 return result; 179 return findInFragments(b, path, multiple); 180 } 181 182 private static URL findWS(Bundle b, IPath path, Map override, ArrayList multiple) { 183 String ws = null; 184 if (override != null) 185 try { 186 ws = (String ) override.get("$ws$"); } catch (ClassCastException e) { 189 } 191 if (ws == null) 192 ws = Platform.getWS(); 194 IPath filePath = new Path("ws").append(ws).append(path); URL result = findInPlugin(b, filePath, multiple); 198 if (result != null && multiple == null) 199 return result; 200 result = findInFragments(b, filePath, multiple); 201 if (result != null && multiple == null) 202 return result; 203 result = findInPlugin(b, path, multiple); 206 if (result != null && multiple == null) 207 return result; 208 return findInFragments(b, path, multiple); 209 } 210 211 private static URL findNL(Bundle b, IPath path, Map override, ArrayList multiple) { 212 String nl = null; 213 String [] nlVariants = null; 214 if (override != null) 215 try { 216 nl = (String ) override.get("$nl$"); } catch (ClassCastException e) { 219 } 221 nlVariants = nl == null ? NL_JAR_VARIANTS : buildNLVariants(nl); 222 if (nl != null && nl.length() == 0) 223 return null; 224 225 URL result = null; 226 for (int i = 0; i < nlVariants.length; i++) { 227 IPath filePath = new Path(nlVariants[i]).append(path); 228 result = findInPlugin(b, filePath, multiple); 229 if (result != null && multiple == null) 230 return result; 231 result = findInFragments(b, filePath, multiple); 232 if (result != null && multiple == null) 233 return result; 234 } 235 result = findInPlugin(b, path, multiple); 238 if (result != null && multiple == null) 239 return result; 240 return findInFragments(b, path, multiple); 241 } 242 243 private static URL findInPlugin(Bundle b, IPath filePath, ArrayList multiple) { 244 URL result = b.getEntry(filePath.toString()); 245 if (result != null && multiple != null) 246 multiple.add(result); 247 return result; 248 } 249 250 private static URL findInFragments(Bundle b, IPath filePath, ArrayList multiple) { 251 Bundle[] fragments = Platform.getFragments(b); 252 if (fragments == null) 253 return null; 254 255 multiple.ensureCapacity(fragments.length + 1); 256 URL fileURL = null; 257 int i = 0; 258 while (i < fragments.length && fileURL == null) { 259 fileURL = fragments[i].getEntry(filePath.toString()); 260 if (fileURL != null && multiple != null) { 261 multiple.add(fileURL); 262 fileURL = null; 263 } 264 i++; 265 } 266 return fileURL; 267 } 268 269 272 public static final InputStream openStream(Bundle bundle, IPath file, boolean localized) throws IOException { 273 URL url = null; 274 if (!localized) { 275 url = findInPlugin(bundle, file, null); 276 if (url == null) 277 url = findInFragments(bundle, file, null); 278 } else { 279 url = FindSupport.find(bundle, file); 280 } 281 if (url != null) 282 return url.openStream(); 283 throw new IOException ("Cannot find " + file.toString()); } 285 286 } 287 | Popular Tags |