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.AbstractFeed;
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 _link;
41      private String _description;
42      private SyndImageI _image;
43      private List _entries;
44      private List _modules;
45  
46      private static final Converters CONVERTERS = new Converters();
47  
48      /***
49       * Returns the real feed types the SyndFeed supports when converting from and to.
50       * <p>
51       * @return the real feed type supported.
52       */
53      public List getSupportedFeedTypes() {
54          return CONVERTERS.getSupportedFeedTypes();
55      }
56  
57      /***
58       * Default constructor. All properties are set to <b>null</b>.
59       * <p>
60       *
61       */
62      public SyndFeed() {
63          this(null);
64      }
65  
66      /***
67       * Creates a SyndFeed an populates all its properties out of the
68       * given RSS Channel or Atom Feed properties.
69       * <p>
70       * @param feed the RSS Channel or the Atom Feed to populate the properties from.
71       *
72       */
73      public SyndFeed(AbstractFeed feed) {
74          if (feed!=null) {
75              String type = feed.getType();
76              Converter converter = (Converter) CONVERTERS.getConverter(type);
77              if (converter==null) {
78                  throw new IllegalArgumentException("Invalid feed type ["+type+"]");
79              }
80              converter.copyInto(feed,this);
81          }
82      }
83  
84      /***
85       * Creates a real feed of the given type containing the information of the SyndFeed.
86       * <p>
87       * @param type type of the real feed to create.
88       * @return the real feed.
89       *
90       */
91      public AbstractFeed createRealFeed(String type) {
92          Converter converter = (Converter) CONVERTERS.getConverter(type);
93          if (converter==null) {
94              throw new IllegalArgumentException("Invalid feed type ["+type+"]");
95          }
96          return converter.createRealFeed(this);
97      }
98  
99      /***
100      * Returns the feed title.
101      * <p>
102      * @return the feed title, <b>null</b> if none.
103      *
104      */
105     public String getTitle() {
106         return _title;
107     }
108 
109     /***
110      * Sets the feed title.
111      * <p>
112      * @param title the feed title to set, <b>null</b> if none.
113      *
114      */
115     public void setTitle(String title) {
116         _title = title;
117     }
118 
119     /***
120      * Returns the feed link.
121      * <p>
122      * @return the feed link, <b>null</b> if none.
123      *
124      */
125     public String getLink() {
126         return _link;
127     }
128 
129     /***
130      * Sets the feed link.
131      * <p>
132      * @param link the feed link to set, <b>null</b> if none.
133      *
134      */
135     public void setLink(String link) {
136         _link = link;
137     }
138 
139     /***
140      * Returns the feed description.
141      * <p>
142      * @return the feed description, <b>null</b> if none.
143      *
144      */
145     public String getDescription() {
146         return _description;
147     }
148 
149     /***
150      * Sets the feed description.
151      * <p>
152      * @param description the feed description to set, <b>null</b> if none.
153      *
154      */
155     public void setDescription(String description) {
156         _description = description;
157     }
158 
159     /***
160      * Returns the feed published date.
161      * <p>
162      * This method is a convenience method, it maps to the Dublin Core module date.
163      * <p>
164      * @return the feed published date, <b>null</b> if none.
165      *
166      */
167     public Date getPublishedDate() {
168         return getDCModule().getDate();
169     }
170 
171     /***
172      * Sets the feed published date.
173      * <p>
174      * This method is a convenience method, it maps to the Dublin Core module date.
175      * <p>
176      * @param publishedDate the feed published date to set, <b>null</b> if none.
177      *
178      */
179     public void setPublishedDate(Date publishedDate) {
180         getDCModule().setDate(publishedDate);
181     }
182 
183     /***
184      * Returns the feed author.
185      * <p>
186      * This method is a convenience method, it maps to the Dublin Core module creator.
187      * <p>
188      * @return the feed author, <b>null</b> if none.
189      *
190      */
191     public String getAuthor() {
192         return getDCModule().getCreator();
193     }
194 
195     /***
196      * Sets the feed author.
197      * <p>
198      * This method is a convenience method, it maps to the Dublin Core module creator.
199      * <p>
200      * @param author the feed author to set, <b>null</b> if none.
201      *
202      */
203     public void setAuthor(String author) {
204         getDCModule().setCreator(author);
205     }
206 
207     /***
208      * Returns the feed copyright.
209      * <p>
210      * This method is a convenience method, it maps to the Dublin Core module rights.
211      * <p>
212      * @return the feed copyright, <b>null</b> if none.
213      *
214      */
215     public String getCopyright() {
216         return getDCModule().getRights();
217     }
218 
219     /***
220      * Sets the feed copyright.
221      * <p>
222      * This method is a convenience method, it maps to the Dublin Core module rights.
223      * <p>
224      * @param copyright the feed copyright to set, <b>null</b> if none.
225      *
226      */
227     public void setCopyright(String copyright) {
228         getDCModule().setRights(copyright);
229     }
230 
231     /***
232      * Returns the feed image.
233      * <p>
234      * @return the feed image, <b>null</b> if none.
235      *
236      */
237     public SyndImageI getImage() {
238         return _image;
239     }
240 
241     /***
242      * Sets the feed image.
243      * <p>
244      * @param image the feed image to set, <b>null</b> if none.
245      *
246      */
247     public void setImage(SyndImageI image) {
248         _image = image;
249     }
250 
251     /***
252      * Returns the feed categories.
253      * <p>
254      * This method is a convenience method, it maps to the Dublin Core module subjects.
255      * <p>
256      * @return a list of SyndCategory elements with the feed categories,
257      *         an empty list if none.
258      *
259      */
260     public List getCategories() {
261         return new SyndCategoryListFacade(getDCModule().getSubjects());
262     }
263 
264     /***
265      * Sets the feed categories.
266      * <p>
267      * This method is a convenience method, it maps to the Dublin Core module subjects.
268      * <p>
269      * @param categories the list of SyndCategory elements with the feed categories to set,
270      *        an empty list or <b>null</b> if none.
271      *
272      */
273     public void setCategories(List categories) {
274         getDCModule().setSubjects(SyndCategoryListFacade.convertElementsSyndCategoryToSubject(categories));
275     }
276 
277     /***
278      * Returns the feed entries.
279      * <p>
280      * @return a list of SyndEntry elements with the feed entries,
281      *         an empty list if none.
282      *
283      */
284     public List getEntries() {
285         return (_entries==null) ? (_entries=new ArrayList()) : _entries;
286     }
287 
288     /***
289      * Sets the feed entries.
290      * <p>
291      * @param entries the list of SyndEntry elements with the feed entries to set,
292      *        an empty list or <b>null</b> if none.
293      *
294      */
295     public void setEntries(List entries) {
296         _entries = entries;
297     }
298 
299     /***
300      * Returns the feed language.
301      * <p>
302      * This method is a convenience method, it maps to the Dublin Core module language.
303      * <p>
304      * @return the feed language, <b>null</b> if none.
305      *
306      */
307     public String getLanguage() {
308         return getDCModule().getLanguage();
309     }
310 
311     /***
312      * Sets the feed language.
313      * <p>
314      * This method is a convenience method, it maps to the Dublin Core module language.
315      * <p>
316      * @param language the feed language to set, <b>null</b> if none.
317      *
318      */
319     public void setLanguage(String language) {
320         getDCModule().setLanguage(language);
321     }
322 
323     /***
324      * Returns the feed modules.
325      * <p>
326      * @return a list of Module elements with the feed modules,
327      *         an empty list if none.
328      *
329      */
330     public List getModules() {
331         return (_modules==null) ? (_modules=new ArrayList()) : _modules;
332     }
333 
334     /***
335      * Sets the feed modules.
336      * <p>
337      * @param modules the list of Module elements with the feed modules to set,
338      *        an empty list or <b>null</b> if none.
339      *
340      */
341     public void setModules(List modules) {
342         _modules = modules;
343     }
344 
345     /***
346      * Returns the Dublin Core module of the feed.
347      * @return the DC module, it's never <b>null</b>
348      *
349      */
350     private DCModuleI getDCModule() {
351         DCModuleI dcModule = null;
352         List modules = getModules();
353         for (int i=0;dcModule==null && i<modules.size();i++) {
354             ModuleI module = (ModuleI) modules.get(i);
355             if (module.getUri().equals(DCModuleI.URI)) {
356                 dcModule = (DCModuleI) module;
357             }
358         }
359         if (dcModule==null) {
360             dcModule = new DCModule();
361             modules.add(dcModule);
362         }
363         return dcModule;
364     }
365 
366 }