1 11 package org.eclipse.update.internal.configurator.branding; 12 13 import java.io.*; 14 import java.net.*; 15 import java.text.MessageFormat ; import java.util.ArrayList ; 17 import java.util.Enumeration ; 18 import java.util.Hashtable ; 19 import java.util.MissingResourceException ; 20 import java.util.Properties ; 21 import java.util.PropertyResourceBundle ; 22 import java.util.StringTokenizer ; 23 24 import org.eclipse.core.runtime.*; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.update.internal.configurator.Messages; 27 import org.eclipse.update.internal.configurator.Utils; 28 import org.osgi.framework.*; 29 30 37 public class IniFileReader { 38 private static final String PID = "org.eclipse.update.configurator"; private static final Status OK_STATUS = new Status(IStatus.OK, PID, 0, "", null); private static final String KEY_PREFIX = "%"; private static final String KEY_DOUBLE_PREFIX = "%%"; private static final String NLS_TAG = "$nl$"; 44 private String featureId; 45 private String pluginId; 46 private String iniFilename; 47 private String propertiesFilename; 48 private String mappingsFilename; 49 private Properties ini = null; 50 private PropertyResourceBundle properties = null; 51 private String [] mappings = null; 52 private Bundle bundle; 53 54 63 public IniFileReader(String featureId, String pluginId, String iniFilename, String propertiesFilename, String mappingsFilename) { 64 super(); 65 66 if (featureId == null || pluginId == null || iniFilename == null) { 67 throw new IllegalArgumentException (); 68 } 69 70 this.featureId = featureId; 71 this.pluginId = pluginId; 72 this.iniFilename = iniFilename; 73 this.propertiesFilename = propertiesFilename; 74 this.mappingsFilename = mappingsFilename; 75 } 76 77 84 public IStatus load() { 85 if (ini != null) 86 return OK_STATUS; 87 88 bundle = Utils.getBundle(pluginId); 90 if (bundle == null || bundle.getState() == Bundle.UNINSTALLED || bundle.getState() == Bundle.INSTALLED) { 91 bundle = null; String message = NLS.bind(Messages.IniFileReader_MissingDesc, (new String [] { featureId })); 93 return new Status(IStatus.ERROR, PID, 0, message, null); 94 } 95 96 URL iniURL = null; 98 IOException ioe = null; 99 iniURL = FileLocator.find(bundle, new Path(NLS_TAG).append(iniFilename), null); 100 if (iniURL == null) { 101 String message = NLS.bind(Messages.IniFileReader_OpenINIError, (new String [] { iniFilename })); 102 return new Status(IStatus.ERROR, PID, 0, message, ioe); 103 } 104 105 URL propertiesURL = null; 107 if (propertiesFilename != null & propertiesFilename.length() > 0) { 108 propertiesURL = FileLocator.find(bundle, new Path(NLS_TAG).append(propertiesFilename), null); 109 } 110 111 URL mappingsURL = null; 113 if (mappingsFilename != null && mappingsFilename.length() > 0) { 114 mappingsURL = FileLocator.find(bundle, new Path(NLS_TAG).append(mappingsFilename), null); 115 } 116 117 return load(iniURL, propertiesURL, mappingsURL); 119 } 120 121 127 public String getString(String key, boolean doNls, Hashtable runtimeMappings) { 128 if (ini == null) 129 return null; 130 String value = ini.getProperty(key); 131 if (value != null && doNls) 132 return getResourceString(value, runtimeMappings); 133 return value; 134 } 135 136 137 142 public URL getURL(String key) { 143 if (ini == null) 144 return null; 145 146 URL url = null; 147 String fileName = ini.getProperty(key); 148 if (fileName != null) { 149 if (bundle == null) 150 return null; 151 url = FileLocator.find(bundle, new Path(fileName), null); 152 } 153 return url; 154 } 155 156 165 public URL[] getURLs(String key) { 166 if (ini == null || bundle == null) 167 return null; 168 169 String value = ini.getProperty(key); 170 if (value == null) 171 return null; 172 173 StringTokenizer tokens = new StringTokenizer (value, ","); ArrayList array = new ArrayList (10); 175 while (tokens.hasMoreTokens()) { 176 String str = tokens.nextToken().trim(); 177 array.add(FileLocator.find(bundle, new Path(str), null)); 178 } 179 180 URL[] urls = new URL[array.size()]; 181 array.toArray(urls); 182 return urls; 183 } 184 185 190 public String getFeaturePluginLabel() { 191 if (bundle == null) 192 return null; 193 return (String )bundle.getHeaders().get(Constants.BUNDLE_NAME); 194 } 195 196 201 public String getProviderName() { 202 if (bundle == null) 203 return null; 204 return (String )bundle.getHeaders().get(Constants.BUNDLE_VENDOR); 205 } 206 207 239 public String getResourceString(String value, Hashtable runtimeMappings) { 240 241 if (value == null) 242 return null; 243 String s = value.trim(); 244 245 if (!s.startsWith(KEY_PREFIX)) 246 return s; 247 248 if (s.startsWith(KEY_DOUBLE_PREFIX)) 249 return s.substring(1); 250 251 int ix = s.indexOf(" "); String key = ix == -1 ? s : s.substring(0, ix); 253 String dflt = ix == -1 ? s : s.substring(ix + 1); 254 255 if (properties == null) 256 return dflt; 257 258 String result = null; 259 try { 260 result = properties.getString(key.substring(1)); 261 } catch (MissingResourceException e) { 262 return dflt; 263 } 264 if (runtimeMappings != null) { 265 for (Enumeration e = runtimeMappings.keys(); e.hasMoreElements();) { 266 String keyValue = (String ) e.nextElement(); 267 int i = result.indexOf(keyValue); 268 if (i != -1) { 269 String s1 = result.substring(0,i); 270 String s2 = (String ) runtimeMappings.get(keyValue); 271 String s3 = result.substring(i+keyValue.length()); 272 result = s1 + s2 + s3; 273 } 274 } 275 } 276 277 if (result.indexOf('{') != -1) { 278 result = MessageFormat.format(result, mappings); 281 } 282 283 return result; 284 } 285 286 289 private IStatus load(URL iniURL, URL propertiesURL, URL mappingsURL) { 290 291 InputStream is = null; 292 try { 293 is = iniURL.openStream(); 294 ini = new Properties (); 295 ini.load(is); 296 } catch (IOException e) { 297 ini = null; 298 String message = NLS.bind(Messages.IniFileReader_ReadIniError, (new String [] { iniURL.toExternalForm() })); 299 return new Status(IStatus.ERROR, PID, 0, message, e); 300 } finally { 301 try { 302 if (is != null) 303 is.close(); 304 } catch (IOException e) { 305 } 306 } 307 308 if (propertiesURL != null) { 309 is = null; 310 try { 311 is = propertiesURL.openStream(); 312 properties = new PropertyResourceBundle (is); 313 } catch (IOException e) { 314 properties = null; 315 String message = NLS.bind(Messages.IniFileReader_ReadPropError, (new String [] { propertiesURL.toExternalForm() })); 316 return new Status(IStatus.ERROR, PID, 0, message, e); 317 } finally { 318 try { 319 if (is != null) 320 is.close(); 321 } catch (IOException e) { 322 } 323 } 324 } 325 326 PropertyResourceBundle bundle = null; 327 if (mappingsURL != null) { 328 is = null; 329 try { 330 is = mappingsURL.openStream(); 331 bundle = new PropertyResourceBundle (is); 332 } catch (IOException e) { 333 bundle = null; 334 String message = NLS.bind(Messages.IniFileReader_ReadMapError, (new String [] { mappingsURL.toExternalForm() })); 335 return new Status(IStatus.ERROR, PID, 0, message, e); 336 } finally { 337 try { 338 if (is != null) 339 is.close(); 340 } catch (IOException e) { 341 } 342 } 343 } 344 345 ArrayList mappingsList = new ArrayList (); 346 if (bundle != null) { 347 boolean found = true; 348 int i = 0; 349 while (found) { 350 try { 351 mappingsList.add(bundle.getString(Integer.toString(i))); 352 } catch (MissingResourceException e) { 353 found = false; 354 } 355 i++; 356 } 357 } 358 mappings = (String [])mappingsList.toArray(new String [mappingsList.size()]); 359 360 return OK_STATUS; 361 } 362 } 363 | Popular Tags |