You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2010/02/22 16:40:17 UTC
svn commit: r912625 -
/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
Author: doogie
Date: Mon Feb 22 15:40:16 2010
New Revision: 912625
URL: http://svn.apache.org/viewvc?rev=912625&view=rev
Log:
Add expand() variants, that don't cast the result to a string, instead
just returning the raw value directly.
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java?rev=912625&r1=912624&r2=912625&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java Mon Feb 22 15:40:16 2010
@@ -328,7 +328,35 @@
* @param timeZone The time zone to be used for localization
* @param locale The locale to be used for localization
*/
- protected abstract void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale);
+ protected abstract Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale);
+
+ private static Locale getLocale(Locale locale, Map<String, ? extends Object> context) {
+ if (locale == null) {
+ locale = (Locale) context.get("locale");
+ if (locale == null && context.containsKey("autoUserLogin")) {
+ Map<String, Object> autoUserLogin = UtilGenerics.cast(context.get("autoUserLogin"));
+ locale = UtilMisc.ensureLocale(autoUserLogin.get("lastLocale"));
+ }
+ if (locale == null) {
+ locale = Locale.getDefault();
+ }
+ }
+ return locale;
+ }
+
+ private static TimeZone getTimeZone(TimeZone timeZone, Map<String, ? extends Object> context) {
+ if (timeZone == null) {
+ timeZone = (TimeZone) context.get("timeZone");
+ if (timeZone == null && context.containsKey("autoUserLogin")) {
+ Map<String, String> autoUserLogin = UtilGenerics.cast(context.get("autoUserLogin"));
+ timeZone = UtilDateTime.toTimeZone(autoUserLogin.get("lastTimeZone"));
+ }
+ if (timeZone == null) {
+ timeZone = TimeZone.getDefault();
+ }
+ }
+ return timeZone;
+ }
/** Evaluate this object's expression and return the result as a <code>String</code>.
* Null or empty expressions return an empty <code>String</code>.
@@ -366,28 +394,17 @@
if (context == null) {
return this.toString();
}
- if (locale == null) {
- locale = (Locale) context.get("locale");
- if (locale == null && context.containsKey("autoUserLogin")) {
- Map<String, Object> autoUserLogin = UtilGenerics.cast(context.get("autoUserLogin"));
- locale = UtilMisc.ensureLocale(autoUserLogin.get("lastLocale"));
- }
- if (locale == null) {
- locale = Locale.getDefault();
- }
- }
- if (timeZone == null) {
- timeZone = (TimeZone) context.get("timeZone");
- if (timeZone == null && context.containsKey("autoUserLogin")) {
- Map<String, String> autoUserLogin = UtilGenerics.cast(context.get("autoUserLogin"));
- timeZone = UtilDateTime.toTimeZone(autoUserLogin.get("lastTimeZone"));
- }
- if (timeZone == null) {
- timeZone = TimeZone.getDefault();
+ timeZone = getTimeZone(timeZone, context);
+ locale = getLocale(locale, context);
+ Object obj = get(context, timeZone, locale);
+ StringBuilder buffer = new StringBuilder(this.hint);
+ try {
+ if (obj != null) {
+ buffer.append(ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true));
}
+ } catch (Exception e) {
+ buffer.append(obj);
}
- StringBuilder buffer = new StringBuilder(this.hint);
- this.append(buffer, context, timeZone, locale);
if (buffer.length() > this.hint) {
synchronized(this) {
this.hint = buffer.length();
@@ -396,6 +413,45 @@
return buffer.toString();
}
+ /** Evaluate this object's expression and return the result as an <code>Object</code>.
+ * Null or empty expressions return an empty <code>String</code>.
+ * A <code>null context</code> argument will return the original expression.
+ *
+ * @param context The evaluation context
+ * @return This object's expression result as a <code>String</code>
+ */
+ public Object expand(Map<String, ? extends Object> context) {
+ return this.expand(context, null, null);
+ }
+
+ /** Evaluate this object's expression and return the result as an <code>Object</code>.
+ * Null or empty expressions return an empty <code>String</code>.
+ * A <code>null context</code> argument will return the original expression.
+ *
+ * @param context The evaluation context
+ * @param locale The locale to be used for localization
+ * @return This object's expression result as a <code>String</code>
+ */
+ public Object expand(Map<String, ? extends Object> context, Locale locale) {
+ return this.expand(context, null, locale);
+ }
+
+ /** Evaluate this object's expression and return the result as an <code>Object</code>.
+ * Null or empty expressions return an empty <code>String</code>.
+ * A <code>null context</code> argument will return the original expression.
+ *
+ * @param context The evaluation context
+ * @param timeZone The time zone to be used for localization
+ * @param locale The locale to be used for localization
+ * @return This object's expression result as a <code>String</code>
+ */
+ public Object expand(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ if (context == null) {
+ return null;
+ }
+ return get(context, getTimeZone(timeZone, context), getLocale(locale, context));
+ }
+
/** Returns a copy of the original expression.
*
* @return The original expression
@@ -453,15 +509,11 @@
}
@Override
- protected void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
try {
Object obj = BshUtil.eval(new String(this.chars, this.parseStart, this.parseLength), UtilMisc.makeMapWritable(context));
if (obj != null) {
- try {
- buffer.append(ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true));
- } catch (Exception e) {
- buffer.append(obj);
- }
+ return obj;
} else {
if (Debug.verboseOn()) {
Debug.logVerbose("BSH scriptlet evaluated to null [" + this + "], got no return so inserting nothing.", module);
@@ -470,6 +522,7 @@
} catch (EvalError e) {
Debug.logWarning(e, "Error evaluating BSH scriptlet [" + this + "], inserting nothing; error was: " + e, module);
}
+ return null;
}
}
@@ -478,21 +531,25 @@
protected ConstSimpleElem(char[] chars) {
super(chars);
}
+
@Override
public boolean isEmpty() {
return this.chars.length == 0;
}
+
@Override
public String getOriginal() {
return new String(this.chars);
}
+
@Override
- public void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
- buffer.append(this.chars);
+ public String expandString(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ return getOriginal();
}
+
@Override
- public String expandString(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
- return new String(this.chars);
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ return isEmpty() ? null : getOriginal();
}
}
@@ -503,8 +560,8 @@
}
@Override
- protected void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
- buffer.append(this.chars, this.offset, this.length);
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ return getOriginal();
}
@Override
@@ -528,12 +585,12 @@
}
@Override
- protected void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
try {
Object obj = UelUtil.evaluate(context, new String(this.valueStr));
if (obj != null) {
String currencyCode = this.codeExpr.expandString(context, timeZone, locale);
- buffer.append(UtilFormatOut.formatCurrency(new BigDecimal(obj.toString()), currencyCode, locale));
+ return UtilFormatOut.formatCurrency(new BigDecimal(obj.toString()), currencyCode, locale);
}
} catch (PropertyNotFoundException e) {
if (Debug.verboseOn()) {
@@ -542,6 +599,7 @@
} catch (Exception e) {
Debug.logError("Error evaluating expression: " + e, module);
}
+ return null;
}
}
@@ -555,10 +613,12 @@
}
@Override
- protected void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ StringBuilder buffer = new StringBuilder();
for (FlexibleStringExpander child : this.childElems) {
- child.append(buffer, context, timeZone, locale);
+ buffer.append(child.expandString(context, timeZone, locale));
}
+ return buffer.toString();
}
}
@@ -572,15 +632,11 @@
}
@Override
- protected void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
try {
Object obj = InvokerHelper.createScript(this.parsedScript, GroovyUtil.getBinding(context)).run();
if (obj != null) {
- try {
- buffer.append(ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true));
- } catch (Exception e) {
- buffer.append(obj);
- }
+ return obj;
} else {
if (Debug.verboseOn()) {
Debug.logVerbose("Groovy scriptlet evaluated to null [" + this + "], got no return so inserting nothing.", module);
@@ -590,6 +646,7 @@
// handle other things, like the groovy.lang.MissingPropertyException
Debug.logWarning(e, "Error evaluating Groovy scriptlet [" + this + "], inserting nothing; error was: " + e, module);
}
+ return null;
}
}
@@ -606,19 +663,16 @@
}
@Override
- protected void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
StringBuilder expr = new StringBuilder(this.hint);
for (FlexibleStringExpander child : this.childElems) {
- child.append(expr, context, timeZone, locale);
+ expr.append(child.expandString(context, timeZone, locale));
}
if (expr.length() == 0) {
- return;
+ return "";
}
try {
- Object obj = UelUtil.evaluate(context, openBracket.concat(expr.toString()).concat(closeBracket));
- if (obj != null) {
- buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, false));
- }
+ return UelUtil.evaluate(context, openBracket.concat(expr.toString()).concat(closeBracket));
} catch (PropertyNotFoundException e) {
if (Debug.verboseOn()) {
Debug.logVerbose("Error evaluating expression: " + e, module);
@@ -626,6 +680,7 @@
} catch (Exception e) {
Debug.logError("Error evaluating expression: " + e, module);
}
+ return "";
}
}
@@ -639,7 +694,7 @@
}
@Override
- protected void append(StringBuilder buffer, Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
+ protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
Object obj = null;
try {
obj = UelUtil.evaluate(context, new String(this.bracketedOriginal));
@@ -656,14 +711,11 @@
String str = (String) obj;
if (str.contains(openBracket)) {
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(str);
- fse.append(buffer, context, timeZone, locale);
- return;
+ return fse.get(context, timeZone, locale);
}
} catch (ClassCastException e) {}
- try {
- buffer.append((String) ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, false));
- } catch (Exception e) {}
}
+ return obj;
}
}
}