001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.date;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.plugin.Plugin;
037: import org.blojsom.plugin.PluginException;
038: import org.blojsom.util.BlojsomUtils;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.text.DateFormat;
043: import java.text.SimpleDateFormat;
044: import java.util.Locale;
045: import java.util.Map;
046: import java.util.TimeZone;
047:
048: /**
049: * DateFormatPlugin
050: *
051: * @author David Czarnecki
052: * @version $Id: DateFormatPlugin.java,v 1.5 2007/01/17 02:35:09 czarneckid Exp $
053: * @since blojsom 3.0
054: */
055: public class DateFormatPlugin implements Plugin {
056:
057: private Log _logger = LogFactory.getLog(DateFormatPlugin.class);
058:
059: private static final String BLOG_TIMEZONE_ID_IP = "blog-timezone-id";
060: private static final String BLOG_DATEFORMAT_PATTERN_IP = "blog-dateformat-pattern";
061:
062: /**
063: * Key under which the date format of the blog will be placed
064: * (example: on the request for the JSPDispatcher)
065: */
066: public static final String BLOJSOM_DATE_FORMAT = "BLOJSOM_DATE_FORMAT";
067:
068: /**
069: * Default constructor
070: */
071: public DateFormatPlugin() {
072: }
073:
074: /**
075: * Initialize this plugin. This method only called when the plugin is instantiated.
076: *
077: * @throws PluginException If there is an error initializing the plugin
078: */
079: public void init() throws PluginException {
080: }
081:
082: /**
083: * Process the blog entries
084: *
085: * @param httpServletRequest Request
086: * @param httpServletResponse Response
087: * @param blog {@link Blog} instance
088: * @param context Context
089: * @param entries Blog entries retrieved for the particular request
090: * @return Modified set of blog entries
091: * @throws PluginException If there is an error processing the blog entries
092: */
093: public Entry[] process(HttpServletRequest httpServletRequest,
094: HttpServletResponse httpServletResponse, Blog blog,
095: Map context, Entry[] entries) throws PluginException {
096: TimeZone _blogTimeZone;
097: String _blogDateFormatPattern;
098:
099: Locale _blogLocale;
100: DateFormat _blogDateFormat;
101:
102: String blogTimeZoneId = blog.getProperty(BLOG_TIMEZONE_ID_IP);
103: if (BlojsomUtils.checkNullOrBlank(blogTimeZoneId)) {
104: blogTimeZoneId = TimeZone.getDefault().getID();
105: }
106: if (_logger.isDebugEnabled()) {
107: _logger.debug("Timezone ID: " + blogTimeZoneId);
108: }
109: // Defaults to GMT if the Id is invalid
110: _blogTimeZone = TimeZone.getTimeZone(blogTimeZoneId);
111:
112: String blogDateFormatPattern = blog.getProperty(
113: BLOG_DATEFORMAT_PATTERN_IP, "EEEE, d MMMM yyyy", false);
114: if (BlojsomUtils.checkNullOrBlank(blogDateFormatPattern)) {
115: _blogDateFormatPattern = null;
116: if (_logger.isDebugEnabled()) {
117: _logger
118: .debug("No value supplied for blog-dateformat-pattern");
119: }
120: } else {
121: _blogDateFormatPattern = blogDateFormatPattern;
122: if (_logger.isDebugEnabled()) {
123: _logger.debug("Date format pattern: "
124: + blogDateFormatPattern);
125: }
126: }
127:
128: // Get a DateFormat for the specified TimeZone
129: _blogLocale = new Locale(blog.getBlogLanguage());
130: _blogDateFormat = DateFormat.getDateTimeInstance(
131: DateFormat.FULL, DateFormat.FULL, _blogLocale);
132: _blogDateFormat.setTimeZone(_blogTimeZone);
133: if (_blogDateFormatPattern != null) {
134: try {
135: SimpleDateFormat sdf = (SimpleDateFormat) _blogDateFormat;
136: sdf.applyPattern(_blogDateFormatPattern);
137: _blogDateFormat = sdf;
138: } catch (IllegalArgumentException ie) {
139: if (_logger.isErrorEnabled()) {
140: _logger.error("Date format pattern \""
141: + _blogDateFormatPattern
142: + "\" is invalid - using DateFormat.FULL");
143: }
144: } catch (ClassCastException ce) {
145: }
146: }
147:
148: context.put(BLOJSOM_DATE_FORMAT, _blogDateFormat);
149:
150: return entries;
151: }
152:
153: /**
154: * Perform any cleanup for the plugin. Called after {@link #process}.
155: *
156: * @throws PluginException If there is an error performing cleanup for this plugin
157: */
158: public void cleanup() throws PluginException {
159: }
160:
161: /**
162: * Called when BlojsomServlet is taken out of service
163: *
164: * @throws PluginException If there is an error in finalizing this plugin
165: */
166: public void destroy() throws PluginException {
167: }
168: }
|