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. |
From: <bra...@us...> - 2010-04-23 23:36:46
|
Revision: 3052 http://archive-access.svn.sourceforge.net/archive-access/?rev=3052&view=rev Author: bradtofel Date: 2010-04-23 23:36:40 +0000 (Fri, 23 Apr 2010) Log Message: ----------- FEATURE: added spacesToNBSP() 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-23 23:35:12 UTC (rev 3051) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java 2010-04-23 23:36:40 UTC (rev 3052) @@ -204,4 +204,13 @@ public String escapeJavaScript(String raw) { return StringEscapeUtils.escapeJavaScript(raw); } + + /** + * Convert... spaces to + * @param input to replace + * @return with spaces replaced + */ + public String spaceToNBSP(String input) { + return input.replaceAll(" ", " "); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bra...@us...> - 2010-08-10 22:44:58
|
Revision: 3225 http://archive-access.svn.sourceforge.net/archive-access/?rev=3225&view=rev Author: bradtofel Date: 2010-08-10 22:44:51 +0000 (Tue, 10 Aug 2010) Log Message: ----------- TWEAK: Quieted down stacktrace 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-08-10 21:24:46 UTC (rev 3224) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java 2010-08-10 22:44:51 UTC (rev 3225) @@ -104,8 +104,8 @@ try { return getFormat(getLocalized(key)).format(objects); } catch (Exception e) { - e.printStackTrace(); - + // TODO: log message + //e.printStackTrace(); } return key; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bra...@us...> - 2011-02-06 14:40:51
|
Revision: 3398 http://archive-access.svn.sourceforge.net/archive-access/?rev=3398&view=rev Author: bradtofel Date: 2011-02-06 14:40:45 +0000 (Sun, 06 Feb 2011) Log Message: ----------- new constructor without resourceBundle check for NPE changed visibility of getFormat 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 2011-02-06 14:38:47 UTC (rev 3397) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java 2011-02-06 14:40:45 UTC (rev 3398) @@ -54,13 +54,22 @@ * objects. * @param locale to use, where applicable with MessageFormat objects */ + public StringFormatter(ResourceBundle bundle) { + this(null,Locale.getDefault()); + } + /** + * 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; this.locale = locale; formats = new HashMap<String,MessageFormat>(); } - private MessageFormat getFormat(String pattern) { + public MessageFormat getFormat(String pattern) { MessageFormat format = formats.get(pattern); if(format == null) { format = new MessageFormat(pattern,locale); @@ -88,11 +97,18 @@ * something goes wrong... */ public String getLocalized(String key) { - try { - return bundle.getString(key); - } catch (Exception e) { - return key; + if(bundle != null) { + try { + return bundle.getString(key); + // String localized = bundle.getString(key); + // if((localized != null) && (localized.length() > 0)) { + // return localized; + // } + } catch (Exception e) { + } + } + return key; } private String formatInner(String key, Object objects[]) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bra...@us...> - 2011-09-06 03:47:37
|
Revision: 3514 http://archive-access.svn.sourceforge.net/archive-access/?rev=3514&view=rev Author: bradtofel Date: 2011-09-06 03:47:31 +0000 (Tue, 06 Sep 2011) Log Message: ----------- FEATURE: added simple String.join functionality 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 2011-09-06 03:46:44 UTC (rev 3513) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/StringFormatter.java 2011-09-06 03:47:31 UTC (rev 3514) @@ -22,6 +22,7 @@ import java.text.DateFormat; import java.text.Format; import java.text.MessageFormat; +import java.util.Collection; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -224,4 +225,21 @@ public String spaceToNBSP(String input) { return input.replaceAll(" ", " "); } + + public static String join(String delim, String...stuff) { + int max = stuff.length - 1; + int len = delim.length() * max; + for(String s : stuff) { + len += s.length(); + } + StringBuilder sb = new StringBuilder(len); + for(int i = 0; i < stuff.length; i++) { + sb.append(stuff[i]); + if(i < max) { + sb.append(delim); + } + } + return sb.toString(); + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |