KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > ReplacingLocalizedString


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.util.*;
13 import org.mmbase.util.logging.*;
14
15 /**
16  * Extends and wraps LocalizedString. It extends to look like a 'normal' LocalizedString, but it
17  * overrides 'get' to do token-replacements first.
18  *
19  * This functionality is not in LocalizedString itself, because now you can have different
20  * replacements on the same value set represented by a LocalizedString withouth having to copy
21  * everything every time.
22  *
23  *
24  * @author Michiel Meeuwissen
25  * @version $Id: ReplacingLocalizedString.java,v 1.4 2005/10/21 16:46:11 michiel Exp $
26  * @since MMBase-1.8
27  */

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     // just for the contract of Serializable
38
protected ReplacingLocalizedString() {
39
40     }
41
42     /**
43      * @param s The wrapped LocalizedString.
44      */

45     public ReplacingLocalizedString(LocalizedString s) {
46         if (s == null) s = new LocalizedString("NULL");
47         wrapped = s;
48     }
49     
50     public void replaceAll(String JavaDoc regexp, String JavaDoc replacement) {
51         replacements.add(new Entry(regexp, replacement));
52     }
53
54     protected String JavaDoc replace(String JavaDoc input) {
55         String JavaDoc 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 JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
61             } catch (Throwable JavaDoc t) {
62                 log.warn("Could not replace " + entry + " in " + input + " because " + t);
63             }
64         }
65         return output;
66     }
67
68     //javadoc inherited
69
public String JavaDoc getKey() {
70         return wrapped.getKey();
71     }
72
73     //javadoc inherited
74
public void setKey(String JavaDoc key) {
75         wrapped.setKey(key);
76     }
77
78     // javadoc inherited
79
public String JavaDoc get(Locale locale) {
80         return replace(wrapped.get(locale));
81     }
82
83     //javadoc inherited
84
public void set(String JavaDoc value, Locale locale) {
85         wrapped.set(value, locale);
86     }
87
88     /**
89      * {@inheritDoc}
90      *
91      * Also takes into account the replacements in the values (but only 'lazily', when actually requested).
92      */

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 JavaDoc next() {
108                                             final Map.Entry value = (Map.Entry) it.next();
109                                             return new Map.Entry() {
110                                                     public Object JavaDoc getKey() {
111                                                         return value.getKey();
112                                                     }
113                                                     public Object JavaDoc getValue() {
114                                                         return replace((String JavaDoc) value.getValue());
115                                                     }
116                                                     public Object JavaDoc setValue(Object JavaDoc v) {
117                                                         throw new UnsupportedOperationException JavaDoc(); // map is umodifiable
118
}
119                                                 };
120                                         }
121                                         public void remove() {
122                                             throw new UnsupportedOperationException JavaDoc(); // map is umodifiable
123
}
124                                     };
125                             }
126                         };
127                 }
128             };
129     }
130
131     // javadoc inherited
132
public void setBundle(String JavaDoc b) {
133         wrapped.setBundle(b);
134     }
135
136     public String JavaDoc toString() {
137         return "replacing-" + wrapped.toString();
138     }
139
140     public Object JavaDoc clone() {
141         ReplacingLocalizedString clone = (ReplacingLocalizedString) super.clone();
142         clone.replacements = (List)((ArrayList)replacements).clone();
143         return clone;
144
145     }
146     /**
147      * Utility method for second argument of replaceAll
148      */

149     public static String JavaDoc makeLiteral(String JavaDoc s) {
150         // sometimes, implementing java looks rather idiotic, but honestely, this is correct!
151
s = s.replaceAll("\\\\", "\\\\\\\\");
152         return s.replaceAll("\\$", "\\\\\\$");
153     }
154
155
156     public static void main(String JavaDoc 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