Revision: 552
http://svn.sourceforge.net/stripes/?rev=552&view=rev
Author: bengunter
Date: 2007-05-25 16:21:12 -0700 (Fri, 25 May 2007)
Log Message:
-----------
Resolved STS-365: add DefaultFormatterFactory.add(...) similar to
DefaultTypeConverterFactory.add(...). DefaultFormatterFactory is now very
similar to DefaultTypeConverterFactory. Now subclasses need only override
init(...) and call add(...) to register new formatters.
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
Modified:
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
2007-05-25 22:25:45 UTC (rev 551)
+++
trunk/stripes/src/net/sourceforge/stripes/format/DefaultFormatterFactory.java
2007-05-25 23:21:12 UTC (rev 552)
@@ -14,11 +14,14 @@
*/
package net.sourceforge.stripes.format;
-import net.sourceforge.stripes.config.Configuration;
-
import java.util.Date;
+import java.util.HashMap;
import java.util.Locale;
+import java.util.Map;
+import net.sourceforge.stripes.config.Configuration;
+import net.sourceforge.stripes.util.Log;
+
/**
* Very simple default implementation of a formatter factory that is aware of
how to format
* dates, numbers and enums.
@@ -26,6 +29,11 @@
* @author Tim Fennell
*/
public class DefaultFormatterFactory implements FormatterFactory {
+ /** A rather generic-heavy Map that maps target type to Formatter. */
+ private Map<Class, Class<? extends Formatter>> formatters =
+ new HashMap<Class, Class<? extends Formatter>>();
+
+ /** Stores a reference to the Configuration passed in at initialization
time. */
private Configuration configuration;
/** Stores a reference to the configuration and returns. */
@@ -39,6 +47,27 @@
}
/**
+ * Gets the (rather confusing) Map of Formatter objects. The Map uses the
target class
+ * as the key in the Map, and the Class object representing the Formatter
as the value.
+ *
+ * @return the Map of Formatter classes
+ */
+ protected Map<Class,Class<? extends Formatter>> getFormatters() {
+ return this.formatters;
+ }
+
+ /**
+ * Adds a Formatter to the set of registered Formatters, overriding an
existing
+ * formatter if one was registered for the type.
+ *
+ * @param targetType the type for which the formatter will handle
formatting
+ * @param formatterClass the implementation class that will handle the
formatting
+ */
+ protected void add(Class targetType, Class<? extends Formatter>
formatterClass) {
+ this.formatters.put(targetType, formatterClass);
+ }
+
+ /**
* Does a simple check to see if the clazz specified is equal to or a
subclass of
* java.util.Date or java.lang.Number, and if so, creates a formatter
instance. Otherwise
* returns null.
@@ -53,7 +82,17 @@
Formatter formatter = null;
// Figure out if we have a type we can format
- if (Date.class.isAssignableFrom(clazz)) {
+ if (formatters.containsKey(clazz)) {
+ Class<? extends Formatter> formatterClass = formatters.get(clazz);
+ try {
+ return getInstance(formatterClass, formatType, formatPattern,
locale);
+ }
+ catch (Exception e) {
+ Log.getInstance(getClass()).error(e, "Unable to instantiate
Formatter ", formatterClass);
+ return null;
+ }
+ }
+ else if (Date.class.isAssignableFrom(clazz)) {
formatter = new DateFormatter();
formatter.setFormatType(formatType);
formatter.setFormatPattern(formatPattern);
@@ -78,4 +117,23 @@
return null;
}
}
+
+ /**
+ * Gets an instance of the Formatter class specified.
+ *
+ * @param clazz the Formatter type that is desired
+ * @return an instance of the Formatter specified
+ * @throws Exception if there is a problem instantiating the Formatter
+ */
+ public Formatter getInstance(Class<? extends Formatter> clazz,
+ String formatType, String formatPattern, Locale locale)
+ throws Exception {
+ // TODO: add thread local caching of formatter classes
+ Formatter formatter = clazz.newInstance();
+ formatter.setFormatType(formatType);
+ formatter.setFormatPattern(formatPattern);
+ formatter.setLocale(locale);
+ formatter.init();
+ return formatter;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development