[seam-commits] Seam SVN: r7336 - in branches/Seam_2_0/src: test/unit/org/jboss/seam/test/unit and 1 other directory.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Fri Feb 1 19:33:16 EST 2008
Author: norman.richards at jboss.com
Date: 2008-02-01 19:33:16 -0500 (Fri, 01 Feb 2008)
New Revision: 7336
Modified:
branches/Seam_2_0/src/main/org/jboss/seam/core/Interpolator.java
branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java
Log:
JBSEAM-2566
Modified: branches/Seam_2_0/src/main/org/jboss/seam/core/Interpolator.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/core/Interpolator.java 2008-02-02 00:31:44 UTC (rev 7335)
+++ branches/Seam_2_0/src/main/org/jboss/seam/core/Interpolator.java 2008-02-02 00:33:16 UTC (rev 7336)
@@ -32,12 +32,9 @@
public static Interpolator instance()
{
- if ( Contexts.isApplicationContextActive() )
- {
+ if (Contexts.isApplicationContextActive()) {
return (Interpolator) Component.getInstance(Interpolator.class, ScopeType.APPLICATION);
- }
- else
- {
+ } else {
return new Interpolator(); //for unit testing
}
}
@@ -51,18 +48,15 @@
*/
public String interpolate(String string, Object... params)
{
- if (params == null)
- {
+ if (params == null) {
params = new Object[0];
}
- if ( params.length>10 )
- {
+ if (params.length>10) {
throw new IllegalArgumentException("more than 10 parameters");
}
- if (string.indexOf('#')>=0 || string.indexOf('{')>=0)
- {
+ if (string.indexOf('#')>=0 || string.indexOf('{')>=0) {
string = interpolateExpressions(string, params);
}
@@ -74,75 +68,63 @@
StringTokenizer tokens = new StringTokenizer(string, "#{}", true);
StringBuilder builder = new StringBuilder(string.length());
try {
- while ( tokens.hasMoreTokens() )
- {
+ while (tokens.hasMoreTokens()) {
String tok = tokens.nextToken();
- if ( "#".equals(tok) && tokens.hasMoreTokens() )
- {
+
+ if ("#".equals(tok) && tokens.hasMoreTokens()) {
String nextTok = tokens.nextToken();
- if ( "{".equals(nextTok) )
- {
+
+ while (nextTok.equals("#") && tokens.hasMoreTokens()) {
+ builder.append(tok);
+ nextTok = tokens.nextToken();
+ }
+
+ if ("{".equals(nextTok)) {
String expression = "#{" + tokens.nextToken() + "}";
- try
- {
+ try {
Object value = Expressions.instance().createValueExpression(expression).getValue();
if (value!=null) builder.append(value);
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
log.warn("exception interpolating string: " + string, e);
}
- tokens.nextToken(); //the }
- }
- else
- {
+ tokens.nextToken(); // the trailing "}"
+
+ } else if (nextTok.equals("#")) {
+ // could be trailing #
+ builder.append("#");
+
+ } else {
int index;
- try
- {
- index = Integer.parseInt( nextTok.substring(0, 1) );
- if (index>=params.length)
- {
+ try {
+ index = Integer.parseInt(nextTok.substring(0, 1));
+ if (index>=params.length) {
//log.warn("parameter index out of bounds: " + index + " in: " + string);
builder.append("#").append(nextTok);
+ } else {
+ builder.append(params[index]).append(nextTok.substring(1));
}
- else
- {
- builder.append( params[index] ).append( nextTok.substring(1) );
- }
- }
- catch (NumberFormatException nfe)
- {
+ } catch (NumberFormatException nfe) {
builder.append("#").append(nextTok);
}
}
- }
- else if ("{".equals(tok))
- {
+ } else if ("{".equals(tok)) {
StringBuilder expr = new StringBuilder();
expr.append(tok);
int level = 1;
- while (tokens.hasMoreTokens())
- {
+ while (tokens.hasMoreTokens()) {
String nextTok = tokens.nextToken();
expr.append(nextTok);
- if (nextTok.equals("{"))
- {
+ if (nextTok.equals("{")) {
++level;
- }
- else if (nextTok.equals("}"))
- {
- if (--level == 0)
- {
- try
- {
+ } else if (nextTok.equals("}")) {
+ if (--level == 0) {
+ try {
String value = new MessageFormat(expr.toString(), Locale.instance()).format(params);
builder.append(value);
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
// if it is a bad message, use the expression itself
builder.append(expr);
}
@@ -152,19 +134,17 @@
}
}
- if (expr != null)
- {
+ if (expr != null) {
builder.append(expr);
}
- }
- else
- {
+ } else {
builder.append(tok);
}
}
} catch (Exception e) {
log.warn("exception interpolating string: " + string, e);
}
+
return builder.toString();
}
Modified: branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java
===================================================================
--- branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java 2008-02-02 00:31:44 UTC (rev 7335)
+++ branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterpolatorTest.java 2008-02-02 00:33:16 UTC (rev 7336)
@@ -27,6 +27,11 @@
Assert.assertEquals("There are no files.", interpolator.interpolate(CHOICE_EXPR, 0));
Assert.assertEquals("There is one file.", interpolator.interpolate(CHOICE_EXPR, 1));
Assert.assertEquals("There are 2 files.", interpolator.interpolate(CHOICE_EXPR, 2));
+
+ // test sequences of multiple #
+ Assert.assertEquals("2", interpolator.interpolate("#0",2));
+ Assert.assertEquals("#2", interpolator.interpolate("##0",2));
+ Assert.assertEquals("##2", interpolator.interpolate("###0",2));
Date date = new Date(0);
More information about the seam-commits
mailing list