KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > utils > MapResourceBundle


1  /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.commons.utils;
7
8 import java.util.Enumeration JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Locale JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.ResourceBundle JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.Vector JavaDoc;
16 import java.util.regex.Matcher JavaDoc;
17 import java.util.regex.Pattern JavaDoc;
18
19 /**
20  * @author Benjamin Mestrallet
21  * benjamin.mestrallet@exoplatform.com
22  */

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