DateFormatPlugin.java [plain text]
package org.blojsom.plugin.date;
import org.blojsom.plugin.BlojsomPlugin;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.blog.Blog;
import org.blojsom.blog.BlogEntry;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.util.BlojsomUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.TimeZone;
import java.util.Locale;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class DateFormatPlugin implements BlojsomPlugin {
private Log _logger = LogFactory.getLog(DateFormatPlugin.class);
private static final String BLOG_TIMEZONE_ID_IP = "blog-timezone-id";
private static final String BLOG_DATEFORMAT_PATTERN_IP = "blog-dateformat-pattern";
public static final String BLOJSOM_DATE_FORMAT = "BLOJSOM_DATE_FORMAT";
public DateFormatPlugin() {
}
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration) throws BlojsomPluginException {
}
public BlogEntry[] process(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
BlogUser user,
Map context,
BlogEntry[] entries) throws BlojsomPluginException {
Blog blog = user.getBlog();
TimeZone _blogTimeZone;
String _blogDateFormatPattern;
Locale _blogLocale;
DateFormat _blogDateFormat;
String blogTimeZoneId = blog.getBlogProperty(BLOG_TIMEZONE_ID_IP);
if (BlojsomUtils.checkNullOrBlank(blogTimeZoneId)) {
blogTimeZoneId = TimeZone.getDefault().getID();
}
_logger.debug("Timezone ID: " + blogTimeZoneId);
_blogTimeZone = TimeZone.getTimeZone(blogTimeZoneId);
String blogDateFormatPattern = blog.getBlogProperty(BLOG_DATEFORMAT_PATTERN_IP);
if (BlojsomUtils.checkNullOrBlank(blogDateFormatPattern)) {
_blogDateFormatPattern = null;
_logger.debug("No value supplied for blog-dateformat-pattern");
} else {
_blogDateFormatPattern = blogDateFormatPattern;
_logger.debug("Date format pattern: " + blogDateFormatPattern);
}
_blogLocale = new Locale(blog.getBlogLanguage());
_blogDateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, _blogLocale);
_blogDateFormat.setTimeZone(_blogTimeZone);
if (_blogDateFormatPattern != null) {
try {
SimpleDateFormat sdf = (SimpleDateFormat) _blogDateFormat;
sdf.applyPattern(_blogDateFormatPattern);
_blogDateFormat = sdf;
} catch (IllegalArgumentException ie) {
_logger.error("Date format pattern \"" + _blogDateFormatPattern + "\" is invalid - using DateFormat.FULL");
} catch (ClassCastException ce) {
_logger.warn("Cannot cast to SimpleDateFormat to apply date format pattern");
}
}
context.put(BLOJSOM_DATE_FORMAT, _blogDateFormat);
return entries;
}
public void cleanup() throws BlojsomPluginException {
}
public void destroy() throws BlojsomPluginException {
}
}