KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > date > DateFormatPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

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 JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import java.text.DateFormat JavaDoc;
44 import java.text.SimpleDateFormat JavaDoc;
45 import java.util.Locale JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.TimeZone JavaDoc;
48
49 /**
50  * DateFormatPlugin
51  *
52  * @author David Czarnecki
53  * @version $Id: DateFormatPlugin.java,v 1.4 2006/04/26 02:09:45 czarneckid Exp $
54  * @since blojsom 3.0
55  */

56 public class DateFormatPlugin implements Plugin {
57
58     private Log _logger = LogFactory.getLog(DateFormatPlugin.class);
59
60     private static final String JavaDoc BLOG_TIMEZONE_ID_IP = "blog-timezone-id";
61     private static final String JavaDoc BLOG_DATEFORMAT_PATTERN_IP = "blog-dateformat-pattern";
62
63     /**
64      * Key under which the date format of the blog will be placed
65      * (example: on the request for the JSPDispatcher)
66      */

67     public static final String JavaDoc BLOJSOM_DATE_FORMAT = "BLOJSOM_DATE_FORMAT";
68
69     /**
70      * Default constructor
71      */

72     public DateFormatPlugin() {
73     }
74
75     /**
76      * Initialize this plugin. This method only called when the plugin is instantiated.
77      *
78      * @throws PluginException If there is an error initializing the plugin
79      */

80     public void init() throws PluginException {
81     }
82
83     /**
84      * Process the blog entries
85      *
86      * @param httpServletRequest Request
87      * @param httpServletResponse Response
88      * @param blog {@link Blog} instance
89      * @param context Context
90      * @param entries Blog entries retrieved for the particular request
91      * @return Modified set of blog entries
92      * @throws PluginException If there is an error processing the blog entries
93      */

94     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
95         TimeZone JavaDoc _blogTimeZone;
96         String JavaDoc _blogDateFormatPattern;
97
98         Locale JavaDoc _blogLocale;
99         DateFormat JavaDoc _blogDateFormat;
100
101         String JavaDoc 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         // Defaults to GMT if the Id is invalid
109
_blogTimeZone = TimeZone.getTimeZone(blogTimeZoneId);
110
111         String JavaDoc 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         // Get a DateFormat for the specified TimeZone
125
_blogLocale = new Locale JavaDoc(blog.getBlogLanguage());
126         _blogDateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, _blogLocale);
127         _blogDateFormat.setTimeZone(_blogTimeZone);
128         if (_blogDateFormatPattern != null) {
129             try {
130                 SimpleDateFormat JavaDoc sdf = (SimpleDateFormat JavaDoc) _blogDateFormat;
131                 sdf.applyPattern(_blogDateFormatPattern);
132                 _blogDateFormat = sdf;
133             } catch (IllegalArgumentException JavaDoc ie) {
134                 if (_logger.isErrorEnabled()) {
135                     _logger.error("Date format pattern \"" + _blogDateFormatPattern + "\" is invalid - using DateFormat.FULL");
136                 }
137             } catch (ClassCastException JavaDoc ce) {
138             }
139         }
140
141         context.put(BLOJSOM_DATE_FORMAT, _blogDateFormat);
142         
143         return entries;
144     }
145
146     /**
147      * Perform any cleanup for the plugin. Called after {@link #process}.
148      *
149      * @throws PluginException If there is an error performing cleanup for this plugin
150      */

151     public void cleanup() throws PluginException {
152     }
153
154     /**
155      * Called when BlojsomServlet is taken out of service
156      *
157      * @throws PluginException If there is an error in finalizing this plugin
158      */

159     public void destroy() throws PluginException {
160     }
161 }
162
Popular Tags