1 11 package org.eclipse.pde.internal.core; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.net.URLClassLoader ; 17 import java.util.MissingResourceException ; 18 import java.util.PropertyResourceBundle ; 19 import java.util.StringTokenizer ; 20 21 import org.eclipse.core.runtime.Platform; 22 23 24 public class NLResourceHelper { 25 public static final String KEY_PREFIX = "%"; public static final String KEY_DOUBLE_PREFIX = "%%"; private PropertyResourceBundle bundle = null; 28 29 public NLResourceHelper(String name, URL [] locations) { 30 try { 31 InputStream stream = getResourceStream(name, locations); 32 if (stream != null) { 33 bundle = new PropertyResourceBundle (stream); 34 stream.close(); 35 } 36 } catch (IOException e) { 37 } 38 } 39 40 public void dispose() { 41 bundle = null; 42 } 43 44 private InputStream getResourceStream(String name, URL [] locations) { 45 URLClassLoader resourceLoader = new URLClassLoader (locations, null); 46 47 StringTokenizer tokenizer = new StringTokenizer (Platform.getNL(), "_"); String language = tokenizer.nextToken(); 49 String country = (tokenizer.hasMoreTokens() ? tokenizer.nextToken() : ""); String variant = (tokenizer.hasMoreTokens() ? tokenizer.nextToken() : ""); 52 String suffix1 = "_" + language + "_" + country + "_" + variant; String suffix2 = "_" + language + "_" + country; String suffix3 = "_" + language; String suffix4 = ""; 57 String [] suffices = new String [] { suffix1, suffix2, suffix3, suffix4 }; 58 59 InputStream stream = null; 60 for (int i = 0; i < suffices.length; i++) { 61 stream = 62 resourceLoader.getResourceAsStream( 63 name + suffices[i] + ".properties"); if (stream != null) 65 break; 66 } 67 return stream; 68 } 69 70 public String getResourceString(String value) { 71 String 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(" "); String key = ix == -1 ? s : s.substring(0, ix); 81 String 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 e) { 89 return dflt; 90 } 91 } 92 93 public boolean resourceExists(String value) { 94 if (bundle == null) 95 return false; 96 try { 97 bundle.getString(value.trim().substring(1)); 98 return true; 99 } catch (MissingResourceException e) { 100 return false; 101 } 102 } 103 104 } 105 | Popular Tags |