001: /*---------------------------------------------------------------------------*\
002: $Id: ShowDatesPlugIn.java 7041 2007-09-09 01:04:47Z bmc $
003: ---------------------------------------------------------------------------
004: This software is released under a BSD-style license:
005:
006: Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions are
010: met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. The end-user documentation included with the redistribution, if any,
016: must include the following acknowlegement:
017:
018: "This product includes software developed by Brian M. Clapper
019: (bmc@clapper.org, http://www.clapper.org/bmc/). That software is
020: copyright (c) 2004-2007 Brian M. Clapper."
021:
022: Alternately, this acknowlegement may appear in the software itself,
023: if wherever such third-party acknowlegements normally appear.
024:
025: 3. Neither the names "clapper.org", "curn", nor any of the names of the
026: project contributors may be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact bmc@clapper.org.
029:
030: 4. Products derived from this software may not be called "curn", nor may
031: "clapper.org" appear in their names without prior written permission
032: of Brian M. Clapper.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
036: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
037: NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
038: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
039: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
040: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
041: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: \*---------------------------------------------------------------------------*/
045:
046: package org.clapper.curn.plugins;
047:
048: import org.clapper.curn.CurnConfig;
049: import org.clapper.curn.CurnException;
050: import org.clapper.curn.FeedInfo;
051: import org.clapper.curn.MainConfigItemPlugIn;
052: import org.clapper.curn.FeedConfigItemPlugIn;
053: import org.clapper.curn.PostFeedParsePlugIn;
054: import org.clapper.curn.parser.RSSChannel;
055: import org.clapper.curn.parser.RSSItem;
056:
057: import org.clapper.util.classutil.ClassUtil;
058: import org.clapper.util.config.ConfigurationException;
059: import org.clapper.util.logging.Logger;
060:
061: import java.util.HashMap;
062: import java.util.Map;
063: import org.clapper.curn.FeedCache;
064:
065: /**
066: * The <tt>ShowDatesPlugIn</tt> handles enabling/disabling display of the
067: * "date" fields on feeds and feed items. It intercepts the following
068: * configuration parameters:
069: *
070: * <table border="1">
071: * <tr valign="top" align="left">
072: * <th align="left">Section</th>
073: * <th align="left">Parameter</th>
074: * <th align="left">Meaning</th>
075: * </tr>
076: * <tr valign="top">
077: * <td><tt>[curn]</tt></td>
078: * <td><tt>ShowDates</tt></td>
079: * <td>Default (global) value for the show dates capability.
080: * Defaults to false.</td>
081: * </tr>
082: * <tr valign="top">
083: * <td><tt>[Feed<i>xxx</i>]</tt></td>
084: * <td><tt>ShowDates</tt></td>
085: * <td>Whether or not to show dates for the feed. If not specified,
086: * the global default is used.</td>
087: * </tr>
088: * </table>
089: *
090: * @version <tt>$Revision: 7041 $</tt>
091: */
092: public class ShowDatesPlugIn implements MainConfigItemPlugIn,
093: FeedConfigItemPlugIn, PostFeedParsePlugIn {
094: /*----------------------------------------------------------------------*\
095: Private Constants
096: \*----------------------------------------------------------------------*/
097:
098: private static final String VAR_SHOW_DATES = "ShowDates";
099:
100: /*----------------------------------------------------------------------*\
101: Private Data Items
102: \*----------------------------------------------------------------------*/
103:
104: /**
105: * Feed author flag, by feed
106: */
107: private Map<FeedInfo, Boolean> perFeedShowDatesFlag = new HashMap<FeedInfo, Boolean>();
108:
109: /**
110: * Global default
111: */
112: private boolean showDatesDefault = false;
113:
114: /**
115: * For log messages
116: */
117: private static final Logger log = new Logger(ShowDatesPlugIn.class);
118:
119: /*----------------------------------------------------------------------*\
120: Constructor
121: \*----------------------------------------------------------------------*/
122:
123: /**
124: * Default constructor (required).
125: */
126: public ShowDatesPlugIn() {
127: // Nothing to do
128: }
129:
130: /*----------------------------------------------------------------------*\
131: Public Methods Required by *PlugIn Interfaces
132: \*----------------------------------------------------------------------*/
133:
134: /**
135: * Get a displayable name for the plug-in.
136: *
137: * @return the name
138: */
139: public String getPlugInName() {
140: return "Show Dates";
141: }
142:
143: /**
144: * Get the sort key for this plug-in.
145: *
146: * @return the sort key string.
147: */
148: public String getPlugInSortKey() {
149: return ClassUtil.getShortClassName(getClass().getName());
150: }
151:
152: /**
153: * Initialize the plug-in. This method is called before any of the
154: * plug-in methods are called.
155: *
156: * @throws CurnException on error
157: */
158: public void initPlugIn() throws CurnException {
159: }
160:
161: /**
162: * Called immediately after <i>curn</i> has read and processed a
163: * configuration item in the main [curn] configuration section. All
164: * configuration items are passed, one by one, to each loaded plug-in.
165: * If a plug-in class is not interested in a particular configuration
166: * item, this method should simply return without doing anything. Note
167: * that some configuration items may simply be variable assignment;
168: * there's no real way to distinguish a variable assignment from a
169: * blessed configuration item.
170: *
171: * @param sectionName the name of the configuration section where
172: * the item was found
173: * @param paramName the name of the parameter
174: * @param config the {@link CurnConfig} object
175: *
176: * @throws CurnException on error
177: *
178: * @see CurnConfig
179: */
180: public void runMainConfigItemPlugIn(String sectionName,
181: String paramName, CurnConfig config) throws CurnException {
182: try {
183: if (paramName.equals(VAR_SHOW_DATES)) {
184: showDatesDefault = config.getRequiredBooleanValue(
185: sectionName, paramName);
186: }
187: }
188:
189: catch (ConfigurationException ex) {
190: throw new CurnException(ex);
191: }
192: }
193:
194: /**
195: * Called immediately after <i>curn</i> has read and processed a
196: * configuration item in a "feed" configuration section. All
197: * configuration items are passed, one by one, to each loaded plug-in.
198: * If a plug-in class is not interested in a particular configuration
199: * item, this method should simply return without doing anything. Note
200: * that some configuration items may simply be variable assignment;
201: * there's no real way to distinguish a variable assignment from a
202: * blessed configuration item.
203: *
204: * @param sectionName the name of the configuration section where
205: * the item was found
206: * @param paramName the name of the parameter
207: * @param config the active configuration
208: * @param feedInfo partially complete <tt>FeedInfo</tt> object
209: * for the feed. The URL is guaranteed to be
210: * present, but no other fields are.
211: *
212: * @return <tt>true</tt> to continue processing the feed,
213: * <tt>false</tt> to skip it
214: *
215: * @throws CurnException on error
216: *
217: * @see CurnConfig
218: * @see FeedInfo
219: * @see FeedInfo#getURL
220: */
221: public boolean runFeedConfigItemPlugIn(String sectionName,
222: String paramName, CurnConfig config, FeedInfo feedInfo)
223: throws CurnException {
224: try {
225: if (paramName.equals(VAR_SHOW_DATES)) {
226: boolean flag = config.getRequiredBooleanValue(
227: sectionName, paramName);
228: perFeedShowDatesFlag.put(feedInfo, flag);
229: log.debug("[" + sectionName + "]: " + paramName + "="
230: + flag);
231: }
232:
233: return true;
234: }
235:
236: catch (ConfigurationException ex) {
237: throw new CurnException(ex);
238: }
239: }
240:
241: /**
242: * Called immediately after a feed is parsed, but before it is
243: * otherwise processed. This method can return <tt>false</tt> to signal
244: * <i>curn</i> that the feed should be skipped. For instance, a plug-in
245: * that filters on the parsed feed data could use this method to weed
246: * out non-matching feeds before they are downloaded. Similarly, a
247: * plug-in that edits the parsed data (removing or editing individual
248: * items, for instance) could use method to do so.
249: *
250: * @param feedInfo the {@link FeedInfo} object for the feed that
251: * has been downloaded and parsed.
252: * @param feedCache the feed cache
253: * @param channel the parsed channel data
254: *
255: * @return <tt>true</tt> if <i>curn</i> should continue to process the
256: * feed, <tt>false</tt> to skip the feed. A return value of
257: * <tt>false</tt> aborts all further processing on the feed.
258: * In particular, <i>curn</i> will not pass the feed along to
259: * other plug-ins that have yet to be notified of this event.
260: *
261: * @throws CurnException on error
262: *
263: * @see RSSChannel
264: * @see FeedInfo
265: */
266: public boolean runPostFeedParsePlugIn(FeedInfo feedInfo,
267: FeedCache feedCache, RSSChannel channel)
268: throws CurnException {
269: Boolean showBoxed = perFeedShowDatesFlag.get(feedInfo);
270: boolean show = showDatesDefault;
271:
272: if (showBoxed != null)
273: show = showBoxed;
274:
275: log.debug("Post-parse, " + feedInfo.getURL() + ": showDates="
276: + show);
277:
278: if (!show) {
279: log.debug("Removing date fields from feed \""
280: + feedInfo.getURL().toString() + "\"");
281:
282: channel.setPublicationDate(null);
283: for (RSSItem item : channel.getItems())
284: item.setPublicationDate(null);
285: }
286:
287: return true;
288: }
289: }
|