Revision: 563
http://svn.sourceforge.net/stripes/?rev=563&view=rev
Author: bengunter
Date: 2007-05-29 14:35:56 -0700 (Tue, 29 May 2007)
Log Message:
-----------
When UrlBuilder creates a URL, it now attempts to format each parameter with an
appropriate Formatter before appending it to the URL. This required
UrlBuilder's constructor to accept a locale, which trickled down to all the
classes that use UrlBuilder. The end result is that parameter formatting is now
supported by stripes:link, stripes:url, RedirectResolution, and
ForwardResolution (via OnwardResolution).
See STS-374 and STS-322
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/action/ForwardResolution.java
trunk/stripes/src/net/sourceforge/stripes/action/OnwardResolution.java
trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
trunk/stripes/src/net/sourceforge/stripes/tag/LinkTagSupport.java
trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java
trunk/tests/src/net/sourceforge/stripes/util/UrlBuilderTest.java
Property Changed:
----------------
trunk/
Property changes on: trunk
___________________________________________________________________
Name: svn:ignore
+ .classpath
.project
.settings
Modified:
trunk/stripes/src/net/sourceforge/stripes/action/ForwardResolution.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/action/ForwardResolution.java
2007-05-29 20:47:35 UTC (rev 562)
+++ trunk/stripes/src/net/sourceforge/stripes/action/ForwardResolution.java
2007-05-29 21:35:56 UTC (rev 563)
@@ -92,7 +92,7 @@
public void execute(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
- String path = getUrl();
+ String path = getUrl(request.getLocale());
// Figure out if we're inside an include, and use an include instead
of a forward
if (autoInclude &&
request.getAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH) != null) {
Modified: trunk/stripes/src/net/sourceforge/stripes/action/OnwardResolution.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/action/OnwardResolution.java
2007-05-29 20:47:35 UTC (rev 562)
+++ trunk/stripes/src/net/sourceforge/stripes/action/OnwardResolution.java
2007-05-29 21:35:56 UTC (rev 563)
@@ -14,12 +14,14 @@
*/
package net.sourceforge.stripes.action;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
import net.sourceforge.stripes.controller.StripesFilter;
+import net.sourceforge.stripes.format.Formatter;
import net.sourceforge.stripes.util.UrlBuilder;
-import java.util.HashMap;
-import java.util.Map;
-
/**
* <p>Abstract class that provides a consistent API for all Resolutions that
send the user onward to
* another view - either by forwarding, redirecting or some other mechanism.
Provides methods
@@ -148,11 +150,25 @@
}
/**
+ * Constructs the URL for the resolution by taking the path and
appending
+ * any parameters supplied.
+ *
+ * @deprecated As of Stripes 1.5, this method has been replaced by
+ * [EMAIL PROTECTED] #getUrl(Locale)}.
+ */
+ @Deprecated
+ public String getUrl() {
+ return getUrl(Locale.getDefault());
+ }
+
+ /**
* Constructs the URL for the resolution by taking the path and appending
any parameters
* supplied.
+ *
+ * @param locale the locale to be used by [EMAIL PROTECTED] Formatter}s
when formatting parameters
*/
- public String getUrl() {
- UrlBuilder builder = new UrlBuilder(path, false);
+ public String getUrl(Locale locale) {
+ UrlBuilder builder = new UrlBuilder(locale, path, false);
builder.addParameters(this.parameters);
return builder.toString();
}
Modified:
trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
2007-05-29 20:47:35 UTC (rev 562)
+++ trunk/stripes/src/net/sourceforge/stripes/action/RedirectResolution.java
2007-05-29 21:35:56 UTC (rev 563)
@@ -153,7 +153,7 @@
}
// Prepend the context path if required
- String url = getUrl();
+ String url = getUrl(request.getLocale());
if ( this.prependContext && !"/".equals(request.getContextPath()) ) {
url = request.getContextPath() + url;
}
Modified: trunk/stripes/src/net/sourceforge/stripes/tag/LinkTagSupport.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/tag/LinkTagSupport.java
2007-05-29 20:47:35 UTC (rev 562)
+++ trunk/stripes/src/net/sourceforge/stripes/tag/LinkTagSupport.java
2007-05-29 21:35:56 UTC (rev 563)
@@ -14,22 +14,15 @@
*/
package net.sourceforge.stripes.tag;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
+import net.sourceforge.stripes.exception.StripesJspException;
+import net.sourceforge.stripes.util.UrlBuilder;
+import net.sourceforge.stripes.controller.StripesConstants;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import java.util.Map;
+import java.util.HashMap;
-import net.sourceforge.stripes.controller.StripesConstants;
-import net.sourceforge.stripes.controller.StripesFilter;
-import net.sourceforge.stripes.exception.StripesJspException;
-import net.sourceforge.stripes.format.Formatter;
-import net.sourceforge.stripes.format.FormatterFactory;
-import net.sourceforge.stripes.util.CollectionUtil;
-import net.sourceforge.stripes.util.UrlBuilder;
-
/**
* Abstract support class for generating links. Used by both the LinkTag
(which generates
* regular [EMAIL PROTECTED] <a href=""/>} style links) and the UrlTag which
is a rough similie
@@ -171,63 +164,15 @@
// Add all the parameters and reset the href attribute; pass to false
here because
// the HtmlTagSupport will HtmlEncode the ampersands for us
- UrlBuilder builder = new UrlBuilder(base, false);
+ UrlBuilder builder = new
UrlBuilder(pageContext.getRequest().getLocale(), base, false);
if (this.event != null) {
builder.addParameter(this.event);
}
if (addSourcePage) {
builder.addParameter(StripesConstants.URL_KEY_SOURCE_PAGE,
request.getServletPath());
}
+ builder.addParameters(this.parameters);
- // format parameters before appending to URL
- Map<String, Object> parameters = new HashMap<String,
Object>(this.parameters);
- for (Entry<String, Object> entry : parameters.entrySet()) {
- Object value = entry.getValue();
- if (value != null && value.getClass().isArray()) {
- Object[] raw = CollectionUtil.asObjectArray(value);
- String[] formatted = new String[raw.length];
- for (int i = 0; i < raw.length; i++)
- formatted[i] = format(raw[i]);
- value = formatted;
- }
- else if (value instanceof Collection) {
- String[] formatted = new String[((Collection) value).size()];
- int index = 0;
- for (Object o : ((Collection) value))
- formatted[index++] = format(o);
- value = formatted;
- }
- else {
- value = format(value);
- }
- entry.setValue(value);
- }
- builder.addParameters(parameters);
-
return response.encodeURL(builder.toString());
}
-
- /**
- * Attempts to format an object using an appropriate [EMAIL PROTECTED]
Formatter}. If
- * no formatter is available for the object, then this method will call
- * <code>toString()</code> on the object. A null <code>value</code> will
- * be formatted as an empty string.
- *
- * @param value
- * the object to be formatted
- * @return the formatted value
- */
- @SuppressWarnings("unchecked")
- protected String format(Object value) {
- if (value == null)
- return "";
-
- FormatterFactory factory =
StripesFilter.getConfiguration().getFormatterFactory();
- Formatter formatter = factory.getFormatter(value.getClass(),
- getPageContext().getRequest().getLocale(), null, null);
- if (formatter == null)
- return String.valueOf(value);
- else
- return formatter.format(value);
- }
}
Modified: trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java
2007-05-29 20:47:35 UTC (rev 562)
+++ trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java
2007-05-29 21:35:56 UTC (rev 563)
@@ -14,13 +14,17 @@
*/
package net.sourceforge.stripes.util;
-import net.sourceforge.stripes.exception.StripesRuntimeException;
-
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
+import java.util.Locale;
import java.util.Map;
+import net.sourceforge.stripes.controller.StripesFilter;
+import net.sourceforge.stripes.exception.StripesRuntimeException;
+import net.sourceforge.stripes.format.Formatter;
+import net.sourceforge.stripes.format.FormatterFactory;
+
/**
* <p>Simple class that encapsulates the process of building up a URL from a
path fragment
* and a zero or more parameters. Parameters can be single valued, array
valued or
@@ -36,6 +40,7 @@
* @since Stripes 1.1.2
*/
public class UrlBuilder {
+ private Locale locale;
private StringBuilder url = new StringBuilder(256);
boolean seenQuestionMark = false;
private String parameterSeparator;
@@ -50,8 +55,27 @@
* @param url the path part of the URL
* @param isForPage true if the URL is to be embedded in a page (e.g. in
an anchor of img
* tag), false if for some other purpose.
+ * @deprecated As of Stripes 1.5, this constructor has been replaced by
+ * [EMAIL PROTECTED] #UrlBuilder(Locale, String, boolean)}.
+ */
+ @Deprecated
+ public UrlBuilder(String url, boolean isForPage) {
+ this(Locale.getDefault(), url, isForPage);
+ }
+
+ /**
+ * Constructs a UrlBuilder with the path to a resource. Parameters can be
added
+ * later using addParameter(). If the link is to be used in a page then
the ampersand
+ * character usually used to separate parameters will be escaped using the
XML entity
+ * for ampersand.
+ *
+ * @param locale the locale to use when formatting parameters with a
[EMAIL PROTECTED] Formatter}
+ * @param url the path part of the URL
+ * @param isForPage true if the URL is to be embedded in a page (e.g. in
an anchor of img
+ * tag), false if for some other purpose.
*/
- public UrlBuilder(String url, boolean isForPage) {
+ public UrlBuilder(Locale locale, String url, boolean isForPage) {
+ this.locale = locale;
if (url != null) {
// Check to see if there is an embedded anchor, and strip it out
for later
int index = url.indexOf('#');
@@ -129,7 +153,7 @@
this.url.append(name);
this.url.append('=');
if (v != null) {
- this.url.append( URLEncoder.encode(v.toString(),
"UTF-8") );
+ this.url.append( URLEncoder.encode(format(v), "UTF-8")
);
}
}
}
@@ -160,8 +184,7 @@
addParameter(name,
CollectionUtil.asObjectArray(valueOrValues));
}
else if (valueOrValues instanceof Collection) {
- Collection values = (Collection) valueOrValues;
- addParameter(name, values);
+ addParameter(name, (Collection) valueOrValues);
}
else {
addParameter(name, valueOrValues);
@@ -207,4 +230,29 @@
return this.url.toString();
}
}
+
+ /**
+ * Attempts to format an object using an appropriate [EMAIL PROTECTED]
Formatter}. If
+ * no formatter is available for the object, then this method will call
+ * <code>toString()</code> on the object. A null <code>value</code> will
+ * be formatted as an empty string.
+ *
+ * @param value
+ * the object to be formatted
+ * @return the formatted value
+ */
+ @SuppressWarnings("unchecked")
+ protected String format(Object value) {
+ if (value == null) {
+ return "";
+ }
+ else {
+ FormatterFactory factory =
StripesFilter.getConfiguration().getFormatterFactory();
+ Formatter formatter =
factory.getFormatter(value.getClass(), locale, null, null);
+ if (formatter == null)
+ return value.toString();
+ else
+ return formatter.format(value);
+ }
+ }
}
Modified: trunk/tests/src/net/sourceforge/stripes/util/UrlBuilderTest.java
===================================================================
--- trunk/tests/src/net/sourceforge/stripes/util/UrlBuilderTest.java
2007-05-29 20:47:35 UTC (rev 562)
+++ trunk/tests/src/net/sourceforge/stripes/util/UrlBuilderTest.java
2007-05-29 21:35:56 UTC (rev 563)
@@ -16,6 +16,7 @@
package net.sourceforge.stripes.util;
import java.util.LinkedHashMap;
+import java.util.Locale;
import java.util.Map;
import org.testng.Assert;
@@ -31,7 +32,7 @@
@Test(groups="fast")
public void testBasicUrl() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
String result = builder.toString();
Assert.assertEquals(result, path);
}
@@ -39,7 +40,7 @@
@Test(groups="fast")
public void testUrlWithParameters() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", 1);
builder.addParameter("two", 2);
builder.addParameter("three", 3);
@@ -50,7 +51,7 @@
@Test(groups="fast")
public void testUrlWithParametersAndAnchor() throws Exception {
String path = "/test/page.jsp#someAnchor";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", 1);
builder.addParameter("two", 2);
builder.addParameter("three", 3);
@@ -61,7 +62,7 @@
@Test(groups="fast")
public void testUrlWithParametersAndAnchor2() throws Exception {
String path = "/test/page.jsp#someAnchor";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", 1);
builder.addParameter("two", 2);
builder.addParameter("three", 3);
@@ -73,7 +74,7 @@
@Test(groups="fast")
public void testUrlWithParametersAndAnchor3() throws Exception {
String path = "/test/page.jsp#someAnchor";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", 1);
builder.addParameter("two", 2);
builder.addParameter("three", 3);
@@ -85,7 +86,7 @@
@Test(groups="fast")
public void testUrlWithParameterVarargs() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", 1, "one", "uno", "i");
String result = builder.toString();
Assert.assertEquals(result,
"/test/page.jsp?one=1&one=one&one=uno&one=i");
@@ -94,7 +95,7 @@
@Test(groups="fast")
public void testUrlWithValuelessParameter() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one");
builder.addParameter("two", "");
builder.addParameter("three");
@@ -105,7 +106,7 @@
@Test(groups="fast")
public void testUrlWithParameterArray() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", (Object[]) Literal.array("1", "one",
"uno", "i") );
String result = builder.toString();
Assert.assertEquals(result,
"/test/page.jsp?one=1&one=one&one=uno&one=i");
@@ -114,7 +115,7 @@
@Test(groups="fast")
public void testUrlWithParameterCollection() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", Literal.list("1", "one", "uno", "i") );
String result = builder.toString();
@@ -124,7 +125,7 @@
@Test(groups="fast")
public void testUrlWithParameterMap() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
Map<Object,Object> map = new LinkedHashMap<Object,Object>();
map.put("one", "one");
map.put("two", Literal.list("2", "two"));
@@ -139,7 +140,7 @@
@Test(groups="fast")
public void testUrlWithRepeatedParameters() throws Exception {
String path = "/test/page.jsp";
- UrlBuilder builder = new UrlBuilder(path, false);
+ UrlBuilder builder = new UrlBuilder(Locale.getDefault(), path, false);
builder.addParameter("one", "one");
builder.addParameter("one", "two", "three");
builder.addParameter("one", "four");
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