1 19 20 package org.netbeans.updater; 21 22 import java.util.*; 23 import java.io.*; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 28 class Localization { 29 30 private static final String FILE_SEPARATOR = System.getProperty ("file.separator"); private static final String LOCALE_DIR = "modules" + FILE_SEPARATOR + "ext" + FILE_SEPARATOR + "locale"; private static final String BUNDLE_NAME = "org/netbeans/updater/Bundle"; private static final String BUNDLE_EXT = ".properties"; private static final String UPDATER_JAR = "updater"; private static final String UPDATER_JAR_EXT = ".jar"; 37 private static ClassLoader brandedLoader = null; 38 39 private static String brandingToken = null; 40 41 private static HashMap bundleCache = new HashMap(); 42 43 public static String getBranding() { 44 if (brandingToken != null) { 45 init(); 46 } 47 return brandingToken; 48 } 49 50 public static String getBrandedString( String key ) { 51 init(); 53 for( LocaleIterator li = new LocaleIterator( Locale.getDefault() ); li.hasNext(); ) { 55 try { 56 ResourceBundle bundle = findBrandedBundle( (String )li.next() ); 57 58 if ( bundle != null ) { 59 String brandedString = bundle.getString( key ); 61 if ( brandedString != null ) { 62 return brandedString; } 64 } 66 } 67 catch ( java.util.MissingResourceException e ) { 68 } 70 } 71 return null; 72 } 73 74 private static ResourceBundle findBrandedBundle( String loc ) { 75 76 ResourceBundle bundle = (ResourceBundle)bundleCache.get( loc ); if ( bundle != null ) { 78 return bundle; 79 } 80 81 83 84 InputStream is = brandedLoader.getResourceAsStream( BUNDLE_NAME + loc + BUNDLE_EXT ); 85 if (is != null) { 86 try { 87 try { 88 Properties p = new Properties(); 89 p.load(is); 90 bundle= new PBundle( p, new Locale( "" ) ); 91 bundleCache.put( loc, bundle ); 92 return bundle; 93 } finally { 94 is.close(); 95 } 96 } catch (IOException e) { 97 return null; 98 } 99 } 100 101 return null; 102 } 103 104 105 public static URL getBrandedResource( String base, String ext ) { 106 init(); 108 for( LocaleIterator li = new LocaleIterator( Locale.getDefault() ); li.hasNext(); ) { 110 URL url = brandedLoader.getResource( base + li.next() + ext ); 111 if ( url != null ) { 112 return url; 113 } 114 } 115 116 return null; 117 } 118 119 120 public static InputStream getBrandedResourceAsStream( String base, String ext ) { 121 init(); 123 for( LocaleIterator li = new LocaleIterator( Locale.getDefault() ); li.hasNext(); ) { 125 InputStream is = brandedLoader.getResourceAsStream( base + li.next() + ext ); 126 if ( is != null ) { 127 return is; 128 } 129 } 130 131 return null; 132 } 133 134 public static void setBranding (String branding) { 135 brandingToken = branding; 136 } 137 138 private static synchronized void init() { 140 if (brandingToken == null) { 141 brandingToken = initBranding(); 143 } 144 if (brandedLoader == null) { 145 146 brandedLoader = Localization.class.getClassLoader(); 148 149 List locJarURLs = new ArrayList(); 151 152 for( LocaleIterator li = new LocaleIterator( Locale.getDefault() ); li.hasNext(); ) { 153 String localeName = li.next().toString (); 154 Iterator it = UpdateTracking.clusters (true).iterator (); 156 while (it.hasNext ()) { 157 File cluster = (File)it.next (); 158 File locJar = new File( cluster.getPath () + FILE_SEPARATOR + LOCALE_DIR + FILE_SEPARATOR + UPDATER_JAR + localeName + UPDATER_JAR_EXT ); 159 if ( locJar.exists() ) { try { 161 locJarURLs.add( locJar.toURI().toURL() ); } 163 catch ( MalformedURLException e ) { 164 } 166 } 167 } 168 } 169 170 if ( !locJarURLs.isEmpty() ) { URL urls[] = new URL [ locJarURLs.size() ]; 173 locJarURLs.toArray( urls ); 174 175 brandedLoader = new URLClassLoader ( urls, brandedLoader ); 177 } 178 179 } 180 } 181 182 184 private static String initBranding() { 185 BufferedReader in = null; 186 String s = null; 187 try { 188 File brandf = new File (org.netbeans.updater.UpdateTracking.getPlatformDir(), 189 "lib" + FILE_SEPARATOR + "branding"); in = new BufferedReader(new FileReader(brandf)); 191 if (in.ready()) { 192 System.out.println("Warning - It's obsolete. Use --branding <branding> instead 'branding' file."); 193 s = in.readLine(); 194 } 195 } 196 catch (IOException e) { 197 } 198 finally { 199 if (in != null) try { in.close(); } catch (IOException e) { }; 200 } 201 return s; 202 } 203 204 232 private static class LocaleIterator extends Object implements Iterator { 233 234 private boolean defaultInProgress = false; 235 236 237 private boolean empty = false; 238 239 240 private Locale locale, initLocale; 241 242 243 private String current; 244 245 246 private String branding; 247 248 251 public LocaleIterator (Locale locale) { 252 this.locale = this.initLocale = locale; 253 if (locale.equals(Locale.getDefault())) { 254 defaultInProgress = true; 255 } 256 current = '_' + locale.toString(); 257 if (brandingToken == null) 258 branding = null; 259 else 260 branding = "_" + brandingToken; } 263 264 267 public Object next () throws NoSuchElementException { 268 if (current == null) 269 throw new NoSuchElementException(); 270 271 final String ret; 272 if (branding == null) { 273 ret = current; 274 } else { 275 ret = branding + current; 276 } 277 int lastUnderbar = current.lastIndexOf('_'); 278 if (lastUnderbar == 0) { 279 if (empty) 280 reset (); 281 else { 282 current = ""; empty = true; 284 } 285 } 286 else { 287 if (lastUnderbar == -1) { 288 if (defaultInProgress) 289 reset (); 290 else { 291 locale = Locale.getDefault(); 294 current = '_' + locale.toString(); 295 defaultInProgress = true; 296 } 297 } 298 else { 299 current = current.substring(0, lastUnderbar); 300 } 301 } 302 return ret; 304 } 305 306 310 private void reset () { 311 if (branding != null) { 312 current = '_' + initLocale.toString (); 313 int idx = branding.lastIndexOf ('_'); 314 if (idx == 0) 315 branding = null; 316 else 317 branding = branding.substring (0, idx); 318 empty = false; 319 } else { 320 current = null; 321 } 322 } 323 324 325 public boolean hasNext () { 326 return (current != null); 327 } 328 329 public void remove () throws UnsupportedOperationException { 330 throw new UnsupportedOperationException (); 331 } 332 333 } 335 343 private static final class PBundle extends ResourceBundle { 344 private final Map m; private final Locale locale; 346 351 public PBundle(Map m, Locale locale) { 352 this.m = m; 353 this.locale = locale; 354 } 355 public Enumeration getKeys() { 356 return Collections.enumeration(m.keySet()); 357 } 358 protected Object handleGetObject(String key) { 359 return m.get(key); 360 } 361 public Locale getLocale() { 362 return locale; 363 } 364 } 365 366 } | Popular Tags |