KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > impl > basic > Channel


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: Channel.java,v 1.38 2004/09/02 09:07:56 spyromus Exp $
28

29 package de.nava.informa.impl.basic;
30
31 import java.net.URL JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import org.jdom.Element;
40
41 import de.nava.informa.core.CategoryIF;
42 import de.nava.informa.core.ChannelFormat;
43 import de.nava.informa.core.ChannelIF;
44 import de.nava.informa.core.ChannelObserverIF;
45 import de.nava.informa.core.CloudIF;
46 import de.nava.informa.core.ImageIF;
47 import de.nava.informa.core.ItemIF;
48 import de.nava.informa.core.TextInputIF;
49 import de.nava.informa.utils.XmlPathUtils;
50
51 /**
52  * In-Memory implementation of the ChannelIF interface.
53  *
54  * @author Niko Schmuck (niko@nava.de)
55  */

56 public class Channel implements ChannelIF, java.io.Serializable JavaDoc {
57
58   private long id;
59   private String JavaDoc title;
60   private String JavaDoc description;
61   private URL JavaDoc location;
62   private URL JavaDoc site;
63   private String JavaDoc creator;
64   private String JavaDoc publisher;
65   private String JavaDoc language;
66   private ChannelFormat format;
67   private Map JavaDoc items;
68   private ImageIF image;
69   private CloudIF cloud;
70   private TextInputIF textInput;
71   private String JavaDoc copyright;
72   private Collection JavaDoc categories;
73   private Date JavaDoc lastUpdated;
74   private Date JavaDoc lastBuild;
75   private Date JavaDoc pubDate;
76   private String JavaDoc rating;
77   private String JavaDoc generator;
78   private String JavaDoc docs;
79   private int ttl = -1;
80   private Element channelElement;
81
82   // RSS 1.0 Syndication Module values
83
// private String updatePeriod = UPDATE_DAILY;
84
// private int updateFrequency = 1;
85
private String JavaDoc updatePeriod = null;
86   private int updateFrequency = -1;
87   private Date JavaDoc updateBase;
88
89   // Has three values representing whether the Commons Collections 3.0 LinkedMap class is available:
90
// -1 : Don't know if it is available
91
// 0 : Not available
92
// 1 : Available
93
private static int hasLinkedMap = -1;
94
95   // Has three values representing whether the JDK 1.4+ LinkedHashMap class is available:
96
// -1 : Don't know if it is available
97
// 0 : Not available
98
// 1 : Available
99
private static int hasLinkedHashMap = -1;
100
101   private transient Collection JavaDoc observers;
102
103   public Channel() {
104     this((String JavaDoc) null);
105   }
106
107   public Channel(String JavaDoc title) {
108     this(null, title);
109   }
110
111   public Channel(Element channelElement) {
112     this(channelElement, "Unnamed channel");
113   }
114
115   public Channel(Element channelElement, String JavaDoc title) {
116     this.id = IdGenerator.getInstance().getId();
117     this.channelElement = channelElement;
118     this.title = title;
119     this.items = getMap();
120     this.categories = new ArrayList JavaDoc();
121     this.observers = new ArrayList JavaDoc();
122     this.format = ChannelFormat.UNKNOWN_CHANNEL_FORMAT;
123     this.lastUpdated = new Date JavaDoc();
124   }
125
126   // --------------------------------------------------------------
127
// implementation of ChannelIF interface
128
// --------------------------------------------------------------
129

130   public long getId() {
131     return id;
132   }
133
134   public void setId(long id) {
135     this.id = id;
136   }
137
138   public String JavaDoc getTitle() {
139     return title;
140   }
141
142   public void setTitle(String JavaDoc title) {
143     this.title = title;
144   }
145
146   public String JavaDoc getDescription() {
147     return description;
148   }
149
150   public void setDescription(String JavaDoc description) {
151     this.description = description;
152   }
153
154   public URL JavaDoc getLocation() {
155     return location;
156   }
157
158   public void setLocation(URL JavaDoc location) {
159     this.location = location;
160   }
161
162   public URL JavaDoc getSite() {
163     return site;
164   }
165
166   public void setSite(URL JavaDoc site) {
167     this.site = site;
168   }
169
170   public String JavaDoc getCreator() {
171     return creator;
172   }
173
174   public void setCreator(String JavaDoc creator) {
175     this.creator = creator;
176   }
177
178   public String JavaDoc getPublisher() {
179     return publisher;
180   }
181
182   public void setPublisher(String JavaDoc publisher) {
183     this.publisher = publisher;
184   }
185
186   public String JavaDoc getLanguage() {
187     return language;
188   }
189
190   public void setLanguage(String JavaDoc language) {
191     this.language = language;
192   }
193
194   public String JavaDoc getRating() {
195     return rating;
196   }
197
198   public void setRating(String JavaDoc rating) {
199     this.rating = rating;
200   }
201
202   public CloudIF getCloud() {
203     return cloud;
204   }
205
206   public void setCloud(CloudIF cloud) {
207     this.cloud = cloud;
208   }
209
210   public String JavaDoc getGenerator() {
211     return generator;
212   }
213
214   public void setGenerator(String JavaDoc generator) {
215     this.generator = generator;
216   }
217
218   public String JavaDoc getDocs() {
219     return docs;
220   }
221
222   public void setDocs(String JavaDoc docs) {
223     this.docs = docs;
224   }
225
226   public int getTtl() {
227     return ttl;
228   }
229
230   public void setTtl(int ttl) {
231     this.ttl = ttl;
232   }
233
234   public ChannelFormat getFormat() {
235     return format;
236   }
237
238   public void setFormat(ChannelFormat format) {
239     this.format = format;
240   }
241
242   public Collection JavaDoc getItems() {
243     return items.values();
244   }
245
246   public void addItem(ItemIF item) {
247     items.put(new Long JavaDoc(item.getId()), item);
248     item.setChannel(this);
249     notifyObserversItemAdded(item);
250   }
251
252   public void removeItem(ItemIF item) {
253     items.remove(new Long JavaDoc(item.getId()));
254   }
255
256   public ItemIF getItem(long anId) {
257     return (ItemIF) items.get(new Long JavaDoc(anId));
258   }
259
260   public ImageIF getImage() {
261     return image;
262   }
263
264   public void setImage(ImageIF image) {
265     this.image = image;
266   }
267
268   public TextInputIF getTextInput() {
269     return textInput;
270   }
271
272   public void setTextInput(TextInputIF textInput) {
273     this.textInput = textInput;
274   }
275
276   public String JavaDoc getCopyright() {
277     return copyright;
278   }
279
280   public void setCopyright(String JavaDoc copyright) {
281     this.copyright = copyright;
282   }
283
284   public Collection JavaDoc getCategories() {
285     return categories;
286   }
287
288   public void setCategories(Collection JavaDoc categories) {
289     this.categories = categories;
290   }
291
292   public void addCategory(CategoryIF category) {
293     categories.add(category);
294   }
295
296   public void removeCategory(CategoryIF category) {
297     categories.remove(category);
298   }
299
300   public Date JavaDoc getLastUpdated() {
301     return lastUpdated;
302   }
303
304   public void setLastUpdated(Date JavaDoc lastUpdated) {
305     this.lastUpdated = lastUpdated;
306     notifyObserversChannelUpdated();
307   }
308
309   public Date JavaDoc getLastBuildDate() {
310     return lastBuild;
311   }
312
313   public void setLastBuildDate(Date JavaDoc date) {
314     this.lastBuild = date;
315   }
316
317   public Date JavaDoc getPubDate() {
318     return pubDate;
319   }
320
321   public void setPubDate(Date JavaDoc pubDate) {
322     this.pubDate = pubDate;
323   }
324
325   /**
326     * setAllProperties - Set all the properties in this Channel by copying them
327     * from a source Channel.
328     * N.B. This includes all properties, but not children such as items etc.
329     * N.B. Location and Format are also not copied.
330     *
331     * @param sourceChan - ChannelIF that will supply new values
332     */

333    public void setAllProperties(ChannelIF sourceChan) {
334      setTitle(sourceChan.getTitle());
335      setDescription(sourceChan.getDescription());
336      setSite(sourceChan.getSite());
337      setCreator(sourceChan.getCreator());
338      setCopyright(sourceChan.getCopyright());
339      setPublisher(sourceChan.getPublisher());
340      setLanguage(sourceChan.getLanguage());
341      setImage(sourceChan.getImage());
342      setTextInput(sourceChan.getTextInput());
343      setRating(sourceChan.getRating());
344      setGenerator(sourceChan.getGenerator());
345      setDocs(sourceChan.getDocs());
346      setTtl(sourceChan.getTtl());
347      setCloud(sourceChan.getCloud());
348      setLastBuildDate(sourceChan.getLastBuildDate());
349      setUpdateBase(sourceChan.getUpdateBase());
350      setUpdateFrequency(sourceChan.getUpdateFrequency());
351      setUpdatePeriod(sourceChan.getUpdatePeriod());
352      setPubDate(sourceChan.getPubDate());
353    }
354
355
356   // RSS 1.0 Syndication Module methods
357

358   public String JavaDoc getUpdatePeriod() {
359     return updatePeriod;
360   }
361
362   public void setUpdatePeriod(String JavaDoc updatePeriod) {
363     this.updatePeriod = updatePeriod;
364   }
365
366   public int getUpdateFrequency() {
367     return updateFrequency;
368   }
369
370   public void setUpdateFrequency(int updateFrequency) {
371     this.updateFrequency = updateFrequency;
372   }
373
374   public Date JavaDoc getUpdateBase() {
375     return updateBase;
376   }
377
378   public void setUpdateBase(Date JavaDoc updateBase) {
379     this.updateBase = updateBase;
380   }
381
382   public String JavaDoc getElementValue(final String JavaDoc path) {
383     return XmlPathUtils.getElementValue(channelElement, path);
384   }
385
386   public String JavaDoc[] getElementValues(final String JavaDoc path, final String JavaDoc[] elements) {
387     return XmlPathUtils.getElementValues(channelElement, path, elements);
388   }
389
390   public String JavaDoc getAttributeValue(final String JavaDoc path, final String JavaDoc attribute) {
391     return XmlPathUtils.getAttributeValue(channelElement, path, attribute);
392   }
393
394   public String JavaDoc[] getAttributeValues(final String JavaDoc path, final String JavaDoc[] attributes) {
395     return XmlPathUtils.getAttributeValues(channelElement, path, attributes);
396   }
397
398   // --------------------------------------------------------------
399
// implementation of ChannelObservableIF interface
400
// --------------------------------------------------------------
401

402   public void addObserver(ChannelObserverIF o) {
403     observers.add(o);
404   }
405
406   public void removeObserver(ChannelObserverIF o) {
407     observers.remove(o);
408   }
409
410   // --------------------------------------------------------------
411
// overwrite default method implementation from Object
412
// --------------------------------------------------------------
413

414   /**
415    * Indicates whether some other object is "equal to" this one.
416    *
417    * @param obj the reference object with which to compare.
418    *
419    * @return <code>true</code> if this object is the same as the obj
420    * argument; <code>false</code> otherwise.
421    *
422    * @see #hashCode()
423    */

424   public boolean equals(Object JavaDoc obj) {
425     if (!(obj instanceof ChannelIF)) {
426       return false;
427     }
428     ChannelIF cmp = (ChannelIF) obj;
429
430     boolean te;
431     if (title != null) {
432       te = title.equals(cmp.getTitle());
433     } else {
434       te = (cmp.getTitle() == null);
435     }
436     boolean de;
437     if (description != null) {
438       de = description.equals(cmp.getDescription());
439     } else {
440       de = (cmp.getDescription() == null);
441     }
442     boolean le;
443     if (location != null) {
444       // Comparison of links uses synchronized code of Java-NET.
445
// This may hurt multi-threaded applications. So, please think twice
446
// before using direct comparison of links.
447
le = location.toString().equals(cmp.getLocation().toString());
448     } else {
449       le = (cmp.getLocation() == null);
450     }
451
452     return (te && de && le);
453   }
454
455   /**
456    * Returns a hash code value for the object. This method is
457    * supported for the benefit of hashtables such as those provided by
458    * <code>java.util.Hashtable</code>.
459    *
460    * @return a hash code value for this object.
461    */

462   public int hashCode() {
463     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(64);
464     sb.append(title).append(description).append(location);
465     return sb.toString().hashCode();
466   }
467
468   /**
469    * Returns a string representation of the object.
470    *
471    * @return a string representation of the object.
472    */

473   public String JavaDoc toString() {
474     return "[Basic Channel (" + id + "): " + title
475        + " (" + location + " )]";
476   }
477
478   // --------------------------------------------------------------
479
// internal helper method(s)
480
// --------------------------------------------------------------
481

482   /**
483    * Loops through and notifies each observer if a new item was
484    * detected.
485    * @param newItem the item
486    */

487   private void notifyObserversItemAdded(ItemIF newItem) {
488     Iterator JavaDoc it = observers.iterator();
489     while (it.hasNext()) {
490       ChannelObserverIF o = (ChannelObserverIF) it.next();
491       o.itemAdded(newItem);
492     }
493   }
494
495   /**
496    * Loops through and notifies each observer if a new item was
497    * detected.
498    */

499   private void notifyObserversChannelUpdated() {
500     Iterator JavaDoc it = observers.iterator();
501     while (it.hasNext()) {
502       ChannelObserverIF o = (ChannelObserverIF) it.next();
503       o.channelRetrieved(this);
504     }
505   }
506
507   /**
508     * If Commons Collections 3.0 LinkedMap is present, generate it,
509     * otherwise generate a LinkedHashMap for JDK 1.4+, and a HashMap for earlier versions.
510     */

511   private static Map JavaDoc getMap() {
512      if (checkLinkedMap3()) {
513         return CommonsCollections3MapFactory.getLinkedMap();
514       } else if (checkLinkedHashMap()) {
515         return JDK14MapFactory.getLinkedHashMap();
516       } else {
517         return new HashMap JavaDoc();
518       }
519    }
520
521   /**
522    * Checks whether Jakarta Commons Collections 3.0 LinkedMap is available.
523    */

524   private static boolean checkLinkedMap3() {
525     if (hasLinkedMap == -1) {
526       try {
527         Class.forName("org.apache.commons.collections.map.LinkedMap");
528         hasLinkedMap = 1;
529       } catch (ClassNotFoundException JavaDoc e) {
530         hasLinkedMap = 0;
531       }
532     }
533     return (hasLinkedMap == 1);
534   }
535
536   /**
537    * Generates a LinkedHashMap for JDK 1.4+.
538    * Is a separate class to avoid ClassNotFound exceptions.
539    */

540   private static final class JDK14MapFactory {
541     /** Hidden utility class constructor. */
542     private JDK14MapFactory() {
543     }
544
545     static Map JavaDoc getLinkedHashMap() {
546       return new java.util.LinkedHashMap JavaDoc();
547     }
548   }
549
550   /**
551    * Checks whether JDK 1.4+ LinkedHashMap is available.
552    */

553   private static boolean checkLinkedHashMap() {
554     if (hasLinkedHashMap == -1) {
555       try {
556         Class.forName("java.util.LinkedHashMap");
557         hasLinkedHashMap = 1;
558       } catch (ClassNotFoundException JavaDoc e) {
559         hasLinkedHashMap = 0;
560       }
561     }
562     return (hasLinkedHashMap == 1);
563   }
564
565   /**
566    * Generates a LinkedMap if Jakarta Commons Collections 3.0 is present.
567    * Is a separate class to avoid ClassNotFound exceptions.
568    */

569   private static final class CommonsCollections3MapFactory {
570     /** Hidden utility class constructor. */
571     private CommonsCollections3MapFactory() {
572     }
573
574     static Map JavaDoc getLinkedMap() {
575       return new org.apache.commons.collections.map.LinkedMap();
576     }
577   }
578 }
579
Popular Tags