1 11 package org.eclipse.update.core.model; 12 13 import java.lang.reflect.Array ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.net.URLClassLoader ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Locale ; 21 import java.util.Map ; 22 import java.util.MissingResourceException ; 23 import java.util.ResourceBundle ; 24 import java.util.Set ; 25 26 import org.eclipse.core.runtime.Assert; 27 import org.eclipse.core.runtime.IPath; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.core.runtime.PlatformObject; 30 import org.eclipse.update.core.Feature; 31 import org.eclipse.update.core.SiteManager; 32 import org.eclipse.update.internal.core.Messages; 33 import org.eclipse.update.internal.core.UpdateCore; 34 import org.eclipse.update.internal.core.UpdateManagerUtils; 35 36 48 public abstract class ModelObject extends PlatformObject { 49 50 private boolean readOnly = false; 51 52 private static final String KEY_PREFIX = "%"; private static final String KEY_DOUBLE_PREFIX = KEY_PREFIX + KEY_PREFIX; 54 55 private static Map bundles; 56 57 62 protected ModelObject() { 63 } 64 65 71 protected final void assertIsWriteable() { 72 Assert.isTrue(!isReadOnly(), Messages.ModelObject_ModelReadOnly); 73 } 74 75 82 public void markReadOnly() { 83 readOnly = true; 84 } 85 86 94 public boolean isReadOnly() { 95 return readOnly; 96 } 97 98 105 protected void markReferenceReadOnly(ModelObject o) { 106 if (o == null) 107 return; 108 o.markReadOnly(); 109 } 110 111 118 protected void markListReferenceReadOnly(ModelObject[] o) { 119 if (o == null) 120 return; 121 for (int i = 0; i < o.length; i++) { 122 o[i].markReadOnly(); 123 } 124 } 125 126 137 public void resolve(URL base, URL bundleURL) throws MalformedURLException { 138 return; 139 } 140 141 150 protected void resolveReference(ModelObject o, URL url, URL bundleURL) throws MalformedURLException { 151 if (o == null) 152 return; 153 o.resolve(url, bundleURL); 154 } 155 156 165 protected void resolveListReference(ModelObject[] o, URL url, URL bundleURL) throws MalformedURLException { 166 if (o == null) 167 return; 168 for (int i = 0; i < o.length; i++) { 169 o[i].resolve(url, bundleURL); 170 } 171 } 172 173 183 protected URL resolveURL(URL context, URL bundleURL, String urlString) throws MalformedURLException { 184 185 if (urlString == null || urlString.trim().equals("")) return null; 188 189 String resolvedUrlString = resolveNLString(bundleURL, urlString); 191 192 resolvedUrlString = resolvePlatfromConfiguration(resolvedUrlString); 193 194 if (context == null) 196 return new URL (resolvedUrlString); 197 198 return new URL (context, resolvedUrlString); 200 } 201 209 private String resolvePlatfromConfiguration(String resolvedUrlString) { 210 int osIndex = resolvedUrlString.indexOf("$os$"); if (osIndex != -1) 212 return getExtendedString(resolvedUrlString); 213 214 int wsIndex = resolvedUrlString.indexOf("$ws$"); if (wsIndex != -1) 216 return getExtendedString(resolvedUrlString); 217 218 int nlIndex = resolvedUrlString.indexOf("$nl$"); if (nlIndex != -1) 220 return getExtendedString(resolvedUrlString); 221 222 int archIndex = resolvedUrlString.indexOf("$arch$"); if (archIndex != -1) 224 return getExtendedString(resolvedUrlString); 225 226 return resolvedUrlString; 227 } 228 229 private String getExtendedString(String resolvedUrlString) { 230 IPath path = new Path(resolvedUrlString); 231 path = getExpandedPath(path); 232 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_WARNINGS) { 233 UpdateCore.warn("Resolved :" + resolvedUrlString + " as:" + path.toOSString()); } 235 236 return path.toOSString(); 237 } 238 239 private IPath getExpandedPath(IPath path) { 240 String first = path.segment(0); 241 if (first != null) { 242 IPath rest = getExpandedPath(path.removeFirstSegments(1)); 243 if (first.equals("$ws$")) { path = new Path(SiteManager.getWS()).append(rest); 245 } else if (first.equals("$os$")) { path = new Path(SiteManager.getOS()).append(rest); 247 } else if (first.equals("$nl$")) { path = new Path(SiteManager.getNL()).append(rest); 249 } else if (first.equals("$arch$")) { path = new Path(SiteManager.getOSArch()).append(rest); 251 } 252 } 253 return path; 254 } 255 256 288 protected String resolveNLString(URL bundleURL, String string) { 289 290 if (string == null) 291 return null; 292 293 String s = string.trim(); 294 295 if (s.equals("")) return string; 297 298 if (!s.startsWith(KEY_PREFIX)) 299 return string; 300 301 if (s.startsWith(KEY_DOUBLE_PREFIX)) 302 return s.substring(1); 303 304 int ix = s.indexOf(" "); String key = ix == -1 ? s : s.substring(0, ix); 306 String dflt = ix == -1 ? s : s.substring(ix + 1); 307 308 ResourceBundle b = getResourceBundle(bundleURL); 309 310 if (b == null) 311 return dflt; 312 313 try { 314 return b.getString(key.substring(1)); 315 } catch (MissingResourceException e) { 316 return dflt; 317 } 318 } 319 320 330 protected Object [] arrayTypeFor(List l) { 331 if (l == null || l.size() == 0) 332 return null; 333 return (Object []) Array.newInstance(l.get(0).getClass(), 0); 334 } 335 336 346 protected Object [] arrayTypeFor(Set s) { 347 if (s == null || s.size() == 0) 348 return null; 349 Iterator i = s.iterator(); 350 return (Object []) Array.newInstance(i.next().getClass(), 0); 351 } 352 353 362 protected ResourceBundle getResourceBundle(URL url) { 363 364 if (url == null) 365 return null; 366 367 if (bundles == null) { 368 bundles = new HashMap (); 369 } else { 370 ResourceBundle bundle = (ResourceBundle ) bundles.get(url.toExternalForm()); 371 if (bundle != null) 372 return bundle; 373 } 374 375 ResourceBundle bundle = null; 376 try { 377 url = UpdateManagerUtils.asDirectoryURL(url); 378 ClassLoader l = new URLClassLoader (new URL [] { url }, null); 379 bundle = ResourceBundle.getBundle(getPropertyName(), Locale.getDefault(), l); 380 bundles.put(url.toExternalForm(), bundle); 381 } catch (MissingResourceException e) { 382 UpdateCore.warn(e.getLocalizedMessage() + ":" + url.toExternalForm()); } catch (MalformedURLException e) { 384 UpdateCore.warn(e.getLocalizedMessage()); 385 } 386 return bundle; 387 } 388 389 393 protected String getPropertyName() { 394 return Feature.FEATURE_FILE; 395 } 396 397 } 398 | Popular Tags |