polishing

This commit is contained in:
Juergen Hoeller 2009-11-27 01:49:18 +00:00
parent d6197b743d
commit a1916ca765
5 changed files with 70 additions and 46 deletions

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.annotation; package org.springframework.format.annotation;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
@ -69,6 +70,7 @@ public @interface DateTimeFormat {
*/ */
String pattern() default ""; String pattern() default "";
/** /**
* Common ISO date time format patterns. * Common ISO date time format patterns.
* @author Keith Donald * @author Keith Donald
@ -96,6 +98,6 @@ public @interface DateTimeFormat {
* Indicates that no ISO-based format pattern should be applied. * Indicates that no ISO-based format pattern should be applied.
*/ */
NONE NONE
}
} }
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.annotation; package org.springframework.format.annotation;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
@ -54,6 +55,7 @@ public @interface NumberFormat {
*/ */
String pattern() default ""; String pattern() default "";
/** /**
* Common number format styles. * Common number format styles.
* @author Keith Donald * @author Keith Donald
@ -75,6 +77,6 @@ public @interface NumberFormat {
* The percent format for the current locale. * The percent format for the current locale.
*/ */
PERCENT PERCENT
}
} }
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.util.Calendar; import java.util.Calendar;
@ -29,6 +30,7 @@ import org.joda.time.LocalTime;
import org.joda.time.ReadableInstant; import org.joda.time.ReadableInstant;
import org.joda.time.ReadablePartial; import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Parser; import org.springframework.format.Parser;
import org.springframework.format.Printer; import org.springframework.format.Printer;
@ -37,7 +39,9 @@ import org.springframework.format.annotation.DateTimeFormat.ISO;
/** /**
* Formats fields annotated with the {@link DateTimeFormat} annotation. * Formats fields annotated with the {@link DateTimeFormat} annotation.
*
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
* @see DateTimeFormat * @see DateTimeFormat
*/ */
@ -45,6 +49,7 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
private final Set<Class<?>> fieldTypes; private final Set<Class<?>> fieldTypes;
public DateTimeFormatAnnotationFormatterFactory() { public DateTimeFormatAnnotationFormatterFactory() {
this.fieldTypes = Collections.unmodifiableSet(createFieldTypes()); this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
} }
@ -53,16 +58,20 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
return this.fieldTypes; return this.fieldTypes;
} }
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) { public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation); DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation);
if (ReadableInstant.class.isAssignableFrom(fieldType)) { if (ReadableInstant.class.isAssignableFrom(fieldType)) {
return new ReadableInstantPrinter(formatter); return new ReadableInstantPrinter(formatter);
} else if (ReadablePartial.class.isAssignableFrom(fieldType)) { }
else if (ReadablePartial.class.isAssignableFrom(fieldType)) {
return new ReadablePartialPrinter(formatter); return new ReadablePartialPrinter(formatter);
} else if (Calendar.class.isAssignableFrom(fieldType)) { }
else if (Calendar.class.isAssignableFrom(fieldType)) {
// assumes Calendar->ReadableInstant converter is registered // assumes Calendar->ReadableInstant converter is registered
return new ReadableInstantPrinter(formatter); return new ReadableInstantPrinter(formatter);
} else { }
else {
// assumes Date->Long converter is registered // assumes Date->Long converter is registered
return new MillisecondInstantPrinter(formatter); return new MillisecondInstantPrinter(formatter);
} }
@ -72,6 +81,7 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
return new DateTimeParser(configureDateTimeFormatterFrom(annotation)); return new DateTimeParser(configureDateTimeFormatterFrom(annotation));
} }
// internal helpers // internal helpers
private Set<Class<?>> createFieldTypes() { private Set<Class<?>> createFieldTypes() {
@ -90,9 +100,11 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
private DateTimeFormatter configureDateTimeFormatterFrom(DateTimeFormat annotation) { private DateTimeFormatter configureDateTimeFormatterFrom(DateTimeFormat annotation) {
if (!annotation.pattern().isEmpty()) { if (!annotation.pattern().isEmpty()) {
return forPattern(annotation.pattern()); return forPattern(annotation.pattern());
} else if (annotation.iso() != ISO.NONE) { }
return forISO(annotation.iso()); else if (annotation.iso() != ISO.NONE) {
} else { return forIso(annotation.iso());
}
else {
return forStyle(annotation.style()); return forStyle(annotation.style());
} }
} }
@ -101,12 +113,14 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
return org.joda.time.format.DateTimeFormat.forPattern(pattern); return org.joda.time.format.DateTimeFormat.forPattern(pattern);
} }
private DateTimeFormatter forISO(ISO iso) { private DateTimeFormatter forIso(ISO iso) {
if (iso == ISO.DATE) { if (iso == ISO.DATE) {
return org.joda.time.format.ISODateTimeFormat.date(); return org.joda.time.format.ISODateTimeFormat.date();
} else if (iso == ISO.TIME) { }
else if (iso == ISO.TIME) {
return org.joda.time.format.ISODateTimeFormat.time(); return org.joda.time.format.ISODateTimeFormat.time();
} else { }
else {
return org.joda.time.format.ISODateTimeFormat.dateTime(); return org.joda.time.format.ISODateTimeFormat.dateTime();
} }
} }

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.format.datetime.joda; package org.springframework.format.datetime.joda;
import java.util.Calendar; import java.util.Calendar;
@ -26,19 +27,22 @@ import org.joda.time.ReadableInstant;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat; import org.joda.time.format.ISODateTimeFormat;
import org.springframework.format.FormatterRegistry; import org.springframework.format.FormatterRegistry;
import org.springframework.format.Parser; import org.springframework.format.Parser;
import org.springframework.format.Printer; import org.springframework.format.Printer;
/** /**
* Configures Joda Time's Formatting system for use with Spring. * Configures Joda Time's Formatting system for use with Spring.
*
* @author Keith Donald * @author Keith Donald
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
* @see #setDateStyle(String) * @see #setDateStyle
* @see #setTimeStyle(String) * @see #setTimeStyle
* @see #setDateTimeStyle(String) * @see #setDateTimeStyle
* @see #setUseISOFormat(boolean) * @see #setUseIsoFormat
* @see #installJodaTimeFormatting(FormatterRegistry) * @see #installJodaTimeFormatting
*/ */
public class JodaTimeFormattingConfigurer { public class JodaTimeFormattingConfigurer {
@ -48,12 +52,12 @@ public class JodaTimeFormattingConfigurer {
private String dateTimeStyle; private String dateTimeStyle;
private boolean useISOFormat; private boolean useIsoFormat;
/** /**
* Set the default format style of Joda {@link LocalDate} objects. * Set the default format style of Joda {@link LocalDate} objects.
* Default is {@link DateTimeFormat#shortDate()}. * Default is {@link DateTimeFormat#shortDate()}.
* @param dateStyle the date format style
*/ */
public void setDateStyle(String dateStyle) { public void setDateStyle(String dateStyle) {
this.dateStyle = dateStyle; this.dateStyle = dateStyle;
@ -62,16 +66,15 @@ public class JodaTimeFormattingConfigurer {
/** /**
* Set the default format style of Joda {@link LocalTime} objects. * Set the default format style of Joda {@link LocalTime} objects.
* Default is {@link DateTimeFormat#shortTime()}. * Default is {@link DateTimeFormat#shortTime()}.
* @param timeStyle the time format style
*/ */
public void setTimeStyle(String timeStyle) { public void setTimeStyle(String timeStyle) {
this.timeStyle = timeStyle; this.timeStyle = timeStyle;
} }
/** /**
* Set the default format style of Joda {@link LocalDateTime} and {@link DateTime} objects, as well as JDK {@link Date} and {@link Calendar} objects. * Set the default format style of Joda {@link LocalDateTime} and {@link DateTime} objects,
* as well as JDK {@link Date} and {@link Calendar} objects.
* Default is {@link DateTimeFormat#shortDateTime()}. * Default is {@link DateTimeFormat#shortDateTime()}.
* @param dateTimeStyle the date time format style
*/ */
public void setDateTimeStyle(String dateTimeStyle) { public void setDateTimeStyle(String dateTimeStyle) {
this.dateTimeStyle = dateTimeStyle; this.dateTimeStyle = dateTimeStyle;
@ -81,27 +84,30 @@ public class JodaTimeFormattingConfigurer {
* Set whether standard ISO formatting should be applied to all Date/Time types. * Set whether standard ISO formatting should be applied to all Date/Time types.
* Default is false (no). * Default is false (no).
* If set to true, the dateStyle, timeStyle, and dateTimeStyle properties are ignored. * If set to true, the dateStyle, timeStyle, and dateTimeStyle properties are ignored.
* @param useISOFormat true to enable ISO formatting
*/ */
public void setUseISOFormat(boolean useISOFormat) { public void setUseIsoFormat(boolean useIsoFormat) {
this.useISOFormat = useISOFormat; this.useIsoFormat = useIsoFormat;
} }
/** /**
* Install Joda Time formatters given the current configuration of this {@link JodaTimeFormattingConfigurer}. * Install Joda Time formatters given the current configuration of this {@link JodaTimeFormattingConfigurer}.
*/ */
public void installJodaTimeFormatting(FormatterRegistry formatterRegistry) { public void installJodaTimeFormatting(FormatterRegistry formatterRegistry) {
JodaTimeConverters.registerConverters(formatterRegistry.getConverterRegistry()); JodaTimeConverters.registerConverters(formatterRegistry);
DateTimeFormatter jodaDateFormatter = getJodaDateFormatter(); DateTimeFormatter jodaDateFormatter = getJodaDateFormatter();
formatterRegistry.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(jodaDateFormatter), new DateTimeParser(jodaDateFormatter)); formatterRegistry.addFormatterForFieldType(LocalDate.class,
new ReadablePartialPrinter(jodaDateFormatter), new DateTimeParser(jodaDateFormatter));
DateTimeFormatter jodaTimeFormatter = getJodaTimeFormatter(); DateTimeFormatter jodaTimeFormatter = getJodaTimeFormatter();
formatterRegistry.addFormatterForFieldType(LocalTime.class, new ReadablePartialPrinter(jodaTimeFormatter), new DateTimeParser(jodaTimeFormatter)); formatterRegistry.addFormatterForFieldType(LocalTime.class,
new ReadablePartialPrinter(jodaTimeFormatter), new DateTimeParser(jodaTimeFormatter));
DateTimeFormatter jodaDateTimeFormatter = getJodaDateTimeFormatter(); DateTimeFormatter jodaDateTimeFormatter = getJodaDateTimeFormatter();
Parser<DateTime> dateTimeParser = new DateTimeParser(jodaDateTimeFormatter); Parser<DateTime> dateTimeParser = new DateTimeParser(jodaDateTimeFormatter);
formatterRegistry.addFormatterForFieldType(LocalDateTime.class, new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser); formatterRegistry.addFormatterForFieldType(LocalDateTime.class,
new ReadablePartialPrinter(jodaDateTimeFormatter), dateTimeParser);
Printer<ReadableInstant> readableInstantPrinter = new ReadableInstantPrinter(jodaDateTimeFormatter); Printer<ReadableInstant> readableInstantPrinter = new ReadableInstantPrinter(jodaDateTimeFormatter);
formatterRegistry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser); formatterRegistry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser);
@ -111,37 +117,42 @@ public class JodaTimeFormattingConfigurer {
formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
} }
// internal helpers // internal helpers
private DateTimeFormatter getJodaDateFormatter() { private DateTimeFormatter getJodaDateFormatter() {
if (this.useISOFormat) { if (this.useIsoFormat) {
return ISODateTimeFormat.date(); return ISODateTimeFormat.date();
} }
if (this.dateStyle != null) { if (this.dateStyle != null) {
return DateTimeFormat.forStyle(this.dateStyle + "-"); return DateTimeFormat.forStyle(this.dateStyle + "-");
} else {
}
else {
return DateTimeFormat.shortDate(); return DateTimeFormat.shortDate();
} }
} }
private DateTimeFormatter getJodaTimeFormatter() { private DateTimeFormatter getJodaTimeFormatter() {
if (this.useISOFormat) { if (this.useIsoFormat) {
return ISODateTimeFormat.time(); return ISODateTimeFormat.time();
} }
if (this.timeStyle != null) { if (this.timeStyle != null) {
return DateTimeFormat.forStyle("-" + this.timeStyle); return DateTimeFormat.forStyle("-" + this.timeStyle);
} else { }
else {
return DateTimeFormat.shortTime(); return DateTimeFormat.shortTime();
} }
} }
private DateTimeFormatter getJodaDateTimeFormatter() { private DateTimeFormatter getJodaDateTimeFormatter() {
if (this.useISOFormat) { if (this.useIsoFormat) {
return ISODateTimeFormat.dateTime(); return ISODateTimeFormat.dateTime();
} }
if (this.dateTimeStyle != null) { if (this.dateTimeStyle != null) {
return DateTimeFormat.forStyle(this.dateTimeStyle); return DateTimeFormat.forStyle(this.dateTimeStyle);
} else { }
else {
return DateTimeFormat.shortDateTime(); return DateTimeFormat.shortDateTime();
} }
} }

View File

@ -18,23 +18,21 @@ package org.springframework.web.servlet.tags;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext; import javax.servlet.jsp.PageContext;
import org.springframework.util.StringUtils;
import org.springframework.web.util.ExpressionEvaluationUtils; import org.springframework.web.util.ExpressionEvaluationUtils;
import org.springframework.web.util.HtmlUtils; import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.JavaScriptUtils; import org.springframework.web.util.JavaScriptUtils;
import org.springframework.web.util.TagUtils; import org.springframework.web.util.TagUtils;
import org.springframework.web.util.UriUtils; import org.springframework.web.util.UriUtils;
import org.springframework.util.StringUtils;
/** /**
* JSP tag for creating URLs. Modeled after the JSTL c:url tag with backwards * JSP tag for creating URLs. Modeled after the JSTL c:url tag with backwards
@ -44,7 +42,7 @@ import org.springframework.util.StringUtils;
* <ul> * <ul>
* <li>URL encoded template URI variables</li> * <li>URL encoded template URI variables</li>
* <li>HTML/XML escaping of URLs</li> * <li>HTML/XML escaping of URLs</li>
* <li>JavaScipt escaping of URLs</li> * <li>JavaScript escaping of URLs</li>
* </ul> * </ul>
* *
* <p>Template URI variables are indicated in the {@link #setValue(String) 'value'} * <p>Template URI variables are indicated in the {@link #setValue(String) 'value'}
@ -235,13 +233,11 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
* @param includeQueryStringDelimiter true if the query string should start * @param includeQueryStringDelimiter true if the query string should start
* with a '?' instead of '&' * with a '?' instead of '&'
* @return the query string * @return the query string
* @throws JspException
*/ */
protected String createQueryString(List<Param> params, Set<String> usedParams, boolean includeQueryStringDelimiter) protected String createQueryString(List<Param> params, Set<String> usedParams, boolean includeQueryStringDelimiter)
throws JspException { throws JspException {
String encoding = pageContext.getResponse().getCharacterEncoding(); String encoding = pageContext.getResponse().getCharacterEncoding();
StringBuilder qs = new StringBuilder(); StringBuilder qs = new StringBuilder();
for (Param param : params) { for (Param param : params) {
if (!usedParams.contains(param.getName()) && StringUtils.hasLength(param.getName())) { if (!usedParams.contains(param.getName()) && StringUtils.hasLength(param.getName())) {
@ -274,12 +270,11 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
* @param params parameters used to replace template markers * @param params parameters used to replace template markers
* @param usedParams set of template parameter names that have been replaced * @param usedParams set of template parameter names that have been replaced
* @return the URL with template parameters replaced * @return the URL with template parameters replaced
* @throws JspException
*/ */
protected String replaceUriTemplateParams(String uri, List<Param> params, Set<String> usedParams) protected String replaceUriTemplateParams(String uri, List<Param> params, Set<String> usedParams)
throws JspException { throws JspException {
String encoding = pageContext.getResponse().getCharacterEncoding();
String encoding = pageContext.getResponse().getCharacterEncoding();
for (Param param : params) { for (Param param : params) {
String template = URL_TEMPLATE_DELIMITER_PREFIX + param.getName() + URL_TEMPLATE_DELIMITER_SUFFIX; String template = URL_TEMPLATE_DELIMITER_PREFIX + param.getName() + URL_TEMPLATE_DELIMITER_SUFFIX;
if (uri.contains(template)) { if (uri.contains(template)) {