[seam-commits] Seam SVN: r10665 - modules/trunk/el/src/main/java/org/jboss/seam/el.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Mon Apr 27 22:45:45 EDT 2009
Author: shane.bryzak at jboss.com
Date: 2009-04-27 22:45:45 -0400 (Mon, 27 Apr 2009)
New Revision: 10665
Added:
modules/trunk/el/src/main/java/org/jboss/seam/el/Interpolator.java
Log:
add Interpolator to el module
Added: modules/trunk/el/src/main/java/org/jboss/seam/el/Interpolator.java
===================================================================
--- modules/trunk/el/src/main/java/org/jboss/seam/el/Interpolator.java (rev 0)
+++ modules/trunk/el/src/main/java/org/jboss/seam/el/Interpolator.java 2009-04-28 02:45:45 UTC (rev 10665)
@@ -0,0 +1,171 @@
+package org.jboss.seam.el;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.StringTokenizer;
+
+import javax.context.Dependent;
+import javax.inject.Current;
+
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
+
+/**
+ * Interpolates EL expressions in Strings
+ *
+ * @author Gavin King
+ */
+ at Dependent
+public class Interpolator
+{
+ private static final LogProvider log = Logging.getLogProvider(Interpolator.class);
+
+ @Current Expressions expressions;
+ @Current Locale locale;
+
+ /**
+ * Replace all EL expressions in the form #{...} with their evaluated values.
+ *
+ * @param string
+ * a template
+ * @return the interpolated string
+ */
+ public String interpolate(String string, Object... params)
+ {
+ if (params == null)
+ {
+ params = new Object[0];
+ }
+
+ if (params.length > 10)
+ {
+ throw new IllegalArgumentException("more than 10 parameters");
+ }
+
+ if (string.indexOf('#') >= 0 || string.indexOf('{') >= 0)
+ {
+ string = interpolateExpressions(string, params);
+ }
+
+ return string;
+ }
+
+ private String interpolateExpressions(String string, Object... params)
+ {
+ StringTokenizer tokens = new StringTokenizer(string, "#{}", true);
+ StringBuilder builder = new StringBuilder(string.length());
+ try
+ {
+ while (tokens.hasMoreTokens())
+ {
+ String tok = tokens.nextToken();
+
+ if ("#".equals(tok) && tokens.hasMoreTokens())
+ {
+ String nextTok = tokens.nextToken();
+
+ while (nextTok.equals("#") && tokens.hasMoreTokens())
+ {
+ builder.append(tok);
+ nextTok = tokens.nextToken();
+ }
+
+ if ("{".equals(nextTok))
+ {
+ String expression = "#{" + tokens.nextToken() + "}";
+ try
+ {
+ Object value = expressions.createValueExpression(expression).getValue();
+ if (value != null)
+ builder.append(value);
+ } catch (Exception e)
+ {
+ log.warn("exception interpolating string: " + string, e);
+ }
+ 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)
+ {
+ // log.warn("parameter index out of bounds: " + index +
+ // " in: " + string);
+ builder.append("#").append(nextTok);
+ } else
+ {
+ builder.append(params[index]).append(
+ nextTok.substring(1));
+ }
+ } catch (NumberFormatException nfe)
+ {
+ builder.append("#").append(nextTok);
+ }
+ }
+ } else if ("{".equals(tok))
+ {
+ StringBuilder expr = new StringBuilder();
+
+ expr.append(tok);
+ int level = 1;
+
+ while (tokens.hasMoreTokens())
+ {
+ String nextTok = tokens.nextToken();
+ expr.append(nextTok);
+
+ if (nextTok.equals("{"))
+ {
+ ++level;
+ } else if (nextTok.equals("}"))
+ {
+ if (--level == 0)
+ {
+ try
+ {
+ if (params.length == 0)
+ {
+ builder.append(expr.toString());
+ } else
+ {
+ String value = new MessageFormat(expr.toString(),
+ locale).format(params);
+ builder.append(value);
+ }
+ } catch (Exception e)
+ {
+ // if it is a bad message, use the expression itself
+ builder.append(expr);
+ }
+ expr = null;
+ break;
+ }
+ }
+ }
+
+ if (expr != null)
+ {
+ builder.append(expr);
+ }
+ } else
+ {
+ builder.append(tok);
+ }
+ }
+ } catch (Exception e)
+ {
+ log.warn("exception interpolating string: " + string, e);
+ }
+
+ return builder.toString();
+ }
+
+}
More information about the seam-commits
mailing list