001: /**
002: * $Id: FeedHandler.java,v 1.2 2006/04/17 20:08:41 jtb Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.rssportlet;
014:
015: import com.sun.syndication.feed.synd.SyndContent;
016: import com.sun.syndication.feed.synd.SyndEntry;
017: import com.sun.syndication.feed.synd.SyndFeed;
018: import com.sun.syndication.io.FeedException;
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Comparator;
026:
027: /**
028: * This class handles builds a <code>FeedBean</code>
029: * object.
030: */
031: public class FeedHandler {
032: public static class EntryPubDateComparator implements Comparator {
033: public int compare(Object o1, Object o2) {
034: SyndEntry e1 = (SyndEntry) o1;
035: SyndEntry e2 = (SyndEntry) o2;
036:
037: Date pub1 = e1.getPublishedDate();
038: Date pub2 = e2.getPublishedDate();
039:
040: if (pub1 == null && pub2 == null) {
041: return 0;
042: }
043: if (pub1 == null) {
044: return 1;
045: }
046: if (pub2 == null) {
047: return -1;
048: }
049:
050: return pub2.compareTo(pub1);
051: }
052:
053: public boolean equals(Object o) {
054: if (o instanceof EntryPubDateComparator) {
055: return true;
056: }
057: return false;
058: }
059: }
060:
061: private static final HTMLCleaner HTML_CLEANER = new HTMLCleaner();
062:
063: private SettingsBean settingsBean;
064: private FeedBean feedBean;
065:
066: private Resources resources;
067:
068: private void initFeedBean() throws IOException, FeedException {
069: initFeed();
070: initFeedEntries();
071: initFeedDescription();
072: }
073:
074: /** Should the feed entry be included in the display? */
075: private boolean isInTime(SyndEntry entry) {
076: // if max age is disabled, everything is in time
077: if (getSettingsBean().isDisableMaxAge()) {
078: return true;
079: }
080: Date pubDate = entry.getPublishedDate();
081: if (pubDate == null) {
082: // assume in time if there's no pub date
083: return true;
084: }
085:
086: long now = System.currentTimeMillis();
087: long pubTime = pubDate.getTime();
088: long maxTime = (long) getSettingsBean().getMaxAge() * 60 * 60 * 1000;
089:
090: if ((pubTime + maxTime) > now) {
091: return true;
092: }
093:
094: return false;
095: }
096:
097: /**
098: * Initialize the bean's feed entries
099: *
100: * The entries are filtered based on their published time, cleaned of all
101: * HTML, and truncated to a maximum length.
102: */
103: private void initFeedEntries() throws IOException, FeedException {
104: List feedEntries;
105: SyndFeed feed = getFeedBean().getFeed();
106: if (feed == null) {
107: feedEntries = Collections.EMPTY_LIST;
108: } else {
109: feedEntries = new ArrayList();
110:
111: List entries = feed.getEntries();
112: int maxEntries = getSettingsBean().getMaxEntries();
113: int numEntries = 0;
114:
115: for (Iterator i = entries.iterator(); numEntries < maxEntries
116: && i.hasNext();) {
117: SyndEntry entry = (SyndEntry) i.next();
118: // test if the entry should be displayed based on its publish time
119: if (isInTime(entry)) {
120: // filter tags out of description
121: SyndContent desc = entry.getDescription();
122: if (desc != null) {
123: String d = desc.getValue();
124: d = HTML_CLEANER.clean(d);
125: int maxLength = getSettingsBean()
126: .getMaxDescriptionLength();
127: if (d.length() > maxLength) {
128: // truncate description
129: d = d.substring(0, maxLength - 1);
130: // add "more" link to indicate that the description
131: // has been truncated
132: d += " <a href=\"" + entry.getLink()
133: + "\">" + resources.get("more")
134: + "</a>";
135: }
136: desc.setValue(d);
137: entry.setDescription(desc);
138: }
139: // keep track that we don't go over max entries
140: numEntries++;
141: feedEntries.add(entry);
142: }
143: }
144: }
145: Collections.sort(feedEntries, new EntryPubDateComparator());
146: getFeedBean().setFeedEntries(feedEntries);
147: }
148:
149: /**
150: * Initialize the feed description
151: *
152: * The feed description is has all HTML filtered out, and is possibly
153: * truncated to fit within the maximum description length.
154: */
155: private void initFeedDescription() throws IOException,
156: FeedException {
157: SyndFeed feed = getFeedBean().getFeed();
158: String desc = feed.getDescription();
159: if (desc != null) {
160: desc = HTML_CLEANER.clean(desc);
161: int maxLength = getSettingsBean().getMaxDescriptionLength();
162: if (desc.length() > maxLength) {
163: desc = desc.substring(0, maxLength - 1);
164: }
165: }
166:
167: getFeedBean().setFeedDescription(desc);
168: }
169:
170: /**
171: * Initialize the ROME feed object.
172: */
173: private void initFeed() throws IOException, FeedException {
174: // delegate to the feed helper to get from cache
175: SyndFeed feed = FeedHelper.getInstance().getFeed(
176: getSettingsBean());
177: getFeedBean().setFeed(feed);
178: }
179:
180: /** Get the Rss portlet bean. */
181: public SettingsBean getSettingsBean() {
182: return settingsBean;
183: }
184:
185: /** Set the Rss portlet bean. */
186: public void setSettingsBean(SettingsBean settingsBean) {
187: this .settingsBean = settingsBean;
188: resources = new Resources(
189: "com.sun.portal.rssportlet.FeedHandler", settingsBean
190: .getLocale());
191: }
192:
193: /** Get the feed bean object. */
194: public FeedBean getFeedBean() {
195: return feedBean;
196: }
197:
198: /**
199: * Set the feed bean.
200: *
201: * This operation has the side effect of causing the feed bean to be
202: * initialized.
203: */
204: public void setFeedBean(FeedBean feedBean) throws IOException,
205: FeedException {
206: this.feedBean = feedBean;
207: initFeedBean();
208: }
209: }
|