001: /*
002: * Created on Apr 23, 2004
003: *
004: * This file is part of Thingamablog. ( http://thingamablog.sf.net )
005: *
006: * Copyright (c) 2004, Bob Tantlinger All Rights Reserved.
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
021: * USA.
022: */
023: package net.sf.thingamablog.generator;
024:
025: import java.text.SimpleDateFormat;
026: import java.util.Date;
027: import java.util.Hashtable;
028: import java.util.Locale;
029:
030: /**
031: * A tag for date-related values
032: *
033: * @author Bob Tantlinger
034: *
035: */
036: public class DateTag extends TemplateTag {
037: /** Attribute name for the format value */
038: public static final String FORMAT = "format";
039: public static final String LANG = "lang";
040: public static final String COUNTRY = "country";
041:
042: public static final String RFC822 = "RFC822";
043: public static final String RFC822_FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
044:
045: private Hashtable ht;
046: private Locale locale = Locale.getDefault();
047: private SimpleDateFormat df;
048:
049: /**
050: * Constructs a DateTag
051: *
052: * @param name The name of the tag
053: */
054: public DateTag(String name) {
055: super (name);
056: ht = new Hashtable();
057: ht.put(FORMAT, "dd/MM/yy h:mm");
058: ht.put(LANG, "");
059: ht.put(COUNTRY, "");
060: df = new SimpleDateFormat(ht.get(FORMAT).toString());
061: }
062:
063: /* (non-Javadoc)
064: * @see net.sf.thingamablog.tmpl.TemplateElement#getDefaultAttributes()
065: */
066: public Hashtable getDefaultAttributes() {
067: return ht;
068: }
069:
070: /* (non-Javadoc)
071: * @see net.sf.thingamablog.tmpl.TemplateElement#process(java.lang.Object, java.lang.String, java.util.Hashtable)
072: */
073: public String process(Object obj, Hashtable attribs) {
074: SimpleDateFormat sdf = initFormatter(attribs);
075: try {
076: return sdf.format((Date) obj);
077: } catch (ClassCastException cce) {
078: }
079: return "";
080: }
081:
082: private SimpleDateFormat initFormatter(Hashtable ht) {
083: if (ht.get(FORMAT).toString().equalsIgnoreCase(RFC822)) {
084: ht.put(FORMAT, RFC822_FORMAT);
085: ht.put(LANG, "en");
086: ht.put(COUNTRY, "US");
087: }
088:
089: if (ht.get(LANG).equals("") && ht.get(COUNTRY).equals("")) {
090: df.applyPattern(ht.get(FORMAT).toString());
091: return df; //no locale attribs specified so return default
092: }
093:
094: Locale loc = new Locale(locale.getLanguage(), locale
095: .getCountry());
096: if (!ht.get(LANG).equals(""))
097: loc = new Locale(ht.get(LANG).toString(), loc.getCountry());
098: if (!ht.get(COUNTRY).equals(""))
099: loc = new Locale(loc.getLanguage(), ht.get(COUNTRY)
100: .toString());
101:
102: return new SimpleDateFormat(ht.get(FORMAT).toString(), loc);
103: }
104:
105: /**
106: * Gets the locale of the date formatter
107: *
108: * @return The locale
109: */
110: public Locale getLocale() {
111: return locale;
112: }
113:
114: /**
115: * Sets the locale of the date formatter
116: *
117: * @param locale The locale
118: */
119: public void setLocale(Locale locale) {
120: this .locale = locale;
121: df = new SimpleDateFormat(ht.get(FORMAT).toString(), locale);
122: }
123: }
|