001: /*---------------------------------------------------------------------------*\
002: $Id: RSSLink.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.parser;
047:
048: import java.net.URL;
049: import java.util.Collection;
050: import java.util.LinkedHashSet;
051:
052: /**
053: * An <tt>RSSLink</tt> object describes a link to a URL, including any
054: * metadata about the URL's content (if available).
055: *
056: * @see RSSItem
057: * @see RSSChannel
058: *
059: * @version <tt>$Revision: 7041 $</tt>
060: */
061: public class RSSLink {
062: /*----------------------------------------------------------------------*\
063: Constants
064: \*----------------------------------------------------------------------*/
065:
066: /**
067: * Link types.
068: */
069: public enum Type {
070: /**
071: * The link refers back to the RSS feed.
072: */
073: SELF,
074:
075: /**
076: * The link refers to an alternate page containing the same
077: * information, but perhaps in a different format (e.g., HTML).
078: */
079: ALTERNATE
080: };
081:
082: /*----------------------------------------------------------------------*\
083: Private Instance Data
084: \*----------------------------------------------------------------------*/
085:
086: private URL url = null;
087: private String mimeType = null;
088: private Type linkType = Type.SELF;
089:
090: private Collection<RSSLinkChangeListener> changeListeners = new LinkedHashSet<RSSLinkChangeListener>();
091:
092: /*----------------------------------------------------------------------*\
093: Constructors
094: \*----------------------------------------------------------------------*/
095:
096: /**
097: * Create an empty <tt>RSSLink</tt> object.
098: *
099: * @see #RSSLink(URL,String,Type)
100: */
101: public RSSLink() {
102: // nothing to do
103: }
104:
105: /**
106: * Create and populate a new <tt>RSSLink</tt> object.
107: *
108: * @param url the link's URL
109: * @param mimeType the MIME type. Must not be null.
110: * @param linkType the link type
111: */
112: public RSSLink(URL url, String mimeType, Type linkType) {
113: this (url, mimeType, linkType, null);
114: }
115:
116: /**
117: * Create and populate a new <tt>RSSLink</tt> object.
118: *
119: * @param url the link's URL
120: * @param mimeType the MIME type. Must not be null.
121: * @param linkType the link type
122: * @param changeListener initial {@link RSSLinkChangeListener} to add to
123: * this object. Can be null.
124: */
125: public RSSLink(URL url, String mimeType, Type linkType,
126: RSSLinkChangeListener changeListener) {
127: this .url = url;
128: this .mimeType = mimeType;
129: this .linkType = linkType;
130:
131: if (changeListener != null)
132: addChangeListener(changeListener);
133: }
134:
135: /*----------------------------------------------------------------------*\
136: Public Methods
137: \*----------------------------------------------------------------------*/
138:
139: /**
140: * Add a change listener to this <tt>RSSLink</tt>.
141: *
142: * @param listener the listener to add
143: *
144: * @see #removeChangeListener
145: */
146: public void addChangeListener(RSSLinkChangeListener listener) {
147: changeListeners.add(listener);
148: }
149:
150: /**
151: * Remove a change listener from this <tt>RSSLink</tt>.
152: *
153: * @param listener the listener to remove
154: *
155: * @see #addChangeListener
156: */
157: public void removeChangeListener(RSSLinkChangeListener listener) {
158: changeListeners.remove(listener);
159: }
160:
161: /**
162: * Get the URL for this link.
163: *
164: * @return the URL, or null if not set yet.
165: *
166: * @see #setURL
167: */
168: public URL getURL() {
169: return url;
170: }
171:
172: /**
173: * Set the URL for this link.
174: *
175: * @param url the URL, or null if not set yet.
176: *
177: * @see #getURL
178: */
179: public void setURL(URL url) {
180: URL oldURL = this .url;
181: this .url = url;
182:
183: for (RSSLinkChangeListener listener : changeListeners)
184: listener.onURLChange(this , oldURL, this .url);
185: }
186:
187: /**
188: * Get the MIME type for this link.
189: *
190: * @return the MIME type, or null if not set yet.
191: *
192: * @see #setMIMEType
193: */
194: public String getMIMEType() {
195: return mimeType;
196: }
197:
198: /**
199: * Set the MIME type for this link.
200: *
201: * @param mimeType the MIME Type, or null if not set yet.
202: *
203: * @see #getMIMEType
204: */
205: public void setMIMEType(String mimeType) {
206: String oldMIMEType = this .mimeType;
207: this .mimeType = mimeType;
208:
209: for (RSSLinkChangeListener listener : changeListeners)
210: listener.onMIMETypeChange(this , oldMIMEType, this .mimeType);
211: }
212:
213: /**
214: * Get the link type for this link.
215: *
216: * @return the link type, or null if not set yet.
217: *
218: * @see #setLinkType
219: */
220: public Type getLinkType() {
221: return linkType;
222: }
223:
224: /**
225: * Set the link type for this link.
226: *
227: * @param linkType the link Type, or null if not set yet.
228: *
229: * @see #getLinkType
230: */
231: public void setLinkType(Type linkType) {
232: Type oldType = this .linkType;
233: this .linkType = linkType;
234:
235: for (RSSLinkChangeListener listener : changeListeners)
236: listener.onLinkTypeChange(this , oldType, this .linkType);
237: }
238:
239: /**
240: * Get the string representation of this link (the URL).
241: *
242: * @return the string version
243: */
244: @Override
245: public String toString() {
246: return (url == null) ? "null" : url.toExternalForm();
247: }
248: }
|