001: /*
002: * Created on 26 Oct 2006
003: */
004: package uk.org.ponder.htmlutil;
005:
006: import java.text.DateFormat;
007: import java.text.DateFormatSymbols;
008: import java.text.SimpleDateFormat;
009: import java.util.Calendar;
010: import java.util.Locale;
011:
012: import uk.org.ponder.arrayutil.ArrayUtil;
013: import uk.org.ponder.localeutil.LocaleReceiver;
014: import uk.org.ponder.stringutil.CharWrap;
015:
016: /**
017: * Emits a selection of Date symbols and formats for a particular Locale,
018: * suitable for inclusion as part of a Javascript <script> block.
019: *
020: * @author Antranig Basman (antranig@caret.cam.ac.uk)
021: */
022:
023: public class DateSymbolJSEmitter extends LocaleReceiver {
024: private String prefix = "PUC_";
025:
026: public void setArrayNamePrefix(String prefix) {
027: this .prefix = prefix;
028: }
029:
030: public String emitDateSymbols() {
031: Locale locale = getLocale();
032: CharWrap togo = new CharWrap();
033: togo.append(HTMLConstants.JS_BLOCK_START);
034:
035: Calendar defcal = Calendar.getInstance(locale);
036: DateFormatSymbols formatsymbols = new DateFormatSymbols(locale);
037: // note that Java numbers months starting at 0!
038: int cmonths = defcal.getActualMaximum(Calendar.MONTH) + 1;
039:
040: String[] monthlong = formatsymbols.getMonths();
041: monthlong = (String[]) ArrayUtil.trim(monthlong, cmonths);
042: togo.append(HTMLUtil.emitJavascriptArray(
043: prefix + "MONTHS_LONG", monthlong));
044:
045: String[] monthshort = formatsymbols.getShortMonths();
046: monthshort = (String[]) ArrayUtil.trim(monthshort, cmonths);
047: togo.append(HTMLUtil.emitJavascriptArray(prefix
048: + "MONTHS_SHORT", monthshort));
049:
050: String[] weeklong = formatsymbols.getWeekdays();
051: // note that Java numbers weekdays starting at 1!
052: weeklong = (String[]) ArrayUtil.subArray(weeklong, 1, 8);
053: togo.append(HTMLUtil.emitJavascriptArray(prefix
054: + "WEEKDAYS_LONG", weeklong));
055:
056: String[] weekmed = formatsymbols.getShortWeekdays();
057: weekmed = (String[]) ArrayUtil.subArray(weekmed, 1, 8);
058: togo.append(HTMLUtil.emitJavascriptArray(prefix
059: + "WEEKDAYS_MEDIUM", weekmed));
060:
061: String[] weekshort = shortenWeekdays(weekmed);
062: togo.append(HTMLUtil.emitJavascriptArray(prefix
063: + "WEEKDAYS_SHORT", weekshort));
064:
065: String[] weekonechar = oneCharWeekdays(weekmed);
066: togo.append(HTMLUtil.emitJavascriptArray(prefix
067: + "WEEKDAYS_1CHAR", weekonechar));
068:
069: int firstday = defcal.getFirstDayOfWeek() - 1;
070: togo.append(HTMLUtil.emitJavascriptVar(prefix
071: + "FIRST_DAY_OF_WEEK", Integer.toString(firstday)));
072:
073: // These illegal casts are required to access the pattern string
074: SimpleDateFormat dateformat = (SimpleDateFormat) DateFormat
075: .getDateInstance(DateFormat.SHORT, locale);
076: togo.append(HTMLUtil.emitJavascriptVar(prefix + "DATE_FORMAT",
077: dateformat.toLocalizedPattern()));
078:
079: SimpleDateFormat datetimeformat = (SimpleDateFormat) DateFormat
080: .getDateTimeInstance(DateFormat.SHORT,
081: DateFormat.SHORT, locale);
082: togo.append(HTMLUtil.emitJavascriptVar(prefix
083: + "DATETIME_FORMAT", datetimeformat
084: .toLocalizedPattern()));
085:
086: SimpleDateFormat timeformat = (SimpleDateFormat) DateFormat
087: .getTimeInstance(DateFormat.SHORT, locale);
088: togo.append(HTMLUtil.emitJavascriptVar(prefix + "TIME_FORMAT",
089: timeformat.toLocalizedPattern()));
090:
091: togo.append(HTMLConstants.JS_BLOCK_END);
092: return togo.toString();
093: }
094:
095: public static String shorten(String s, int prefix, int length) {
096: if (prefix != 0) {
097: s = s.substring(prefix);
098: }
099: return s.length() < length ? s : s.substring(0, length);
100:
101: }
102:
103: // Deal with oriental-style weekday names by removing any common prefix
104: public static int getCommonPrefix(String[] names) {
105: if (names.length < 2)
106: return 0;
107: else {
108: int index = 0;
109: while (true) {
110: if (index >= names[0].length()
111: || index >= names[1].length())
112: break;
113: if (names[0].charAt(index) != names[1].charAt(index))
114: break;
115: ++index;
116: }
117: return index;
118: }
119: }
120:
121: String[] shortenWeekdays(String[] weekmed) {
122: String[] togo = new String[weekmed.length];
123: int prefix = getCommonPrefix(weekmed);
124: for (int i = 0; i < weekmed.length; ++i) {
125: togo[i] = shorten(weekmed[i], prefix, 2);
126: }
127: return togo;
128: }
129:
130: String[] oneCharWeekdays(String[] weekmed) {
131: String[] togo = new String[weekmed.length];
132: int prefix = getCommonPrefix(weekmed);
133: for (int i = 0; i < weekmed.length; ++i) {
134: togo[i] = shorten(weekmed[i], prefix, 1);
135: }
136: return togo;
137: }
138: }
|