|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.equinox.internal.p2.touchpoint.eclipse; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.io.InputStream; |
| 15 |
import java.net.URL; |
| 16 |
import java.text.MessageFormat; |
| 17 |
import java.util.*; |
| 18 |
import org.eclipse.core.runtime.*; |
| 19 |
import org.eclipse.osgi.util.NLS; |
| 20 |
import org.osgi.framework.Bundle; |
| 21 |
import org.osgi.framework.Constants; |
| 22 |
|
| 23 |
// Have to use MessageFormat directly, can't use ICU, possible launch problem? |
| 24 |
|
| 25 |
/** |
| 26 |
* Reads the information found in an "INI" file. This file must be in a |
| 27 |
* standard Java properties format. A properties file may also be provided |
| 28 |
* to NL values in the INI file - values must start with the % prefix. A |
| 29 |
* mapping file may also be provided that contains "fill-ins" for the |
| 30 |
* properties file - format being "n = some text", where n is a number. |
| 31 |
*/ |
| 32 |
public class IniFileReader { |
| 33 |
private static final String PID = "org.eclipse.update.configurator"; //$NON-NLS-1$ |
| 34 |
private static final Status OK_STATUS = new Status(IStatus.OK, PID, 0, "", null); //$NON-NLS-1$ |
| 35 |
private static final String KEY_PREFIX = "%"; //$NON-NLS-1$ |
| 36 |
private static final String KEY_DOUBLE_PREFIX = "%%"; //$NON-NLS-1$ |
| 37 |
private static final String NLS_TAG = "$nl$"; //$NON-NLS-1$ |
| 38 |
|
| 39 |
private String featureId; |
| 40 |
private String pluginId; |
| 41 |
private String iniFilename; |
| 42 |
private String propertiesFilename; |
| 43 |
private String mappingsFilename; |
| 44 |
private Properties ini = null; |
| 45 |
private PropertyResourceBundle properties = null; |
| 46 |
private String[] mappings = null; |
| 47 |
private Bundle bundle; |
| 48 |
|
| 49 |
/** |
| 50 |
* Creates an INI file reader that can parse the contents into key,value pairs. |
| 51 |
* |
| 52 |
* @param featureId the unique identifier of the feature, must not be <code>null</code> |
| 53 |
* @param pluginId the unique identifier of the feature plug-in, must not be <code>null</code> |
| 54 |
* @param iniFilename the INI file name, must not be <code>null</code> |
| 55 |
* @param propertiesFilename the properties filename, can be <code>null</code> if not required |
| 56 |
* @param mappingsFilename the mappings filename, can be <code>null</code> if not required |
| 57 |
*/ |
| 58 |
public IniFileReader(String featureId, String pluginId, String iniFilename, String propertiesFilename, String mappingsFilename) { |
| 59 |
super(); |
| 60 |
|
| 61 |
if (featureId == null || pluginId == null || iniFilename == null) { |
| 62 |
throw new IllegalArgumentException(); |
| 63 |
} |
| 64 |
|
| 65 |
this.featureId = featureId; |
| 66 |
this.pluginId = pluginId; |
| 67 |
this.iniFilename = iniFilename; |
| 68 |
this.propertiesFilename = propertiesFilename; |
| 69 |
this.mappingsFilename = mappingsFilename; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Read the contents of the INI, properties, and mappings files. |
| 74 |
* Does nothing if the content has already been read and parsed. |
| 75 |
* |
| 76 |
* @return an <code>IStatus</code> indicating the success or failure |
| 77 |
* of reading and parsing the INI file content |
| 78 |
*/ |
| 79 |
public IStatus load() { |
| 80 |
if (ini != null) |
| 81 |
return OK_STATUS; |
| 82 |
|
| 83 |
// attempt to locate the corresponding plugin |
| 84 |
bundle = Activator.getBundle(pluginId); |
| 85 |
if (bundle == null || bundle.getState() == Bundle.UNINSTALLED || bundle.getState() == Bundle.INSTALLED) { |
| 86 |
bundle = null; // make it null for other test down the road |
| 87 |
String message = NLS.bind("Cannot find plug-in for feature {0}.", (new String[] {featureId})); |
| 88 |
return new Status(IStatus.ERROR, PID, 0, message, null); |
| 89 |
} |
| 90 |
|
| 91 |
// Determine the ini file location |
| 92 |
URL iniURL = null; |
| 93 |
IOException ioe = null; |
| 94 |
iniURL = FileLocator.find(bundle, new Path(NLS_TAG).append(iniFilename), null); |
| 95 |
if (iniURL == null) { |
| 96 |
String message = NLS.bind("Cannot open branding file {0}.", (new String[] {iniFilename})); |
| 97 |
return new Status(IStatus.ERROR, PID, 0, message, ioe); |
| 98 |
} |
| 99 |
|
| 100 |
// Determine the properties file location |
| 101 |
URL propertiesURL = null; |
| 102 |
if (propertiesFilename != null & propertiesFilename.length() > 0) { |
| 103 |
propertiesURL = FileLocator.find(bundle, new Path(NLS_TAG).append(propertiesFilename), null); |
| 104 |
} |
| 105 |
|
| 106 |
// Determine the mappings file location |
| 107 |
URL mappingsURL = null; |
| 108 |
if (mappingsFilename != null && mappingsFilename.length() > 0) { |
| 109 |
mappingsURL = FileLocator.find(bundle, new Path(NLS_TAG).append(mappingsFilename), null); |
| 110 |
} |
| 111 |
|
| 112 |
// OK to pass null properties and/or mapping file |
| 113 |
return load(iniURL, propertiesURL, mappingsURL); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the string value for the given key, or <code>null</code>. |
| 118 |
* The string value is NLS if requested. |
| 119 |
* |
| 120 |
* @return the string value for the given key, or <code>null</code> |
| 121 |
*/ |
| 122 |
public String getString(String key, boolean doNls, Hashtable<String, String> runtimeMappings) { |
| 123 |
if (ini == null) |
| 124 |
return null; |
| 125 |
String value = ini.getProperty(key); |
| 126 |
if (value != null && doNls) |
| 127 |
return getResourceString(value, runtimeMappings); |
| 128 |
return value; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns a URL for the given key, or <code>null</code>. |
| 133 |
* |
| 134 |
* @return a URL for the given key, or <code>null</code> |
| 135 |
*/ |
| 136 |
public URL getURL(String key) { |
| 137 |
if (ini == null) |
| 138 |
return null; |
| 139 |
|
| 140 |
URL url = null; |
| 141 |
String fileName = ini.getProperty(key); |
| 142 |
if (fileName != null) { |
| 143 |
if (bundle == null) |
| 144 |
return null; |
| 145 |
url = FileLocator.find(bundle, new Path(fileName), null); |
| 146 |
} |
| 147 |
return url; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns a array of URL for the given key, or <code>null</code>. The |
| 152 |
* property value should be a comma separated list of urls, tokens for |
| 153 |
* which bundle cannot build an url will have a null entry. |
| 154 |
* |
| 155 |
* @param key name of the property containing the requested urls |
| 156 |
* @return a URL for the given key, or <code>null</code> |
| 157 |
* @since 3.0 |
| 158 |
*/ |
| 159 |
public URL[] getURLs(String key) { |
| 160 |
if (ini == null || bundle == null) |
| 161 |
return null; |
| 162 |
|
| 163 |
String value = ini.getProperty(key); |
| 164 |
if (value == null) |
| 165 |
return null; |
| 166 |
|
| 167 |
StringTokenizer tokens = new StringTokenizer(value, ","); //$NON-NLS-1$ |
| 168 |
ArrayList<URL> array = new ArrayList<URL>(10); |
| 169 |
while (tokens.hasMoreTokens()) { |
| 170 |
String str = tokens.nextToken().trim(); |
| 171 |
array.add(FileLocator.find(bundle, new Path(str), null)); |
| 172 |
} |
| 173 |
return array.toArray(new URL[array.size()]); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns the feature plugin label, or <code>null</code>. |
| 178 |
* |
| 179 |
* @return the feature plugin lable, or <code>null</code> if none. |
| 180 |
*/ |
| 181 |
public String getFeaturePluginLabel() { |
| 182 |
return bundle == null ? null : bundle.getHeaders().get(Constants.BUNDLE_NAME); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the provider name for this feature, or <code>null</code>. |
| 187 |
* |
| 188 |
* @return the provider name for this feature, or <code>null</code> |
| 189 |
*/ |
| 190 |
public String getProviderName() { |
| 191 |
return bundle == null ? null : bundle.getHeaders().get(Constants.BUNDLE_VENDOR); |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* Returns a resource string corresponding to the given argument |
| 196 |
* value and bundle. |
| 197 |
* If the argument value specifies a resource key, the string |
| 198 |
* is looked up in the given resource bundle. If the argument does not |
| 199 |
* specify a valid key, the argument itself is returned as the |
| 200 |
* resource string. The key lookup is performed against the |
| 201 |
* specified resource bundle. If a resource string |
| 202 |
* corresponding to the key is not found in the resource bundle |
| 203 |
* the key value, or any default text following the key in the |
| 204 |
* argument value is returned as the resource string. |
| 205 |
* A key is identified as a string begining with the "%" character. |
| 206 |
* Note that the "%" character is stripped off prior to lookup |
| 207 |
* in the resource bundle. |
| 208 |
* <p> |
| 209 |
* For example, assume resource bundle plugin.properties contains |
| 210 |
* name = Project Name |
| 211 |
* <pre> |
| 212 |
* <li>getResourceString("Hello World") returns "Hello World"</li> |
| 213 |
* <li>getResourceString("%name") returns "Project Name"</li> |
| 214 |
* <li>getResourceString("%name Hello World") returns "Project Name"</li> |
| 215 |
* <li>getResourceString("%abcd Hello World") returns "Hello World"</li> |
| 216 |
* <li>getResourceString("%abcd") returns "%abcd"</li> |
| 217 |
* <li>getResourceString("%%name") returns "%name"</li> |
| 218 |
* <li>getResourceString(<code>null</code>) returns <code>null</code></li> |
| 219 |
* </pre> |
| 220 |
* </p> |
| 221 |
* |
| 222 |
* @param value the value or <code>null</code> |
| 223 |
* @param runtimeMappings runtime mappings or <code>null</code> |
| 224 |
* @return the resource string |
| 225 |
*/ |
| 226 |
public String getResourceString(String value, Hashtable<String, String> runtimeMappings) { |
| 227 |
|
| 228 |
if (value == null) |
| 229 |
return null; |
| 230 |
String s = value.trim(); |
| 231 |
|
| 232 |
if (!s.startsWith(KEY_PREFIX)) |
| 233 |
return s; |
| 234 |
|
| 235 |
if (s.startsWith(KEY_DOUBLE_PREFIX)) |
| 236 |
return s.substring(1); |
| 237 |
|
| 238 |
int ix = s.indexOf(" "); //$NON-NLS-1$ |
| 239 |
String key = ix == -1 ? s : s.substring(0, ix); |
| 240 |
String dflt = ix == -1 ? s : s.substring(ix + 1); |
| 241 |
|
| 242 |
if (properties == null) |
| 243 |
return dflt; |
| 244 |
|
| 245 |
String result = null; |
| 246 |
try { |
| 247 |
result = properties.getString(key.substring(1)); |
| 248 |
} catch (MissingResourceException e) { |
| 249 |
return dflt; |
| 250 |
} |
| 251 |
if (runtimeMappings != null) { |
| 252 |
for (Enumeration<String> e = runtimeMappings.keys(); e.hasMoreElements();) { |
| 253 |
String keyValue = e.nextElement(); |
| 254 |
int i = result.indexOf(keyValue); |
| 255 |
if (i != -1) { |
| 256 |
String s1 = result.substring(0, i); |
| 257 |
String s2 = runtimeMappings.get(keyValue); |
| 258 |
String s3 = result.substring(i + keyValue.length()); |
| 259 |
result = s1 + s2 + s3; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
if (result.indexOf('{') != -1) { |
| 265 |
// We test for the curly braces since due to NL issues we do not |
| 266 |
// want to use MessageFormat unless we have to. |
| 267 |
try { |
| 268 |
result = MessageFormat.format(result, mappings); |
| 269 |
} catch (IllegalArgumentException e) { |
| 270 |
//ignore and return string without bound parameters |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return result; |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* Read the contents of the ini, properties, and mappings files. |
| 279 |
*/ |
| 280 |
private IStatus load(URL iniURL, URL propertiesURL, URL mappingsURL) { |
| 281 |
|
| 282 |
InputStream is = null; |
| 283 |
try { |
| 284 |
is = iniURL.openStream(); |
| 285 |
ini = new Properties(); |
| 286 |
ini.load(is); |
| 287 |
} catch (IOException e) { |
| 288 |
ini = null; |
| 289 |
String message = NLS.bind("Error reading branding file {0}.", (new String[] {iniURL.toExternalForm()})); |
| 290 |
return new Status(IStatus.ERROR, PID, 0, message, e); |
| 291 |
} finally { |
| 292 |
try { |
| 293 |
if (is != null) |
| 294 |
is.close(); |
| 295 |
} catch (IOException e) { |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (propertiesURL != null) { |
| 300 |
is = null; |
| 301 |
try { |
| 302 |
is = propertiesURL.openStream(); |
| 303 |
properties = new PropertyResourceBundle(is); |
| 304 |
} catch (IOException e) { |
| 305 |
properties = null; |
| 306 |
String message = NLS.bind("Error reading branding properties file {0}.", (new String[] {propertiesURL.toExternalForm()})); |
| 307 |
return new Status(IStatus.ERROR, PID, 0, message, e); |
| 308 |
} finally { |
| 309 |
try { |
| 310 |
if (is != null) |
| 311 |
is.close(); |
| 312 |
} catch (IOException e) { |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
PropertyResourceBundle bundle = null; |
| 318 |
if (mappingsURL != null) { |
| 319 |
is = null; |
| 320 |
try { |
| 321 |
is = mappingsURL.openStream(); |
| 322 |
bundle = new PropertyResourceBundle(is); |
| 323 |
} catch (IOException e) { |
| 324 |
bundle = null; |
| 325 |
String message = NLS.bind("Error reading branding mapping file {0}.", (new String[] {mappingsURL.toExternalForm()})); |
| 326 |
return new Status(IStatus.ERROR, PID, 0, message, e); |
| 327 |
} finally { |
| 328 |
try { |
| 329 |
if (is != null) |
| 330 |
is.close(); |
| 331 |
} catch (IOException e) { |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
ArrayList mappingsList = new ArrayList(); |
| 337 |
if (bundle != null) { |
| 338 |
boolean found = true; |
| 339 |
int i = 0; |
| 340 |
while (found) { |
| 341 |
try { |
| 342 |
mappingsList.add(bundle.getString(Integer.toString(i))); |
| 343 |
} catch (MissingResourceException e) { |
| 344 |
found = false; |
| 345 |
} |
| 346 |
i++; |
| 347 |
} |
| 348 |
} |
| 349 |
mappings = (String[]) mappingsList.toArray(new String[mappingsList.size()]); |
| 350 |
|
| 351 |
return OK_STATUS; |
| 352 |
} |
| 353 |
} |