Author: etnu
Date: Thu Jan 29 04:58:56 2009
New Revision: 738760
URL: http://svn.apache.org/viewvc?rev=738760&view=rev
Log:
Fixed a bug in variable substitution that was causing incorrect behavior for
patterns with underscores that did not begin a valid substitution.
Example:
var foo = bar + "__" baz;
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/SubstitutionsTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java?rev=738760&r1=738759&r2=738760&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/variables/Substitutions.java
Thu Jan 29 04:58:56 2009
@@ -83,14 +83,14 @@
* @param value
*/
public void addSubstitution(Type type, String key, String value) {
- substitutions.put(type.prefix + key + "__", value);
+ substitutions.put(type.prefix + key, value);
}
/**
* @return The value stored for the given type and key, or null.
*/
public String getSubstitution(Type type, String key) {
- return substitutions.get(type.prefix + key + "__");
+ return substitutions.get(type.prefix + key);
}
/**
@@ -115,14 +115,14 @@
}
output.append(input.substring(lastPosition, i));
- lastPosition = next + 2;
- String pattern = input.substring(i, lastPosition);
+ String pattern = input.substring(i, next);
boolean isMessage = pattern.startsWith(Type.MESSAGE.prefix);
String replacement;
+
if (isMessage && isNested) {
- replacement = pattern;
+ replacement = pattern + "__";
} else {
replacement = substitutions.get(pattern);
}
@@ -130,11 +130,15 @@
if (replacement == null) {
// Keep it.
output.append(pattern);
- } else if (isMessage && !isNested) {
- // Messages can get recursive
- performSubstitutions(replacement, output, true);
+ lastPosition = next;
} else {
- output.append(replacement);
+ lastPosition = next + 2;
+ if (isMessage && !isNested) {
+ // Messages can be recursive
+ performSubstitutions(replacement, output, true);
+ } else {
+ output.append(replacement);
+ }
}
}
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/SubstitutionsTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/SubstitutionsTest.java?rev=738760&r1=738759&r2=738760&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/SubstitutionsTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/variables/SubstitutionsTest.java
Thu Jan 29 04:58:56 2009
@@ -20,10 +20,10 @@
import org.apache.shindig.gadgets.variables.Substitutions.Type;
-import junit.framework.TestCase;
-
import org.apache.commons.lang.StringUtils;
+import junit.framework.TestCase;
+
public class SubstitutionsTest extends TestCase {
private Substitutions subst;
@@ -75,6 +75,22 @@
subst.substituteString(msg));
}
+ public void testDanglingUnderScoresAreIgnored() throws Exception {
+ String msg = "__MSG_hello__, var_msg + '__' + 'world __MSG_world__";
+ subst.addSubstitution(Type.MESSAGE, "hello", "Hello");
+ subst.addSubstitution(Type.MESSAGE, "world", "World");
+
+ assertEquals("Hello, var_msg + '__' + 'world World",
subst.substituteString(msg));
+ }
+
+ public void testComplexUnderscores() throws Exception {
+ String msg = "__MSG_hello____________ten____________MSG_world______";
+ subst.addSubstitution(Type.MESSAGE, "hello", "Hello");
+ subst.addSubstitution(Type.MESSAGE, "world", "World");
+
+ assertEquals("Hello__________ten__________World____",
subst.substituteString(msg));
+ }
+
public void loadTest() throws Exception {
String msg
= "Random text and __UP_hello__, amongst other words __MSG_world__
stuff __weeeeee";