1 31 package org.blojsom.plugin.date; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Entry; 37 import org.blojsom.plugin.Plugin; 38 import org.blojsom.plugin.PluginException; 39 import org.blojsom.util.BlojsomUtils; 40 41 import javax.servlet.http.HttpServletRequest ; 42 import javax.servlet.http.HttpServletResponse ; 43 import java.text.DateFormat ; 44 import java.text.SimpleDateFormat ; 45 import java.util.Locale ; 46 import java.util.Map ; 47 import java.util.TimeZone ; 48 49 56 public class DateFormatPlugin implements Plugin { 57 58 private Log _logger = LogFactory.getLog(DateFormatPlugin.class); 59 60 private static final String BLOG_TIMEZONE_ID_IP = "blog-timezone-id"; 61 private static final String BLOG_DATEFORMAT_PATTERN_IP = "blog-dateformat-pattern"; 62 63 67 public static final String BLOJSOM_DATE_FORMAT = "BLOJSOM_DATE_FORMAT"; 68 69 72 public DateFormatPlugin() { 73 } 74 75 80 public void init() throws PluginException { 81 } 82 83 94 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 95 TimeZone _blogTimeZone; 96 String _blogDateFormatPattern; 97 98 Locale _blogLocale; 99 DateFormat _blogDateFormat; 100 101 String blogTimeZoneId = blog.getProperty(BLOG_TIMEZONE_ID_IP); 102 if (BlojsomUtils.checkNullOrBlank(blogTimeZoneId)) { 103 blogTimeZoneId = TimeZone.getDefault().getID(); 104 } 105 if (_logger.isDebugEnabled()) { 106 _logger.debug("Timezone ID: " + blogTimeZoneId); 107 } 108 _blogTimeZone = TimeZone.getTimeZone(blogTimeZoneId); 110 111 String blogDateFormatPattern = blog.getProperty(BLOG_DATEFORMAT_PATTERN_IP, "EEEE, d MMMM yyyy", false); 112 if (BlojsomUtils.checkNullOrBlank(blogDateFormatPattern)) { 113 _blogDateFormatPattern = null; 114 if (_logger.isDebugEnabled()) { 115 _logger.debug("No value supplied for blog-dateformat-pattern"); 116 } 117 } else { 118 _blogDateFormatPattern = blogDateFormatPattern; 119 if (_logger.isDebugEnabled()) { 120 _logger.debug("Date format pattern: " + blogDateFormatPattern); 121 } 122 } 123 124 _blogLocale = new Locale (blog.getBlogLanguage()); 126 _blogDateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, _blogLocale); 127 _blogDateFormat.setTimeZone(_blogTimeZone); 128 if (_blogDateFormatPattern != null) { 129 try { 130 SimpleDateFormat sdf = (SimpleDateFormat ) _blogDateFormat; 131 sdf.applyPattern(_blogDateFormatPattern); 132 _blogDateFormat = sdf; 133 } catch (IllegalArgumentException ie) { 134 if (_logger.isErrorEnabled()) { 135 _logger.error("Date format pattern \"" + _blogDateFormatPattern + "\" is invalid - using DateFormat.FULL"); 136 } 137 } catch (ClassCastException ce) { 138 } 139 } 140 141 context.put(BLOJSOM_DATE_FORMAT, _blogDateFormat); 142 143 return entries; 144 } 145 146 151 public void cleanup() throws PluginException { 152 } 153 154 159 public void destroy() throws PluginException { 160 } 161 } 162 | Popular Tags |