-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Oliver Heger schrieb:
> Tom Schindl wrote:
>
>> Stephen Colebourne schrieb:
>>
>> >Please prefix emails by [lang]
>
[...]
> An alternative would be to provide an additional implementation of the
> VariableResolver interface. The default implementation stays as is and
> does not handle formats. An extended implementation could support
> further arguments that are appended to variables.
>
> Oliver
so i did now and added a new resolver based upon the existing one. What
this patch does:
- - added a new Resolver named MapVariableResolverWithFormats
- - static functions to turn Formats on/off
=> default methods changed to use Formats
- - new constructors
Hope that's better than my first try. I didn't have a enough time to
look closer at how to make VariableFormatter extend Format which I'd
desire most so that the interface between MessageFormat and
VariableFormatter is equal.
>
> <snip/>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFC+I+YkVPeOFLgZFIRAlszAJsGCQ7fdUnKtsho4rEvxliptJMQTACeKv1d
U1wwIvsx++Bw6Rn65CD7yCg=
=5lYH
-----END PGP SIGNATURE-----
Index:
E:/eclipse-workspaces/beso-internal-devel/jakarta-lang/src/java/org/apache/commons/lang/text/VariableFormatter.java
===================================================================
---
E:/eclipse-workspaces/beso-internal-devel/jakarta-lang/src/java/org/apache/commons/lang/text/VariableFormatter.java
(revision 231018)
+++
E:/eclipse-workspaces/beso-internal-devel/jakarta-lang/src/java/org/apache/commons/lang/text/VariableFormatter.java
(working copy)
@@ -16,6 +16,7 @@
package org.apache.commons.lang.text;
+import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -175,6 +176,48 @@
Object resolveVariable(String varName);
}
+ /**
+ * <p>
+ * A VariableResolver backed by a [EMAIL PROTECTED] Map} who uses Formats
like MessageFormat does
+ * </p>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Tom Schindl</a>
+ * @version $Id$
+ */
+ public static class MapVariableResolverWithFormats extends
MapVariableResolver {
+
+ /**
+ * Creates a new variable resolver to handle formats like
+ * [EMAIL PROTECTED] MessageFormat} does it. You can use e.g.
{today,date,short} to
+ * format the variable today as a short date
+ *
+ * @param map
+ * The variable names and values.
+ */
+ public MapVariableResolverWithFormats(Map map) {
+ super(map);
+ }
+
+ /**
+ * Resolves the variable and formats the object retrieved using
+ * [EMAIL PROTECTED] MessageFormat}
+ *
+ * @param varName
+ * name of the variable and optionally the data-type and
+ * data-format seperated by comma
+ */
+ public Object resolveVariable(String varName) {
+ int index;
+
+ if (this.getMap() != null && (index = varName.indexOf(",")) !=
-1) {
+ return MessageFormat.format("{0" +
varName.substring(index)
+ + "}", new Object[] { this.getMap().get(
+ varName.substring(0,
index)) });
+ } else {
+ return super.resolveVariable(varName);
+ }
+ }
+ }
+
/** Constant for the default escape character. */
static final char DEFAULT_ESCAPE = '$';
@@ -185,37 +228,95 @@
static final String DEFAULT_SUFFIX = "}";
/**
- * Replaces the occurrences of all variables in the given source data by
their current values obtained from the
- * passed in map.
- *
- * @param valueMap
- * the map with the values
- * @param source
- * the source text
- * @return the result of the replace operation
- */
- public static String replace(Map valueMap, Object source) {
- return new VariableFormatter(valueMap).replace(source);
- }
+ * Replaces the occurrences of all variables in the given source data by
+ * their current values obtained from the passed in map. Formats are
applied
+ * automatically if you have defined them in your variable definition.
+ *
+ * @param valueMap
+ * the map with the values
+ * @param source
+ * the source text
+ * @return the result of the replace operation
+ */
+ public static String replace(Map valueMap, Object source) {
+ return replace(valueMap, source, true);
+ }
- /**
- * Replaces the occurrences of all variables in the given source data by
their current values obtained from the
- * passed in map. This method allows to specifiy a custom variable prefix
and suffix
- *
- * @param valueMap
- * the map with the values
- * @param prefix
- * the prefix of variables
- * @param suffix
- * the suffix of variables
- * @param source
- * the source text
- * @return the result of the replace operation
- */
- public static String replace(Map valueMap, String prefix, String suffix,
Object source) {
- return new VariableFormatter(valueMap, prefix, suffix).replace(source);
- }
+ /**
+ * Replaces the occurrences of all variables in the given source data by
+ * their current values obtained from the passed in map.
+ *
+ * @param valueMap
+ * the map with the values
+ * @param source
+ * the source text
+ * @param withFormats
+ * format the values replaced using format informations.
This may
+ * slow down the process of replacing a little bit so if you
+ * don't need it turn it of
+ * @return the result of the replace operation
+ */
+ public static String replace(Map valueMap, Object source,
+ boolean withFormats) {
+ if (withFormats) {
+ return new VariableFormatter(new
MapVariableResolverWithFormats(
+ valueMap)).replace(source);
+ } else {
+ return new VariableFormatter(valueMap).replace(source);
+ }
+ }
+ /**
+ * Replaces the occurrences of all variables in the given source data by
+ * their current values obtained from the passed in map. This method
allows
+ * to specifiy a custom variable prefix and suffix. Formats are applied
+ * automatically if you have defined them in your variable definition.
+ *
+ * @param valueMap
+ * the map with the values
+ * @param prefix
+ * the prefix of variables
+ * @param suffix
+ * the suffix of variables
+ * @param source
+ * the source text
+ * @return the result of the replace operation
+ */
+ public static String replace(Map valueMap, String prefix, String suffix,
+ Object source) {
+ return replace(valueMap, prefix, suffix, source, true);
+ }
+
+ /**
+ * Replaces the occurrences of all variables in the given source data by
+ * their current values obtained from the passed in map. This method
allows
+ * to specifiy a custom variable prefix and suffix
+ *
+ * @param valueMap
+ * the map with the values
+ * @param prefix
+ * the prefix of variables
+ * @param suffix
+ * the suffix of variables
+ * @param source
+ * the source text
+ * @param withFormats
+ * format the values replaced using format informations.
This may
+ * slow down the process of replacing a little bit so if you
+ * don't need it turn it of
+ * @return the result of the replace operation
+ */
+ public static String replace(Map valueMap, String prefix, String suffix,
+ Object source, boolean withFormats) {
+ if (withFormats) {
+ return new VariableFormatter(new
MapVariableResolverWithFormats(
+ valueMap), prefix,
suffix).replace(source);
+ } else {
+ return new VariableFormatter(valueMap, prefix, suffix)
+ .replace(source);
+ }
+ }
+
/**
* Replaces all variables in the given source data with values obtained
from system properties.
*
@@ -251,6 +352,17 @@
public VariableFormatter(Map valueMap) {
this(valueMap, DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
}
+
+ /**
+ * Creates a new instance and initializes it. Uses defaults for variable
+ * prefix and suffix and the escaping character.
+ *
+ * @param variableResolver
+ * the resolver used to map variable names
+ */
+ public VariableFormatter(VariableResolver variableResolver) {
+ this(variableResolver, DEFAULT_PREFIX, DEFAULT_SUFFIX,
DEFAULT_ESCAPE);
+ }
/**
* Creates a new instance with defaults for variable prefix and suffix and
the escaping character.
@@ -274,6 +386,22 @@
}
/**
+ * Creates a new instance and initializes it. Uses a default escaping
+ * character.
+ *
+ * @param variableResolver
+ * the resolver used to map variable names
+ * @param prefix
+ * the prefix for variables
+ * @param suffix
+ * the suffix for variables
+ */
+ public VariableFormatter(VariableResolver variableResolver, String
prefix,
+ String suffix) {
+ this(variableResolver, prefix, suffix, DEFAULT_ESCAPE);
+ }
+
+ /**
* Creates a new instance and initializes it.
*
* @param valueMap
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]