1 10 package org.mmbase.util; 11 12 import java.util.*; 13 import org.mmbase.util.logging.*; 14 15 28 public class ReplacingLocalizedString extends LocalizedString { 29 30 private static final Logger log = Logging.getLoggerInstance(ReplacingLocalizedString.class); 31 32 33 private LocalizedString wrapped; 34 private List replacements = new ArrayList(); 35 36 37 protected ReplacingLocalizedString() { 39 40 } 41 42 45 public ReplacingLocalizedString(LocalizedString s) { 46 if (s == null) s = new LocalizedString("NULL"); 47 wrapped = s; 48 } 49 50 public void replaceAll(String regexp, String replacement) { 51 replacements.add(new Entry(regexp, replacement)); 52 } 53 54 protected String replace(String input) { 55 String output = input; 56 Iterator i = replacements.iterator(); 57 while (i.hasNext()) { 58 Map.Entry entry = (Map.Entry) i.next(); 59 try { 60 output = output.replaceAll((String ) entry.getKey(), (String ) entry.getValue()); 61 } catch (Throwable t) { 62 log.warn("Could not replace " + entry + " in " + input + " because " + t); 63 } 64 } 65 return output; 66 } 67 68 public String getKey() { 70 return wrapped.getKey(); 71 } 72 73 public void setKey(String key) { 75 wrapped.setKey(key); 76 } 77 78 public String get(Locale locale) { 80 return replace(wrapped.get(locale)); 81 } 82 83 public void set(String value, Locale locale) { 85 wrapped.set(value, locale); 86 } 87 88 93 public Map asMap() { 94 final Map map = super.asMap(); 95 return new AbstractMap() { 96 public Set entrySet() { 97 return new AbstractSet() { 98 public int size() { 99 return map.size(); 100 } 101 public Iterator iterator() { 102 final Iterator it = map.entrySet().iterator(); 103 return new Iterator() { 104 public boolean hasNext() { 105 return it.hasNext(); 106 } 107 public Object next() { 108 final Map.Entry value = (Map.Entry) it.next(); 109 return new Map.Entry() { 110 public Object getKey() { 111 return value.getKey(); 112 } 113 public Object getValue() { 114 return replace((String ) value.getValue()); 115 } 116 public Object setValue(Object v) { 117 throw new UnsupportedOperationException (); } 119 }; 120 } 121 public void remove() { 122 throw new UnsupportedOperationException (); } 124 }; 125 } 126 }; 127 } 128 }; 129 } 130 131 public void setBundle(String b) { 133 wrapped.setBundle(b); 134 } 135 136 public String toString() { 137 return "replacing-" + wrapped.toString(); 138 } 139 140 public Object clone() { 141 ReplacingLocalizedString clone = (ReplacingLocalizedString) super.clone(); 142 clone.replacements = (List)((ArrayList)replacements).clone(); 143 return clone; 144 145 } 146 149 public static String makeLiteral(String s) { 150 s = s.replaceAll("\\\\", "\\\\\\\\"); 152 return s.replaceAll("\\$", "\\\\\\$"); 153 } 154 155 156 public static void main(String argv[]) { 157 ReplacingLocalizedString s = new ReplacingLocalizedString(new LocalizedString("abcd")); 158 s.replaceAll("b", makeLiteral(argv[0])); 159 System.out.println(s.get(null)); 160 } 161 162 } 163 | Popular Tags |