1 11 package org.eclipse.osgi.framework.internal.core; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.*; 17 import org.eclipse.osgi.framework.util.Headers; 18 import org.osgi.framework.Bundle; 19 import org.osgi.framework.Constants; 20 21 24 public class ManifestLocalization { 25 private AbstractBundle bundle = null; 26 private Dictionary rawHeaders = null; 27 private Dictionary defaultLocaleHeaders = null; 28 private Hashtable cache = new Hashtable(5); 29 30 public ManifestLocalization(AbstractBundle bundle, Dictionary rawHeaders) { 31 this.bundle = bundle; 32 this.rawHeaders = rawHeaders; 33 } 34 35 protected Dictionary getHeaders(String localeString) { 36 if (localeString.length() == 0) 37 return (rawHeaders); 38 boolean isDefaultLocale = false; 39 String defaultLocale = Locale.getDefault().toString(); 40 if (localeString.equals(defaultLocale)) { 41 if (defaultLocaleHeaders != null) 42 return (defaultLocaleHeaders); 43 isDefaultLocale = true; 44 } 45 try { 46 bundle.checkValid(); 47 } catch (IllegalStateException ex) { 48 if (defaultLocaleHeaders != null) 50 return defaultLocaleHeaders; 51 return (rawHeaders); 52 } 53 ResourceBundle localeProperties = getResourceBundle(localeString); 54 if (localeProperties == null && !isDefaultLocale) 55 localeProperties = getResourceBundle(defaultLocale); 57 Enumeration e = this.rawHeaders.keys(); 58 Headers localeHeaders = new Headers(this.rawHeaders.size()); 59 while (e.hasMoreElements()) { 60 String key = (String ) e.nextElement(); 61 String value = (String ) this.rawHeaders.get(key); 62 if (value.startsWith("%") && (value.length() > 1)) { String propertiesKey = value.substring(1); 64 try { 65 value = localeProperties == null ? propertiesKey : (String ) localeProperties.getObject(propertiesKey); 66 } catch (MissingResourceException ex) { 67 value = propertiesKey; 68 } 69 } 70 localeHeaders.set(key, value); 71 } 72 localeHeaders.setReadOnly(); 73 if (isDefaultLocale) { 74 defaultLocaleHeaders = localeHeaders; 75 } 76 return (localeHeaders); 77 } 78 79 private String [] buildNLVariants(String nl) { 80 ArrayList result = new ArrayList(); 81 int lastSeparator; 82 while ((lastSeparator = nl.lastIndexOf('_')) != -1) { 83 result.add(nl); 84 if (lastSeparator != -1) { 85 nl = nl.substring(0, lastSeparator); 86 } 87 } 88 result.add(nl); 89 result.add(""); return (String []) result.toArray(new String [result.size()]); 92 } 93 94 98 protected ResourceBundle getResourceBundle(String localeString) { 99 String propertiesLocation = (String ) rawHeaders.get(Constants.BUNDLE_LOCALIZATION); 100 if (propertiesLocation == null) { 101 propertiesLocation = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME; 102 } 103 104 BundleResourceBundle result = (BundleResourceBundle) cache.get(localeString); 105 if (result != null) 106 return (ResourceBundle) (result.isEmpty() ? null : result); 107 String [] nlVarients = buildNLVariants(localeString); 108 BundleResourceBundle parent = null; 109 for (int i = nlVarients.length - 1; i >= 0; i--) { 110 BundleResourceBundle varientBundle = null; 111 URL varientURL = findResource(propertiesLocation + (nlVarients[i].equals("") ? nlVarients[i] : '_' + nlVarients[i]) + ".properties"); if (varientURL == null) { 113 varientBundle = (BundleResourceBundle) cache.get(nlVarients[i]); 114 } else { 115 InputStream resourceStream = null; 116 try { 117 resourceStream = varientURL.openStream(); 118 varientBundle = new LocalizationResourceBundle(resourceStream); 119 } catch (IOException e) { 120 } finally { 122 if (resourceStream != null) { 123 try { 124 resourceStream.close(); 125 } catch (IOException e3) { 126 } 128 } 129 } 130 } 131 132 if (varientBundle == null) { 133 varientBundle = new EmptyResouceBundle(); 134 } 135 if (parent != null) 136 varientBundle.setParent((ResourceBundle) parent); 137 cache.put(nlVarients[i], varientBundle); 138 parent = varientBundle; 139 } 140 result = (BundleResourceBundle) cache.get(localeString); 141 return (ResourceBundle) (result.isEmpty() ? null : result); 142 } 143 144 private URL findResource(String resource) { 145 AbstractBundle searchBundle = bundle; 146 if (bundle.isResolved()) { 147 if (bundle.isFragment() && bundle.getHosts() != null) { 148 searchBundle = bundle.getHosts()[0].getBundleHost(); 150 if (searchBundle.getState() == Bundle.UNINSTALLED) 151 searchBundle = bundle; 152 } 153 return findInResolved(resource, searchBundle); 154 } 155 return findInBundle(resource, searchBundle); 156 } 157 158 private URL findInResolved(String filePath, AbstractBundle bundleHost) { 159 160 URL result = findInBundle(filePath, bundleHost); 161 if (result != null) 162 return result; 163 return findInFragments(filePath, bundleHost); 164 } 165 166 private URL findInBundle(String filePath, AbstractBundle searchBundle) { 167 return searchBundle.getEntry(filePath); 168 } 169 170 private URL findInFragments(String filePath, AbstractBundle searchBundle) { 171 org.osgi.framework.Bundle[] fragments = searchBundle.getFragments(); 172 URL fileURL = null; 173 for (int i = 0; fragments != null && i < fragments.length && fileURL == null; i++) { 174 if (fragments[i].getState() != Bundle.UNINSTALLED) 175 fileURL = fragments[i].getEntry(filePath); 176 } 177 return fileURL; 178 } 179 180 private abstract interface BundleResourceBundle { 181 void setParent(ResourceBundle parent); 182 183 boolean isEmpty(); 184 } 185 186 private class LocalizationResourceBundle extends PropertyResourceBundle implements BundleResourceBundle { 187 public LocalizationResourceBundle(InputStream in) throws IOException { 188 super(in); 189 } 190 191 public void setParent(ResourceBundle parent) { 192 super.setParent(parent); 193 } 194 195 public boolean isEmpty() { 196 return false; 197 } 198 } 199 200 private class EmptyResouceBundle extends ResourceBundle implements BundleResourceBundle { 201 public Enumeration getKeys() { 202 return null; 203 } 204 205 protected Object handleGetObject(String arg0) throws MissingResourceException { 206 return null; 207 } 208 209 public void setParent(ResourceBundle parent) { 210 super.setParent(parent); 211 } 212 213 public boolean isEmpty() { 214 if (parent == null) 215 return true; 216 return ((BundleResourceBundle) parent).isEmpty(); 217 } 218 } 219 } 220 | Popular Tags |