📄 rssreader.java
字号:
/*
* This library is part of the code for the book: OpenCms for Developers
*
* Copyright (c) 2007, 2008 Dan Liliedahl
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.deepthoughts.rss;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
/**
* A simple bean which reads an RSS Feed using ROME.
*/
public class RSSReader {
/** The URL of the RSS Feed */
protected String m_strFeed;
/** SyndFeedInput instance */
protected SyndFeedInput m_input;
/** SyndFeed instance */
protected SyndFeed m_feed;
/** This contains the list of RSS Items */
protected List m_lstItems;
/** This is an iterator for the items */
protected Iterator m_iItems;
/** Error message */
protected String m_strError = "";
/**
* Constructor
*
* @param Feed the RSS feed to be read
*/
public RSSReader(String Feed) {
m_strFeed = Feed;
m_input = new SyndFeedInput();
m_feed = null;
m_lstItems = null;
m_iItems = null;
}
/**
* Reads all items from the given feed. If an error occurs then nothing will be read but the
* error message will be set and may be retrieved using the <code>{@link #getError()}</code> method
*/
public boolean readAll() {
try {
// Create the URL connection
URLConnection conn = new URL(m_strFeed).openConnection();
// some feeds require User-Agent identification
conn.setRequestProperty("User-Agent", "DeepThoughts RSS Reader");
// build the feed and read all the entries
m_feed = m_input.build(new XmlReader(conn));
m_lstItems = m_feed.getEntries();
return true;
} catch (IllegalArgumentException e) {
m_strError = e.getMessage();
} catch (MalformedURLException e) {
m_strError = e.getMessage();
} catch (FeedException e) {
m_strError = e.getMessage();
} catch (IOException e) {
m_strError = e.getMessage();
}
return false;
}
/**
* Returns the error string.
*
* @return String containing any error resulting from attempting to read the feeds
*/
public String getError() {
return m_strError;
}
/**
* Determine if more feed items are in the read feed
*
* @return true if more items, else false
*/
public boolean hasMore() {
if (null != m_lstItems) {
if (null == m_iItems) {
m_iItems = m_lstItems.iterator();
}
return m_iItems.hasNext();
}
return false;
}
/**
* Returns the next feed item.
*
* @return SyndEntry for the next feed item
*/
public SyndEntry getNext() {
if (hasMore()) {
return (SyndEntry) m_iItems.next();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -