KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > configurator > branding > IniFileReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.update.internal.configurator.branding;
12
13 import java.io.*;
14 import java.net.*;
15 import java.text.MessageFormat JavaDoc; // Can't use ICU, possible launch problem?
16
import java.util.ArrayList JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.MissingResourceException JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.util.PropertyResourceBundle JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
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 /**
31  * Reads the information found in an "INI" file. This file must be in a
32  * standard Java properties format. A properties file may also be provided
33  * to NL values in the INI file - values must start with the % prefix. A
34  * mapping file may also be provided that contains "fill-ins" for the
35  * properties file - format being "n = some text", where n is a number.
36  */

37 public class IniFileReader {
38     private static final String JavaDoc PID = "org.eclipse.update.configurator"; //$NON-NLS-1$
39
private static final Status OK_STATUS = new Status(IStatus.OK, PID, 0, "", null); //$NON-NLS-1$
40
private static final String JavaDoc KEY_PREFIX = "%"; //$NON-NLS-1$
41
private static final String JavaDoc KEY_DOUBLE_PREFIX = "%%"; //$NON-NLS-1$
42
private static final String JavaDoc NLS_TAG = "$nl$"; //$NON-NLS-1$
43

44     private String JavaDoc featureId;
45     private String JavaDoc pluginId;
46     private String JavaDoc iniFilename;
47     private String JavaDoc propertiesFilename;
48     private String JavaDoc mappingsFilename;
49     private Properties JavaDoc ini = null;
50     private PropertyResourceBundle JavaDoc properties = null;
51     private String JavaDoc[] mappings = null;
52     private Bundle JavaDoc bundle;
53
54     /**
55      * Creates an INI file reader that can parse the contents into key,value pairs.
56      *
57      * @param featureId the unique identifier of the feature, must not be <code>null</code>
58      * @param pluginId the unique identifier of the feature plug-in, must not be <code>null</code>
59      * @param iniFilename the INI file name, must not be <code>null</code>
60      * @param propertiesFilename the properties filename, can be <code>null</code> if not required
61      * @param mappingsFilename the mappings filename, can be <code>null</code> if not required
62      */

63     public IniFileReader(String JavaDoc featureId, String JavaDoc pluginId, String JavaDoc iniFilename, String JavaDoc propertiesFilename, String JavaDoc mappingsFilename) {
64         super();
65         
66         if (featureId == null || pluginId == null || iniFilename == null) {
67             throw new IllegalArgumentException JavaDoc();
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     /**
78      * Read the contents of the INI, properties, and mappings files.
79      * Does nothing if the content has already been read and parsed.
80      *
81      * @return an <code>IStatus</code> indicating the success or failure
82      * of reading and parsing the INI file content
83      */

84     public IStatus load() {
85         if (ini != null)
86             return OK_STATUS;
87             
88         // attempt to locate the corresponding plugin
89
bundle = Utils.getBundle(pluginId);
90         if (bundle == null || bundle.getState() == Bundle.UNINSTALLED || bundle.getState() == Bundle.INSTALLED) {
91             bundle = null; // make it null for other test down the road
92
String JavaDoc message = NLS.bind(Messages.IniFileReader_MissingDesc, (new String JavaDoc[] { featureId }));
93             return new Status(IStatus.ERROR, PID, 0, message, null);
94         }
95
96         // Determine the ini file location
97
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 JavaDoc message = NLS.bind(Messages.IniFileReader_OpenINIError, (new String JavaDoc[] { iniFilename }));
102             return new Status(IStatus.ERROR, PID, 0, message, ioe);
103         }
104         
105         // Determine the properties file location
106
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         // Determine the mappings file location
112
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         // OK to pass null properties and/or mapping file
118
return load(iniURL, propertiesURL, mappingsURL);
119     }
120         
121     /**
122      * Returns the string value for the given key, or <code>null</code>.
123      * The string value is NLS if requested.
124      *
125      * @return the string value for the given key, or <code>null</code>
126      */

127     public String JavaDoc getString(String JavaDoc key, boolean doNls, Hashtable JavaDoc runtimeMappings) {
128         if (ini == null)
129             return null;
130         String JavaDoc value = ini.getProperty(key);
131         if (value != null && doNls)
132             return getResourceString(value, runtimeMappings);
133         return value;
134     }
135
136
137     /**
138      * Returns a URL for the given key, or <code>null</code>.
139      *
140      * @return a URL for the given key, or <code>null</code>
141      */

142     public URL getURL(String JavaDoc key) {
143         if (ini == null)
144             return null;
145
146         URL url = null;
147         String JavaDoc 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     /**
157      * Returns a array of URL for the given key, or <code>null</code>. The
158      * property value should be a comma separated list of urls, tokens for
159      * which bundle cannot build an url will have a null entry.
160      *
161      * @param key name of the property containing the requested urls
162      * @return a URL for the given key, or <code>null</code>
163      * @since 3.0
164      */

165     public URL[] getURLs(String JavaDoc key) {
166         if (ini == null || bundle == null)
167             return null;
168
169         String JavaDoc value = ini.getProperty(key);
170         if (value == null)
171             return null;
172
173         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(value, ","); //$NON-NLS-1$
174
ArrayList JavaDoc array = new ArrayList JavaDoc(10);
175         while (tokens.hasMoreTokens()) {
176             String JavaDoc 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     /**
186      * Returns the feature plugin label, or <code>null</code>.
187      *
188      * @return the feature plugin lable, or <code>null</code> if none.
189      */

190     public String JavaDoc getFeaturePluginLabel() {
191         if (bundle == null)
192             return null;
193         return (String JavaDoc)bundle.getHeaders().get(Constants.BUNDLE_NAME);
194     }
195     
196     /**
197      * Returns the provider name for this feature, or <code>null</code>.
198      *
199      * @return the provider name for this feature, or <code>null</code>
200      */

201     public String JavaDoc getProviderName() {
202         if (bundle == null)
203             return null;
204         return (String JavaDoc)bundle.getHeaders().get(Constants.BUNDLE_VENDOR);
205     }
206     
207     /*
208      * Returns a resource string corresponding to the given argument
209      * value and bundle.
210      * If the argument value specifies a resource key, the string
211      * is looked up in the given resource bundle. If the argument does not
212      * specify a valid key, the argument itself is returned as the
213      * resource string. The key lookup is performed against the
214      * specified resource bundle. If a resource string
215      * corresponding to the key is not found in the resource bundle
216      * the key value, or any default text following the key in the
217      * argument value is returned as the resource string.
218      * A key is identified as a string begining with the "%" character.
219      * Note that the "%" character is stripped off prior to lookup
220      * in the resource bundle.
221      * <p>
222      * For example, assume resource bundle plugin.properties contains
223      * name = Project Name
224      * <pre>
225      * <li>getResourceString("Hello World") returns "Hello World"</li>
226      * <li>getResourceString("%name") returns "Project Name"</li>
227      * <li>getResourceString("%name Hello World") returns "Project Name"</li>
228      * <li>getResourceString("%abcd Hello World") returns "Hello World"</li>
229      * <li>getResourceString("%abcd") returns "%abcd"</li>
230      * <li>getResourceString("%%name") returns "%name"</li>
231      * <li>getResourceString(<code>null</code>) returns <code>null</code></li>
232      * </pre>
233      * </p>
234      *
235      * @param value the value or <code>null</code>
236      * @param runtimeMappings runtime mappings or <code>null</code>
237      * @return the resource string
238      */

239      public String JavaDoc getResourceString(String JavaDoc value, Hashtable JavaDoc runtimeMappings) {
240         
241         if (value == null)
242             return null;
243         String JavaDoc 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(" "); //$NON-NLS-1$
252
String JavaDoc key = ix == -1 ? s : s.substring(0, ix);
253         String JavaDoc dflt = ix == -1 ? s : s.substring(ix + 1);
254
255         if (properties == null)
256             return dflt;
257
258         String JavaDoc result = null;
259         try {
260             result = properties.getString(key.substring(1));
261         } catch (MissingResourceException JavaDoc e) {
262             return dflt;
263         }
264         if (runtimeMappings != null) {
265             for (Enumeration JavaDoc e = runtimeMappings.keys(); e.hasMoreElements();) {
266                 String JavaDoc keyValue = (String JavaDoc) e.nextElement();
267                 int i = result.indexOf(keyValue);
268                 if (i != -1) {
269                     String JavaDoc s1 = result.substring(0,i);
270                     String JavaDoc s2 = (String JavaDoc) runtimeMappings.get(keyValue);
271                     String JavaDoc s3 = result.substring(i+keyValue.length());
272                     result = s1 + s2 + s3;
273                 }
274             }
275         }
276     
277         if (result.indexOf('{') != -1) {
278             // We test for the curly braces since due to NL issues we do not
279
// want to use MessageFormat unless we have to.
280
result = MessageFormat.format(result, mappings);
281         }
282         
283         return result;
284     }
285
286     /*
287      * Read the contents of the ini, properties, and mappings files.
288      */

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 JavaDoc();
295             ini.load(is);
296         } catch (IOException e) {
297             ini = null;
298             String JavaDoc message = NLS.bind(Messages.IniFileReader_ReadIniError, (new String JavaDoc[] { 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 JavaDoc(is);
313             } catch (IOException e) {
314                 properties = null;
315                 String JavaDoc message = NLS.bind(Messages.IniFileReader_ReadPropError, (new String JavaDoc[] { 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 JavaDoc bundle = null;
327         if (mappingsURL != null) {
328             is = null;
329             try {
330                 is = mappingsURL.openStream();
331                 bundle = new PropertyResourceBundle JavaDoc(is);
332             } catch (IOException e) {
333                 bundle = null;
334                 String JavaDoc message = NLS.bind(Messages.IniFileReader_ReadMapError, (new String JavaDoc[] { 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 JavaDoc mappingsList = new ArrayList JavaDoc();
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 JavaDoc e) {
353                     found = false;
354                 }
355                 i++;
356             }
357         }
358         mappings = (String JavaDoc[])mappingsList.toArray(new String JavaDoc[mappingsList.size()]);
359         
360         return OK_STATUS;
361     }
362 }
363
Popular Tags