From: <bra...@us...> - 2010-04-14 21:23:42
|
Revision: 3039 http://archive-access.svn.sourceforge.net/archive-access/?rev=3039&view=rev Author: bradtofel Date: 2010-04-14 21:23:31 +0000 (Wed, 14 Apr 2010) Log Message: ----------- BUGFIX/FEATURE: now uses explicitly sets UTC on internal DateFormat objects generated by MessageFormat FEATURE: this class now handles HTML escaping COMMENTS: Javadoc is now more-or-less complete Modified Paths: -------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java 2010-04-14 21:20:49 UTC (rev 3038) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java 2010-04-14 21:23:31 UTC (rev 3039) @@ -24,26 +24,40 @@ */ package org.archive.wayback.util; +import java.text.DateFormat; +import java.text.Format; import java.text.MessageFormat; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; +import java.util.TimeZone; +import org.apache.commons.lang.StringEscapeUtils; + /** + * An class which assists in UI generation, primarily through Locale-aware + * String formatting, and also helps in escaping (hopefully properly) Strings + * for use in HTML. + * + * Note that date formatting done through this class forces all times to the + * UTC timezone - at the moment it appears too confusing to attempt to localize + * times in any other way.. * - * * @author brad * @version $Date$, $Revision$ */ public class StringFormatter { - + private final static TimeZone TZ_UTC = TimeZone.getTimeZone("UTC"); + ResourceBundle bundle = null; Locale locale = null; Map<String,MessageFormat> formats = null; /** - * @param bundle - * @param locale + * Construct a StringFormatter... + * @param bundle ResourceBundle to lookup patterns for MessageFormat + * objects. + * @param locale to use, where applicable with MessageFormat objects */ public StringFormatter(ResourceBundle bundle, Locale locale) { this.bundle = bundle; @@ -55,13 +69,26 @@ MessageFormat format = formats.get(pattern); if(format == null) { format = new MessageFormat(pattern,locale); + // lets try to make sure any internal DateFormats use UTC: + Format[] subFormats = format.getFormats(); + if(subFormats != null) { + for(Format subFormat : subFormats) { + if(subFormat instanceof DateFormat) { + DateFormat subDateFormat = (DateFormat) subFormat; + subDateFormat.setTimeZone(TZ_UTC); + } + } + } + formats.put(pattern,format); } return format; } /** - * @param key + * Access a localized string associated with key from the ResourceBundle, + * likely the UI.properties file. + * @param key to lookup in the ResourceBundle * @return localized String version of key argument, or key itself if * something goes wrong... */ @@ -73,68 +100,108 @@ } } - /** - * @param key - * @param args - * @return Localized String for key, interpolated with args - */ - public String format(String key, Object args[]) { + private String formatInner(String key, Object objects[]) { try { - return getFormat(getLocalized(key)).format(args); + return getFormat(getLocalized(key)).format(objects); } catch (Exception e) { e.printStackTrace(); } return key; } + + // What gives? This works in the Junit test, but not in jsps... +// /** +// * @param key String property name in UI.properties file to use as the +// * pattern for interpolation +// * @param objects array of things to interpolate within the MessageFormat +// * described by the pattern in UI.properties for key key +// * @return Localized Formatted String for key, interpolated with args +// */ +// public String format(String key, Object...objects) { +// return formatInner(key,objects); +// } /** - * @param key + * Localize a string key from the UI.properties file + * @param key String property name in UI.properties file to use as the + * pattern for the MessageFormat * @return Localized String for key */ public String format(String key) { Object args[] = {}; - return format(key,args); + return formatInner(key,args); } /** - * @param key - * @param o1 - * @return Localized String for key, interpolated with o1 + * @param key String property name in UI.properties file to use as the + * pattern for interpolation + * @param o1 thing1 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @return Localized Formatted String for key, interpolated with argument objects */ public String format(String key,Object o1) { Object args[] = {o1}; - return format(key,args); + return formatInner(key,args); } /** - * @param key - * @param o1 - * @param o2 - * @return Localized String for key, interpolated with o1,o2 + * @param key String property name in UI.properties file to use as the + * pattern for interpolation + * @param o1 thing1 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @param o2 thing2 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @return Localized Formatted String for key, interpolated with argument objects */ public String format(String key,Object o1,Object o2) { Object args[] = {o1,o2}; - return format(key,args); + return formatInner(key,args); } /** - * @param key - * @param o1 - * @param o2 - * @param o3 - * @return Localized String for key, interpolated with o1,o2,o3 + * @param key String property name in UI.properties file to use as the + * pattern for interpolation + * @param o1 thing1 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @param o2 thing2 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @param o3 thing3 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @return Localized Formatted String for key, interpolated with argument objects */ public String format(String key,Object o1,Object o2,Object o3) { Object args[] = {o1,o2,o3}; - return format(key,args); + return formatInner(key,args); } /** - * @param key - * @param o1 - * @param o2 - * @param o3 - * @param o4 - * @return Localized String for key, interpolated with o1,o2,o3,o4 + * @param key String property name in UI.properties file to use as the + * pattern for interpolation + * @param o1 thing1 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @param o2 thing2 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @param o3 thing3 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @param o4 thing4 to interpolate within the MessageFormat + * described by the pattern in UI.properties for key key + * @return Localized Formatted String for key, interpolated with argument objects */ public String format(String key,Object o1,Object o2,Object o3,Object o4) { Object args[] = {o1,o2,o3,o4}; - return format(key,args); + return formatInner(key,args); } + + /** + * handy shortcut to the apache StringEscapeUtils + * @param raw string to be escaped + * @return the string escaped so it's safe for insertion in HTML + */ + public String escapeHtml(String raw) { + return StringEscapeUtils.escapeHtml(raw); + } + /** + * handy shortcut to the apache StringEscapeUtils + * @param raw string to be escaped + * @return the string escaped so it's safe for insertion in Javascript + */ + public String escapeJavaScript(String raw) { + return StringEscapeUtils.escapeJavaScript(raw); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |