001: /**
002: * RSS framework and reader
003: * Copyright (C) 2004 Christian Robert
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */package org.jperdian.rss2;
019:
020: import java.net.URL;
021:
022: import org.jperdian.rss2.dom.RssChannel;
023:
024: /**
025: * Implementation of a connector that can be used to read RSS channel data
026: * and transform them into a valid RSS DOM object
027: *
028: * @author Christian Robert
029: */
030:
031: public class RssClient {
032:
033: private RssParser myParser = new RssParser();
034: private URL myURL = null;
035: private String myKeywords = "";
036: private String myTitle = "";
037:
038: /**
039: * Creates a new client that receives it's data from the given URL
040: * @param url
041: * the <code>URL</code> from which to receive the data
042: */
043: public RssClient(URL url) {
044: this (url, "", "");
045: }
046:
047: /**
048: * Creates a new client that receives it's data from the given URL
049: * @param url
050: * the <code>URL</code> from which to receive the data
051: * @param keywords
052: * the keywords that can later be used for sorting
053: * @param title
054: * the title to be used for display
055: */
056: public RssClient(URL url, String keywords, String title) {
057: this .setURL(url);
058: this .setKeywords(keywords);
059: this .setTitle(title);
060: }
061:
062: /**
063: * Reads the data from the remote RSS source and create a new
064: * <code>RssChannel</code> object that contains the data that has been read
065: * @return
066: * the data in RSS DOM format
067: * @throws RssException
068: * thrown if the data is not in valid RSS format or the connection
069: * is unreachable
070: */
071: public final RssChannel getData() throws RssException {
072: RssChannel channel = new RssChannel(this );
073: channel.setClient(this );
074: this .loadData(channel);
075: return channel;
076: }
077:
078: /**
079: * Loads the data into the given <code>RssChannel</code>
080: */
081: public void loadData(RssChannel channel) throws RssException {
082: this .getParser().parse(this .getURL(), channel);
083: channel.setDataLoaded(true);
084: channel.setLastUpdate(System.currentTimeMillis());
085: }
086:
087: // --------------------------------------------------------------------------
088: // -- Property access methods ---------------------------------------------
089: // --------------------------------------------------------------------------
090:
091: /**
092: * Gets the <code>RssParser</code> through which the messages are analyzed
093: */
094: protected RssParser getParser() {
095: return this .myParser;
096: }
097:
098: /**
099: * Sets the <code>URL</code> from which to read the data
100: */
101: protected void setURL(URL url) {
102: this .myURL = url;
103: }
104:
105: /**
106: * Gets the <code>URL</code> from which to read the data
107: */
108: public URL getURL() {
109: return this .myURL;
110: }
111:
112: /**
113: * Sets the keywords to be used for sorting the current client
114: */
115: protected void setKeywords(String keywords) {
116: this .myKeywords = keywords;
117: }
118:
119: /**
120: * Gets the keywords to be used for sorting the current client
121: */
122: public String getKeywords() {
123: return this .myKeywords;
124: }
125:
126: /**
127: * Sets the title to be used for sorting the current client
128: */
129: protected void setTitle(String title) {
130: this .myTitle = title;
131: }
132:
133: /**
134: * Gets the title to be used for sorting the current client
135: */
136: public String getTitle() {
137: return this.myTitle;
138: }
139:
140: }
|