Author: nfilotto
Date: 2010-08-27 03:03:02 -0400 (Fri, 27 Aug 2010)
New Revision: 2996
Modified:
kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java
kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestMapResourceBundle.java
kernel/trunk/exo.kernel.commons/src/test/resources/org/exoplatform/commons/utils/resources.properties
Log:
EXOJCR-922: We use a call stack to check if a key has already been asked without being
resolved
Other little improvements:
* The StringBuffer has been replaced with a StringBuilder
* The tempMap is initialized with the content of props to avoid resolving several time the
same value of a given key
Modified:
kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java
===================================================================
---
kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java 2010-08-26
15:30:31 UTC (rev 2995)
+++
kernel/trunk/exo.kernel.commons/src/main/java/org/exoplatform/commons/utils/MapResourceBundle.java 2010-08-27
07:03:02 UTC (rev 2996)
@@ -21,6 +21,7 @@
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
@@ -36,6 +37,11 @@
public class MapResourceBundle extends ResourceBundle implements Serializable
{
+ /**
+ * The serial version UID
+ */
+ private static final long serialVersionUID = -7020823660841958748L;
+
private final static String REGEXP = "#\\{.*\\}";
private Map props = new HashMap();
@@ -123,35 +129,42 @@
public void resolveDependencies()
{
- Map tempMap = new HashMap();
- Set keys = props.keySet();
+ Map tempMap = new HashMap(props);
+ Set keys = tempMap.keySet();
Pattern pattern = Pattern.compile(REGEXP);
for (Iterator iter = keys.iterator(); iter.hasNext();)
{
String element = (String)iter.next();
- String value = lookupKey(element, pattern);
+ String value = lookupKey(tempMap, element, pattern, new
HashSet<String>());
tempMap.put(element, value);
}
props = tempMap;
}
- private String lookupKey(String key, Pattern pattern)
+ private String lookupKey(Map props, String key, Pattern pattern, Set<String>
callStack)
{
String s = (String)props.get(key);
- if (s == null)
+ if (s == null || callStack.contains(key))
+ {
+ // The value cannot be found or it has already been asked which means that
+ // a loop has been detected
return key;
+ }
+ callStack.add(key);
Matcher matcher = pattern.matcher(s);
if (matcher.find())
{
- return recursivedResolving(s, pattern);
+ return recursivedResolving(props, s, pattern, callStack);
}
+ // The value could be resolved thus it can be removed from the callStack
+ callStack.remove(key);
return s;
}
- private String recursivedResolving(String key, Pattern pattern)
+ private String recursivedResolving(Map props, String value, Pattern pattern,
Set<String> callStack)
{
- String resolved = key;
- StringBuffer sB = new StringBuffer();
+ String resolved = value;
+ StringBuilder sB = new StringBuilder();
while (resolved.indexOf("#{") != -1)
{
sB.setLength(0);
@@ -159,7 +172,7 @@
int lastIndex = resolved.indexOf('}', firstIndex);
String realKey = resolved.substring(firstIndex + 2, lastIndex);
sB.append(resolved.substring(0, firstIndex));
- sB.append(lookupKey(realKey, pattern));
+ sB.append(lookupKey(props, realKey, pattern, callStack));
sB.append(resolved.substring(lastIndex + 1));
resolved = sB.toString();
}
Modified:
kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestMapResourceBundle.java
===================================================================
---
kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestMapResourceBundle.java 2010-08-26
15:30:31 UTC (rev 2995)
+++
kernel/trunk/exo.kernel.commons/src/test/java/org/exoplatform/commons/utils/TestMapResourceBundle.java 2010-08-27
07:03:02 UTC (rev 2996)
@@ -33,9 +33,19 @@
ResourceBundle rB =
ResourceBundle.getBundle("org.exoplatform.commons.utils.resources");
MapResourceBundle mapRB = new MapResourceBundle(rB, new Locale("en"));
mapRB.resolveDependencies();
- // System.out.println("KEYYYYYYYYY2
"+mapRB.getString("key1"));
- System.out.println("KEYYYYYYYYY2 " + mapRB.getString("key2"));
- // System.out.println("KEYYYYYYYYY2
"+mapRB.getString("key3"));
+ assertEquals("attention", mapRB.getString("key1"));
+ assertEquals("attention please", mapRB.getString("key3"));
+ assertEquals("attention and attention please alors",
mapRB.getString("key2"));
+ assertEquals("non-existing", mapRB.getString("key4"));
+ assertEquals("key5 StackOverflowError",
mapRB.getString("key5"));
+ assertTrue(mapRB.getString("key6").startsWith("key"));
+ assertTrue(mapRB.getString("key7").startsWith("key"));
+ assertEquals("some other value value",
mapRB.getString("key8"));
+ assertEquals("other value", mapRB.getString("key9"));
+ assertEquals("value", mapRB.getString("key10"));
+ assertEquals("X", mapRB.getString("key11"));
+ assertEquals("-X-", mapRB.getString("key12"));
+ assertEquals("-X-", mapRB.getString("key13"));
+ assertEquals("--X--", mapRB.getString("key14"));
}
-
}
Modified:
kernel/trunk/exo.kernel.commons/src/test/resources/org/exoplatform/commons/utils/resources.properties
===================================================================
---
kernel/trunk/exo.kernel.commons/src/test/resources/org/exoplatform/commons/utils/resources.properties 2010-08-26
15:30:31 UTC (rev 2995)
+++
kernel/trunk/exo.kernel.commons/src/test/resources/org/exoplatform/commons/utils/resources.properties 2010-08-27
07:03:02 UTC (rev 2996)
@@ -1,3 +1,14 @@
key1=attention
key2=#{key1} and #{key3} alors
-key3=#{key1} please
\ No newline at end of file
+key3=#{key1} please
+key4=#{non-existing}
+key5=#{key5} StackOverflowError
+key6=#{key7}
+key7=#{key6}
+key8=some #{key9} #{key10}
+key9=other #{key10}
+key10=value
+key11=X
+key12=-#{key11}-
+key13=-#{key11}-
+key14=-#{key13}-
\ No newline at end of file