001: /*---------------------------------------------------------------------------*\
002: $Id: ShowAuthorsPlugIn.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>ShowAuthorsPlugIn</tt> handles enabling/disabling display of the
067: * "author" 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>ShowAuthors</tt></td>
079: * <td>Default (global) value for the show authors capability.
080: * Defaults to false.</td>
081: * </tr>
082: * <tr valign="top">
083: * <td><tt>[Feed<i>xxx</i>]</tt></td>
084: * <td><tt>ShowAuthors</tt></td>
085: * <td>Whether or not to show author data 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 ShowAuthorsPlugIn implements MainConfigItemPlugIn,
093: FeedConfigItemPlugIn, PostFeedParsePlugIn {
094: /*----------------------------------------------------------------------*\
095: Private Constants
096: \*----------------------------------------------------------------------*/
097:
098: private static final String VAR_SHOW_AUTHORS = "ShowAuthors";
099:
100: /*----------------------------------------------------------------------*\
101: Private Data Items
102: \*----------------------------------------------------------------------*/
103:
104: /**
105: * Feed author flag, by feed
106: */
107: private Map<FeedInfo, Boolean> perFeedShowAuthorsFlag = new HashMap<FeedInfo, Boolean>();
108:
109: /**
110: * Global default
111: */
112: private boolean showAuthorsDefault = false;
113:
114: /**
115: * For log messages
116: */
117: private static final Logger log = new Logger(
118: ShowAuthorsPlugIn.class);
119:
120: /*----------------------------------------------------------------------*\
121: Constructor
122: \*----------------------------------------------------------------------*/
123:
124: /**
125: * Default constructor (required).
126: */
127: public ShowAuthorsPlugIn() {
128: // Nothing to do
129: }
130:
131: /*----------------------------------------------------------------------*\
132: Public Methods Required by *PlugIn Interfaces
133: \*----------------------------------------------------------------------*/
134:
135: /**
136: * Get a displayable name for the plug-in.
137: *
138: * @return the name
139: */
140: public String getPlugInName() {
141: return "Show Authors";
142: }
143:
144: /**
145: * Get the sort key for this plug-in.
146: *
147: * @return the sort key string.
148: */
149: public String getPlugInSortKey() {
150: return ClassUtil.getShortClassName(getClass().getName());
151: }
152:
153: /**
154: * Initialize the plug-in. This method is called before any of the
155: * plug-in methods are called.
156: *
157: * @throws CurnException on error
158: */
159: public void initPlugIn() throws CurnException {
160: }
161:
162: /**
163: * Called immediately after <i>curn</i> has read and processed a
164: * configuration item in the main [curn] configuration section. All
165: * configuration items are passed, one by one, to each loaded plug-in.
166: * If a plug-in class is not interested in a particular configuration
167: * item, this method should simply return without doing anything. Note
168: * that some configuration items may simply be variable assignment;
169: * there's no real way to distinguish a variable assignment from a
170: * blessed configuration item.
171: *
172: * @param sectionName the name of the configuration section where
173: * the item was found
174: * @param paramName the name of the parameter
175: * @param config the {@link CurnConfig} object
176: *
177: * @throws CurnException on error
178: *
179: * @see CurnConfig
180: */
181: public void runMainConfigItemPlugIn(String sectionName,
182: String paramName, CurnConfig config) throws CurnException {
183: try {
184: if (paramName.equals(VAR_SHOW_AUTHORS)) {
185: showAuthorsDefault = config.getRequiredBooleanValue(
186: sectionName, paramName);
187: }
188: }
189:
190: catch (ConfigurationException ex) {
191: throw new CurnException(ex);
192: }
193: }
194:
195: /**
196: * Called immediately after <i>curn</i> has read and processed a
197: * configuration item in a "feed" configuration section. All
198: * configuration items are passed, one by one, to each loaded plug-in.
199: * If a plug-in class is not interested in a particular configuration
200: * item, this method should simply return without doing anything. Note
201: * that some configuration items may simply be variable assignment;
202: * there's no real way to distinguish a variable assignment from a
203: * blessed configuration item.
204: *
205: * @param sectionName the name of the configuration section where
206: * the item was found
207: * @param paramName the name of the parameter
208: * @param config the active configuration
209: * @param feedInfo partially complete <tt>FeedInfo</tt> object
210: * for the feed. The URL is guaranteed to be
211: * present, but no other fields are.
212: *
213: * @return <tt>true</tt> to continue processing the feed,
214: * <tt>false</tt> to skip it
215: *
216: * @throws CurnException on error
217: *
218: * @see CurnConfig
219: * @see FeedInfo
220: * @see FeedInfo#getURL
221: */
222: public boolean runFeedConfigItemPlugIn(String sectionName,
223: String paramName, CurnConfig config, FeedInfo feedInfo)
224: throws CurnException {
225: try {
226: if (paramName.equals(VAR_SHOW_AUTHORS)) {
227: boolean flag = config.getRequiredBooleanValue(
228: sectionName, paramName);
229: perFeedShowAuthorsFlag.put(feedInfo, flag);
230: log.debug("[" + sectionName + "]: " + paramName + "="
231: + flag);
232: }
233:
234: return true;
235: }
236:
237: catch (ConfigurationException ex) {
238: throw new CurnException(ex);
239: }
240: }
241:
242: /**
243: * Called immediately after a feed is parsed, but before it is
244: * otherwise processed. This method can return <tt>false</tt> to signal
245: * <i>curn</i> that the feed should be skipped. For instance, a plug-in
246: * that filters on the parsed feed data could use this method to weed
247: * out non-matching feeds before they are downloaded. Similarly, a
248: * plug-in that edits the parsed data (removing or editing individual
249: * items, for instance) could use method to do so.
250: *
251: * @param feedInfo the {@link FeedInfo} object for the feed that
252: * has been downloaded and parsed.
253: * @param feedCache the feed cache
254: * @param channel the parsed channel data
255: *
256: * @return <tt>true</tt> if <i>curn</i> should continue to process the
257: * feed, <tt>false</tt> to skip the feed. A return value of
258: * <tt>false</tt> aborts all further processing on the feed.
259: * In particular, <i>curn</i> will not pass the feed along to
260: * other plug-ins that have yet to be notified of this event.
261: *
262: * @throws CurnException on error
263: *
264: * @see RSSChannel
265: * @see FeedInfo
266: */
267: public boolean runPostFeedParsePlugIn(FeedInfo feedInfo,
268: FeedCache feedCache, RSSChannel channel)
269: throws CurnException {
270: Boolean showBoxed = perFeedShowAuthorsFlag.get(feedInfo);
271: boolean show = showAuthorsDefault;
272:
273: if (showBoxed != null)
274: show = showBoxed;
275:
276: log.debug("Post-parse, " + feedInfo.getURL() + ": showAuthors="
277: + show);
278:
279: if (!show) {
280: log.debug("Removing author fields from feed \""
281: + feedInfo.getURL().toString() + "\"");
282:
283: channel.clearAuthors();
284: for (RSSItem item : channel.getItems())
285: item.clearAuthors();
286: }
287:
288: return true;
289: }
290: }
|