Source Code Cross Referenced for FeedHandler.java in  » Portal » Open-Portal » com » sun » portal » rssportlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.rssportlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.