1 5 6 package org.exoplatform.commons.utils; 7 8 import java.util.Enumeration ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.Locale ; 12 import java.util.Map ; 13 import java.util.ResourceBundle ; 14 import java.util.Set ; 15 import java.util.Vector ; 16 import java.util.regex.Matcher ; 17 import java.util.regex.Pattern ; 18 19 23 public class MapResourceBundle extends ResourceBundle { 24 25 private final static String REGEXP = "#\\{.*\\}"; 26 27 private Map props = new HashMap (); 28 private ResourceBundle rB; 29 private Locale locale; 30 31 public MapResourceBundle(Locale l) { 32 this.locale = l; 33 } 34 35 public MapResourceBundle(ResourceBundle rB, Locale l) { 36 this.locale = l; 37 this.rB = rB; 38 initMap(); 39 } 40 41 private void initMap() { 42 Enumeration e = rB.getKeys(); 43 while (e.hasMoreElements()) { 44 String s = (String ) e.nextElement(); 45 Object value = rB.getObject(s); 46 try { 47 String [] newArray = rB.getStringArray(s); 48 if (props.get(s) == null) { 49 props.put(s, newArray); 50 } 51 } catch (ClassCastException ex) { 52 props.put(s, value); 53 } 54 } 55 } 56 57 protected Object handleGetObject(String key) { 58 return props.get(key); 59 } 60 61 public Enumeration getKeys() { 62 return new Vector (props.keySet()).elements(); 63 } 64 65 public Locale getLocale() { 66 return this.locale; 67 } 68 69 public void add(String key, Object value){ 70 props.put(key, value); 71 } 72 73 public void remove(String key){ 74 props.remove(key); 75 } 76 77 public void merge(ResourceBundle bundle){ 78 Enumeration e = bundle.getKeys(); 79 while (e.hasMoreElements()) { 80 String s = (String ) e.nextElement(); 81 Object value = bundle.getObject(s); 82 try { 83 String [] newArray = bundle.getStringArray(s); 84 if (props.get(s) == null) { 85 props.put(s, newArray); 86 } 87 } catch (ClassCastException ex) { 88 props.put(s, value); 89 } 90 } 91 } 92 93 public void resolveDependencies(){ 94 Map tempMap = new HashMap (); 95 Set keys = props.keySet(); 96 Pattern pattern = Pattern.compile(REGEXP); 97 for (Iterator iter = keys.iterator(); iter.hasNext();) { 98 String element = (String ) iter.next(); 99 String value = lookupKey(element, pattern); 100 tempMap.put(element, value); 101 } 102 props = tempMap; 103 } 104 105 private String lookupKey(String key, Pattern pattern){ 106 String s = (String ) props.get(key); 107 if(s == null) return key; 108 Matcher matcher = pattern.matcher(s); 109 if(matcher.find()){ 110 return recursivedResolving(s, pattern); 111 } 112 return s; 113 } 114 115 private String recursivedResolving(String key, Pattern pattern){ 116 String resolved = key; 117 StringBuffer sB = new StringBuffer (); 118 while(resolved.indexOf('#') != -1){ 119 sB.setLength(0) ; 120 int firstIndex = resolved.indexOf('#'); 121 int lastIndex = resolved.indexOf('}', firstIndex); 122 String realKey = resolved.substring(firstIndex + 2, lastIndex); 123 sB.append(resolved.substring(0, firstIndex)); 124 sB.append(lookupKey(realKey, pattern)); 125 sB.append(resolved.substring(lastIndex + 1)); 126 resolved = sB.toString(); 127 } 128 return resolved; 129 } 130 } | Popular Tags |