KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > NLResourceHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.core;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.net.URLClassLoader JavaDoc;
17 import java.util.MissingResourceException JavaDoc;
18 import java.util.PropertyResourceBundle JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.eclipse.core.runtime.Platform;
22
23
24 public class NLResourceHelper {
25     public static final String JavaDoc KEY_PREFIX = "%"; //$NON-NLS-1$
26
public static final String JavaDoc KEY_DOUBLE_PREFIX = "%%"; //$NON-NLS-1$
27
private PropertyResourceBundle JavaDoc bundle = null;
28
29     public NLResourceHelper(String JavaDoc name, URL JavaDoc[] locations) {
30         try {
31             InputStream JavaDoc stream = getResourceStream(name, locations);
32             if (stream != null) {
33                 bundle = new PropertyResourceBundle JavaDoc(stream);
34                 stream.close();
35             }
36         } catch (IOException JavaDoc e) {
37         }
38     }
39     
40     public void dispose() {
41         bundle = null;
42     }
43
44     private InputStream JavaDoc getResourceStream(String JavaDoc name, URL JavaDoc[] locations) {
45         URLClassLoader JavaDoc resourceLoader = new URLClassLoader JavaDoc(locations, null);
46         
47         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(Platform.getNL(), "_"); //$NON-NLS-1$
48
String JavaDoc language = tokenizer.nextToken();
49         String JavaDoc country = (tokenizer.hasMoreTokens() ? tokenizer.nextToken() : ""); //$NON-NLS-1$
50
String JavaDoc variant = (tokenizer.hasMoreTokens() ? tokenizer.nextToken() : ""); //$NON-NLS-1$
51

52         String JavaDoc suffix1 = "_" + language + "_" + country + "_" + variant; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
53
String JavaDoc suffix2 = "_" + language + "_" + country; //$NON-NLS-1$ //$NON-NLS-2$
54
String JavaDoc suffix3 = "_" + language; //$NON-NLS-1$
55
String JavaDoc suffix4 = ""; //$NON-NLS-1$
56

57         String JavaDoc[] suffices = new String JavaDoc[] { suffix1, suffix2, suffix3, suffix4 };
58
59         InputStream JavaDoc stream = null;
60         for (int i = 0; i < suffices.length; i++) {
61             stream =
62                 resourceLoader.getResourceAsStream(
63                     name + suffices[i] + ".properties"); //$NON-NLS-1$
64
if (stream != null)
65                 break;
66         }
67         return stream;
68     }
69
70     public String JavaDoc getResourceString(String JavaDoc value) {
71         String JavaDoc s = value.trim();
72
73         if (!s.startsWith(KEY_PREFIX))
74             return s;
75
76         if (s.startsWith(KEY_DOUBLE_PREFIX))
77             return s.substring(1);
78
79         int ix = s.indexOf(" "); //$NON-NLS-1$
80
String JavaDoc key = ix == -1 ? s : s.substring(0, ix);
81         String JavaDoc dflt = ix == -1 ? s : s.substring(ix + 1);
82
83         if (bundle == null)
84             return dflt;
85
86         try {
87             return bundle.getString(key.substring(1));
88         } catch (MissingResourceException JavaDoc e) {
89             return dflt;
90         }
91     }
92     
93     public boolean resourceExists(String JavaDoc value) {
94         if (bundle == null)
95             return false;
96         try {
97             bundle.getString(value.trim().substring(1));
98             return true;
99         } catch (MissingResourceException JavaDoc e) {
100             return false;
101         }
102     }
103
104 }
105
Popular Tags