View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package com.sun.syndication.feed.synd;
18  
19  import com.sun.syndication.common.ObjectBean;
20  import com.sun.syndication.feed.WireFeed;
21  import com.sun.syndication.feed.synd.impl.Converters;
22  import com.sun.syndication.feed.module.DCModule;
23  import com.sun.syndication.feed.module.ModuleI;
24  import com.sun.syndication.feed.module.DCModuleI;
25  
26  import java.util.ArrayList;
27  import java.util.Date;
28  import java.util.List;
29  
30  /***
31   * Bean for all types of feeds.
32   * <p>
33   * It handles all RSS versions and Atom 0.3, it normalizes all info, it may lose information.
34   * <p>
35   * @author Alejandro Abdelnur
36   *
37   */
38  public class SyndFeed extends ObjectBean implements SyndFeedI {
39      private String _title;
40      private String _feedType;
41      private String _link;
42      private String _description;
43      private SyndImageI _image;
44      private List _entries;
45      private List _modules;
46  
47      private static final Converters CONVERTERS = new Converters();
48  
49      /***
50       * Returns the real feed types the SyndFeed supports when converting from and to.
51       * <p>
52       * @return the real feed type supported.
53       */
54      public List getSupportedFeedTypes() {
55          return CONVERTERS.getSupportedFeedTypes();
56      }
57  
58      /***
59       * Default constructor. All properties are set to <b>null</b>.
60       * <p>
61       *
62       */
63      public SyndFeed() {
64          this(null);
65      }
66  
67      /***
68       * Creates a SyndFeed an populates all its properties out of the
69       * given RSS Channel or Atom Feed properties.
70       * <p>
71       * @param feed the RSS Channel or the Atom Feed to populate the properties from.
72       *
73       */
74      public SyndFeed(WireFeed feed) {
75          if (feed!=null) {
76              _feedType = feed.getFeedType();
77              Converter converter = (Converter) CONVERTERS.getConverter(_feedType);
78              if (converter==null) {
79                  throw new IllegalArgumentException("Invalid feed type ["+_feedType+"]");
80              }
81              converter.copyInto(feed,this);
82          }
83      }
84  
85      /***
86       * Creates a real feed containing the information of the SyndFeed.
87       * <p>
88       * The feed type of the created WireFeed is taken from the SyndFeed feedType property.
89       * <p>
90       * @return the real feed.
91       *
92       */
93      public WireFeed createWireFeed() {
94          return createWireFeed(_feedType);
95      }
96  
97      /***
98       * Creates a real feed containing the information of the SyndFeed.
99       * <p>
100      * @param feedType the feed type for the WireFeed to be created.
101      * @return the real feed.
102      *
103      */
104     public WireFeed createWireFeed(String feedType) {
105         if (feedType==null) {
106             throw new IllegalArgumentException("Feed type cannot be null");
107         }
108         Converter converter = (Converter) CONVERTERS.getConverter(feedType);
109         if (converter==null) {
110             throw new IllegalArgumentException("Invalid feed type ["+feedType+"]");
111         }
112         return converter.createRealFeed(this);
113     }
114 
115     /***
116      * Returns the wire feed type the feed had/will-have when coverted from/to a WireFeed.
117      * <p>
118      * @return the feed type, <b>null</b> if none.
119      *
120      */
121     public String getFeedType() {
122         return _feedType;
123     }
124 
125     /***
126      * Sets the wire feed type the feed will-have when coverted to a WireFeed.
127      * <p>
128      * @param feedType the feed type to set, <b>null</b> if none.
129      *
130      */
131     public void setFeedType(String feedType) {
132         _feedType = feedType;
133     }
134 
135     /***
136      * Returns the feed title.
137      * <p>
138      * @return the feed title, <b>null</b> if none.
139      *
140      */
141     public String getTitle() {
142         return _title;
143     }
144 
145     /***
146      * Sets the feed title.
147      * <p>
148      * @param title the feed title to set, <b>null</b> if none.
149      *
150      */
151     public void setTitle(String title) {
152         _title = title;
153     }
154 
155     /***
156      * Returns the feed link.
157      * <p>
158      * @return the feed link, <b>null</b> if none.
159      *
160      */
161     public String getLink() {
162         return _link;
163     }
164 
165     /***
166      * Sets the feed link.
167      * <p>
168      * @param link the feed link to set, <b>null</b> if none.
169      *
170      */
171     public void setLink(String link) {
172         _link = link;
173     }
174 
175     /***
176      * Returns the feed description.
177      * <p>
178      * @return the feed description, <b>null</b> if none.
179      *
180      */
181     public String getDescription() {
182         return _description;
183     }
184 
185     /***
186      * Sets the feed description.
187      * <p>
188      * @param description the feed description to set, <b>null</b> if none.
189      *
190      */
191     public void setDescription(String description) {
192         _description = description;
193     }
194 
195     /***
196      * Returns the feed published date.
197      * <p>
198      * This method is a convenience method, it maps to the Dublin Core module date.
199      * <p>
200      * @return the feed published date, <b>null</b> if none.
201      *
202      */
203     public Date getPublishedDate() {
204         return getDCModule().getDate();
205     }
206 
207     /***
208      * Sets the feed published date.
209      * <p>
210      * This method is a convenience method, it maps to the Dublin Core module date.
211      * <p>
212      * @param publishedDate the feed published date to set, <b>null</b> if none.
213      *
214      */
215     public void setPublishedDate(Date publishedDate) {
216         getDCModule().setDate(publishedDate);
217     }
218 
219     /***
220      * Returns the feed author.
221      * <p>
222      * This method is a convenience method, it maps to the Dublin Core module creator.
223      * <p>
224      * @return the feed author, <b>null</b> if none.
225      *
226      */
227     public String getAuthor() {
228         return getDCModule().getCreator();
229     }
230 
231     /***
232      * Sets the feed author.
233      * <p>
234      * This method is a convenience method, it maps to the Dublin Core module creator.
235      * <p>
236      * @param author the feed author to set, <b>null</b> if none.
237      *
238      */
239     public void setAuthor(String author) {
240         getDCModule().setCreator(author);
241     }
242 
243     /***
244      * Returns the feed copyright.
245      * <p>
246      * This method is a convenience method, it maps to the Dublin Core module rights.
247      * <p>
248      * @return the feed copyright, <b>null</b> if none.
249      *
250      */
251     public String getCopyright() {
252         return getDCModule().getRights();
253     }
254 
255     /***
256      * Sets the feed copyright.
257      * <p>
258      * This method is a convenience method, it maps to the Dublin Core module rights.
259      * <p>
260      * @param copyright the feed copyright to set, <b>null</b> if none.
261      *
262      */
263     public void setCopyright(String copyright) {
264         getDCModule().setRights(copyright);
265     }
266 
267     /***
268      * Returns the feed image.
269      * <p>
270      * @return the feed image, <b>null</b> if none.
271      *
272      */
273     public SyndImageI getImage() {
274         return _image;
275     }
276 
277     /***
278      * Sets the feed image.
279      * <p>
280      * @param image the feed image to set, <b>null</b> if none.
281      *
282      */
283     public void setImage(SyndImageI image) {
284         _image = image;
285     }
286 
287     /***
288      * Returns the feed categories.
289      * <p>
290      * This method is a convenience method, it maps to the Dublin Core module subjects.
291      * <p>
292      * @return a list of SyndCategory elements with the feed categories,
293      *         an empty list if none.
294      *
295      */
296     public List getCategories() {
297         return new SyndCategoryListFacade(getDCModule().getSubjects());
298     }
299 
300     /***
301      * Sets the feed categories.
302      * <p>
303      * This method is a convenience method, it maps to the Dublin Core module subjects.
304      * <p>
305      * @param categories the list of SyndCategory elements with the feed categories to set,
306      *        an empty list or <b>null</b> if none.
307      *
308      */
309     public void setCategories(List categories) {
310         getDCModule().setSubjects(SyndCategoryListFacade.convertElementsSyndCategoryToSubject(categories));
311     }
312 
313     /***
314      * Returns the feed entries.
315      * <p>
316      * @return a list of SyndEntry elements with the feed entries,
317      *         an empty list if none.
318      *
319      */
320     public List getEntries() {
321         return (_entries==null) ? (_entries=new ArrayList()) : _entries;
322     }
323 
324     /***
325      * Sets the feed entries.
326      * <p>
327      * @param entries the list of SyndEntry elements with the feed entries to set,
328      *        an empty list or <b>null</b> if none.
329      *
330      */
331     public void setEntries(List entries) {
332         _entries = entries;
333     }
334 
335     /***
336      * Returns the feed language.
337      * <p>
338      * This method is a convenience method, it maps to the Dublin Core module language.
339      * <p>
340      * @return the feed language, <b>null</b> if none.
341      *
342      */
343     public String getLanguage() {
344         return getDCModule().getLanguage();
345     }
346 
347     /***
348      * Sets the feed language.
349      * <p>
350      * This method is a convenience method, it maps to the Dublin Core module language.
351      * <p>
352      * @param language the feed language to set, <b>null</b> if none.
353      *
354      */
355     public void setLanguage(String language) {
356         getDCModule().setLanguage(language);
357     }
358 
359     /***
360      * Returns the feed modules.
361      * <p>
362      * @return a list of Module elements with the feed modules,
363      *         an empty list if none.
364      *
365      */
366     public List getModules() {
367         return (_modules==null) ? (_modules=new ArrayList()) : _modules;
368     }
369 
370     /***
371      * Sets the feed modules.
372      * <p>
373      * @param modules the list of Module elements with the feed modules to set,
374      *        an empty list or <b>null</b> if none.
375      *
376      */
377     public void setModules(List modules) {
378         _modules = modules;
379     }
380 
381     /***
382      * Returns the Dublin Core module of the feed.
383      * @return the DC module, it's never <b>null</b>
384      *
385      */
386     private DCModuleI getDCModule() {
387         DCModuleI dcModule = null;
388         List modules = getModules();
389         for (int i=0;dcModule==null && i<modules.size();i++) {
390             ModuleI module = (ModuleI) modules.get(i);
391             if (module.getUri().equals(DCModuleI.URI)) {
392                 dcModule = (DCModuleI) module;
393             }
394         }
395         if (dcModule==null) {
396             dcModule = new DCModule();
397             modules.add(dcModule);
398         }
399         return dcModule;
400     }
401 
402 }