1 14 package org.jahia.resourcebundle; 15 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Locale ; 19 import java.util.MissingResourceException ; 20 import java.util.ResourceBundle ; 21 import java.util.Vector ; 22 23 import org.jahia.exceptions.JahiaException; 24 import org.jahia.registries.ServicesRegistry; 25 import org.jahia.utils.JahiaTools; 26 27 28 37 public class ResourceBundleMarker implements Comparator { 38 39 42 private String resourceBundleID; 43 44 47 private String resourceKey; 48 49 52 private String value; 53 54 57 private String defaultValue; 58 59 public ResourceBundleMarker( String resourceBundleID, 60 String resourceKey, 61 String defaultValue ){ 62 this.resourceBundleID = resourceBundleID; 63 this.resourceKey = resourceKey; 64 this.defaultValue = defaultValue; 65 } 66 67 72 public String getResourceBundleID(){ 73 return this.resourceBundleID; 74 } 75 76 81 public String getResourceKey(){ 82 return this.resourceKey; 83 } 84 85 92 public String getValue(){ 93 if ( this.value == null ){ 94 return ""; 95 } 96 return this.value; 97 } 98 99 106 public String getValue(Locale locale) 107 throws JahiaException { 108 109 String result = this.getDefaultValue(); 110 111 ResourceBundleDefinition rbDef = 112 (ResourceBundleDefinition)ServicesRegistry.getInstance() 113 .getResourceBundleService() 114 .getResourceBundle(this.getResourceBundleID()); 115 if ( rbDef == null ){ 116 return result; 117 } 118 119 try { 120 ResourceBundle res = 121 ResourceBundle.getBundle(rbDef.getResourceBundleFile(), 122 locale); 123 result = JahiaResourceBundle.getString(res, this.getResourceKey(), locale); 125 } catch ( MissingResourceException mre ) { 126 } 128 return result; 129 } 130 131 143 public String getValueFromResourceKey(String resourceKey, Locale locale) 144 throws JahiaException { 145 146 String result = this.getDefaultValue(); 147 148 ResourceBundleDefinition rbDef = 149 (ResourceBundleDefinition)ServicesRegistry.getInstance() 150 .getResourceBundleService() 151 .getResourceBundle(this.getResourceBundleID()); 152 if ( rbDef == null ){ 153 return this.getDefaultValue(); 154 } 155 156 try { 157 ResourceBundle res = 158 ResourceBundle.getBundle(rbDef.getResourceBundleFile(), 159 locale); 160 result = JahiaResourceBundle.getString(res, this.getResourceKey(), locale); 162 } catch ( MissingResourceException mre ) { 163 } 165 return result; 166 } 167 168 173 public String getDefaultValue(){ 174 return this.defaultValue; 175 } 176 177 187 public String drawMarker(){ 188 return drawMarker(this.getResourceBundleID(), 189 this.getResourceKey(), 190 this.getDefaultValue()); 191 } 192 193 198 public void setValue(String value){ 199 this.value = value; 200 } 201 202 209 public void setValue(Locale locale) 210 throws JahiaException { 211 this.value = this.getValueFromResourceKey(this.getResourceKey(),locale); 212 } 213 214 221 public static ResourceBundleMarker parseMarkerValue(String markerStr){ 222 223 if ( markerStr == null ){ 224 return null; 225 } 226 227 ResourceBundleMarker marker = null; 228 String val = markerStr.trim(); 229 String resourceBundleID = ""; 230 String resourceKey = ""; 231 String defaultValue = ""; 232 233 if ( val.startsWith("<jahia-resource") && val.endsWith("/>") ){ 234 235 try { 236 237 int pos = val.indexOf(" id=\""); 238 if ( pos != -1 ){ 239 resourceBundleID = 240 val.substring(pos + 5, 241 pos + 5 + val.substring(pos + 5).indexOf("\"")); 242 } 243 244 pos = val.indexOf(" key=\""); 245 if ( pos != -1 ){ 246 resourceKey = 247 val.substring(pos + 6, 248 pos + 6 + val.substring(pos + 6).indexOf("\"")); 249 } 250 251 pos = val.indexOf(" default-value=\""); 252 if ( pos != -1 ){ 253 defaultValue = 254 val.substring(pos + 16, 255 val.lastIndexOf("\"")); 256 } 257 258 marker = new ResourceBundleMarker(resourceBundleID,resourceKey, 259 defaultValue); 260 } catch ( Throwable t ){ 261 } 263 } 264 265 return marker; 266 } 267 268 280 public static String getValue(String value, Locale locale) 281 throws JahiaException { 282 283 ResourceBundleMarker marker = 284 ResourceBundleMarker.parseMarkerValue(value); 285 if ( marker == null ){ 286 return value; 287 } 288 289 String result = marker.getDefaultValue(); 290 291 ResourceBundleDefinition rbDef = 292 (ResourceBundleDefinition)ServicesRegistry.getInstance() 293 .getResourceBundleService() 294 .getResourceBundle(marker.getResourceBundleID()); 295 if ( rbDef == null ){ 296 return result; 297 } 298 299 try { 300 ResourceBundle res = 301 ResourceBundle.getBundle(rbDef.getResourceBundleFile(), 302 locale); 303 result = JahiaResourceBundle.getString(res, marker.getResourceKey(), locale); 305 } catch ( MissingResourceException mre ) { 306 } 308 return result; 309 } 310 311 321 public static Vector buildResourceBundleMarkers(String enumValues, 322 Locale processingLocale) 323 throws JahiaException { 324 325 String [] tokens = JahiaTools.getTokens(enumValues,":"); 326 Vector markers = new Vector (); 327 for ( int i=0 ; i<tokens.length ; i++ ) 328 { 329 ResourceBundleMarker marker = 331 ResourceBundleMarker.parseMarkerValue(tokens[i]); 332 if ( marker == null ){ 333 marker = new ResourceBundleMarker("","",""); 337 marker.setValue(tokens[i]); 338 } else { 339 marker.setValue(processingLocale); 341 } 342 markers.add(marker); 343 } 344 ResourceBundleMarker compMarker = new ResourceBundleMarker("","",""); 346 compMarker.setValue(""); 347 Collections.sort(markers,compMarker); 348 return markers; 349 } 350 351 361 public static String drawMarker(String resourceBundleID, 362 String resourceKey, String defaultValue ){ 363 364 StringBuffer buff = new StringBuffer ("<jahia-resource "); 365 buff.append("id=\""); 366 buff.append(resourceBundleID); 367 buff.append("\" key=\""); 368 buff.append(resourceKey); 369 buff.append("\" default-value=\""); 370 buff.append(defaultValue); 371 buff.append("\"/>"); 372 return buff.toString(); 373 374 } 375 376 383 public int compare(Object c1, Object c2) throws ClassCastException { 384 385 return ((ResourceBundleMarker)c1) 386 .getValue() 387 .compareToIgnoreCase(((ResourceBundleMarker)c2).getValue().toLowerCase()); 388 389 } 390 391 } | Popular Tags |