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.synd.impl.SyndCopyFrom;
23  import com.sun.syndication.feed.module.*;
24  import com.sun.syndication.feed.module.impl.ModuleUtils;
25  
26  import java.util.*;
27  
28  /***
29   * Bean for all types of feeds.
30   * <p>
31   * It handles all RSS versions and Atom 0.3, it normalizes all info, it may lose information.
32   * <p>
33   * @author Alejandro Abdelnur
34   *
35   */
36  public class SyndFeed extends ObjectBean implements SyndFeedI {
37      private String _encoding;
38      private String _title;
39      private String _feedType;
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(WireFeed feed) {
74          super(SyndFeedI.class);
75          if (feed!=null) {
76              _feedType = feed.getFeedType();
77              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 = 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 charset encoding of a the feed. This is not set by Rome parsers.
137      * <p>
138      * @return the charset encoding of the feed.
139      *
140      */
141     public String getEncoding() {
142         return _encoding;
143     }
144 
145     /***
146      * Sets the charset encoding of a the feed. This is not set by Rome parsers.
147      * <p>
148      * @param encoding the charset encoding of the feed.
149      *
150      */
151     public void setEncoding(String encoding) {
152         _encoding = encoding;
153     }
154 
155     /***
156      * Returns the feed title.
157      * <p>
158      * @return the feed title, <b>null</b> if none.
159      *
160      */
161     public String getTitle() {
162         return _title;
163     }
164 
165     /***
166      * Sets the feed title.
167      * <p>
168      * @param title the feed title to set, <b>null</b> if none.
169      *
170      */
171     public void setTitle(String title) {
172         _title = title;
173     }
174 
175     /***
176      * Returns the feed link.
177      * <p>
178      * @return the feed link, <b>null</b> if none.
179      *
180      */
181     public String getLink() {
182         return _link;
183     }
184 
185     /***
186      * Sets the feed link.
187      * <p>
188      * @param link the feed link to set, <b>null</b> if none.
189      *
190      */
191     public void setLink(String link) {
192         _link = link;
193     }
194 
195     /***
196      * Returns the feed description.
197      * <p>
198      * @return the feed description, <b>null</b> if none.
199      *
200      */
201     public String getDescription() {
202         return _description;
203     }
204 
205     /***
206      * Sets the feed description.
207      * <p>
208      * @param description the feed description to set, <b>null</b> if none.
209      *
210      */
211     public void setDescription(String description) {
212         _description = description;
213     }
214 
215     /***
216      * Returns the feed published date.
217      * <p>
218      * This method is a convenience method, it maps to the Dublin Core module date.
219      * <p>
220      * @return the feed published date, <b>null</b> if none.
221      *
222      */
223     public Date getPublishedDate() {
224         return getDCModule().getDate();
225     }
226 
227     /***
228      * Sets the feed published date.
229      * <p>
230      * This method is a convenience method, it maps to the Dublin Core module date.
231      * <p>
232      * @param publishedDate the feed published date to set, <b>null</b> if none.
233      *
234      */
235     public void setPublishedDate(Date publishedDate) {
236         getDCModule().setDate(publishedDate);
237     }
238 
239     /***
240      * Returns the feed author.
241      * <p>
242      * This method is a convenience method, it maps to the Dublin Core module creator.
243      * <p>
244      * @return the feed author, <b>null</b> if none.
245      *
246      */
247     public String getAuthor() {
248         return getDCModule().getCreator();
249     }
250 
251     /***
252      * Sets the feed author.
253      * <p>
254      * This method is a convenience method, it maps to the Dublin Core module creator.
255      * <p>
256      * @param author the feed author to set, <b>null</b> if none.
257      *
258      */
259     public void setAuthor(String author) {
260         getDCModule().setCreator(author);
261     }
262 
263     /***
264      * Returns the feed copyright.
265      * <p>
266      * This method is a convenience method, it maps to the Dublin Core module rights.
267      * <p>
268      * @return the feed copyright, <b>null</b> if none.
269      *
270      */
271     public String getCopyright() {
272         return getDCModule().getRights();
273     }
274 
275     /***
276      * Sets the feed copyright.
277      * <p>
278      * This method is a convenience method, it maps to the Dublin Core module rights.
279      * <p>
280      * @param copyright the feed copyright to set, <b>null</b> if none.
281      *
282      */
283     public void setCopyright(String copyright) {
284         getDCModule().setRights(copyright);
285     }
286 
287     /***
288      * Returns the feed image.
289      * <p>
290      * @return the feed image, <b>null</b> if none.
291      *
292      */
293     public SyndImageI getImage() {
294         return _image;
295     }
296 
297     /***
298      * Sets the feed image.
299      * <p>
300      * @param image the feed image to set, <b>null</b> if none.
301      *
302      */
303     public void setImage(SyndImageI image) {
304         _image = image;
305     }
306 
307     /***
308      * Returns the feed categories.
309      * <p>
310      * This method is a convenience method, it maps to the Dublin Core module subjects.
311      * <p>
312      * @return a list of SyndCategory elements with the feed categories,
313      *         an empty list if none.
314      *
315      */
316     public List getCategories() {
317         return new SyndCategoryListFacade(getDCModule().getSubjects());
318     }
319 
320     /***
321      * Sets the feed categories.
322      * <p>
323      * This method is a convenience method, it maps to the Dublin Core module subjects.
324      * <p>
325      * @param categories the list of SyndCategory elements with the feed categories to set,
326      *        an empty list or <b>null</b> if none.
327      *
328      */
329     public void setCategories(List categories) {
330         getDCModule().setSubjects(SyndCategoryListFacade.convertElementsSyndCategoryToSubject(categories));
331     }
332 
333     /***
334      * Returns the feed entries.
335      * <p>
336      * @return a list of SyndEntry elements with the feed entries,
337      *         an empty list if none.
338      *
339      */
340     public List getEntries() {
341         return (_entries==null) ? (_entries=new ArrayList()) : _entries;
342     }
343 
344     /***
345      * Sets the feed entries.
346      * <p>
347      * @param entries the list of SyndEntry elements with the feed entries to set,
348      *        an empty list or <b>null</b> if none.
349      *
350      */
351     public void setEntries(List entries) {
352         _entries = entries;
353     }
354 
355     /***
356      * Returns the feed language.
357      * <p>
358      * This method is a convenience method, it maps to the Dublin Core module language.
359      * <p>
360      * @return the feed language, <b>null</b> if none.
361      *
362      */
363     public String getLanguage() {
364         return getDCModule().getLanguage();
365     }
366 
367     /***
368      * Sets the feed language.
369      * <p>
370      * This method is a convenience method, it maps to the Dublin Core module language.
371      * <p>
372      * @param language the feed language to set, <b>null</b> if none.
373      *
374      */
375     public void setLanguage(String language) {
376         getDCModule().setLanguage(language);
377     }
378 
379     /***
380      * Returns the feed modules.
381      * <p>
382      * @return a list of Module elements with the feed modules,
383      *         an empty list if none.
384      *
385      */
386     public List getModules() {
387         return (_modules==null) ? (_modules=new ArrayList()) : _modules;
388     }
389 
390 
391     /***
392      * Sets the feed modules.
393      * <p>
394      * @param modules the list of Module elements with the feed modules to set,
395      *        an empty list or <b>null</b> if none.
396      *
397      */
398     public void setModules(List modules) {
399         _modules = modules;
400     }
401 
402     /***
403      * Returns the module identified by a given URI.
404      * <p>
405      * @param uri the URI of the Module.
406      * @return The module with the given URI, <b>null</b> if none.
407      */
408     public ModuleI getModule(String uri) {
409         return ModuleUtils.getModule(getModules(),uri);
410     }
411 
412     /***
413      * Returns the Dublin Core module of the feed.
414      * @return the DC module, it's never <b>null</b>
415      *
416      */
417     private DCModuleI getDCModule() {
418         DCModuleI dcModule = (DCModuleI) getModule(DCModuleI.URI);
419         if (dcModule==null) {
420             dcModule = new DCModule();
421             getModules().add(dcModule);
422         }
423         return dcModule;
424     }
425 
426     public Class getInterface() {
427         return SyndFeedI.class;
428     }
429 
430     public void copyFrom(Object obj) {
431         SYND_COPY_FROM.copy(this,obj);
432     }
433 
434 
435     // TODO We need to find out how to refactor this one in a nice reusable way.
436 
437     private static final SyndCopyFrom SYND_COPY_FROM;
438 
439     static {
440         Map basePropInterfaceMap = new HashMap();
441         basePropInterfaceMap.put("feedType",String.class);
442         basePropInterfaceMap.put("encoding",String.class);
443         basePropInterfaceMap.put("title",String.class);
444         basePropInterfaceMap.put("link",String.class);
445         basePropInterfaceMap.put("description",String.class);
446         basePropInterfaceMap.put("image",SyndImageI.class);
447         basePropInterfaceMap.put("entries",SyndEntryI.class);
448         basePropInterfaceMap.put("modules",ModuleI.class);
449 
450         Map basePropClassImplMap = new HashMap();
451         basePropClassImplMap.put(SyndEntryI.class,SyndEntry.class);
452         basePropClassImplMap.put(SyndImageI.class,SyndImage.class);
453         basePropClassImplMap.put(DCModuleI.class,DCModule.class);
454         basePropClassImplMap.put(SyModuleI.class,SyModule.class);
455 
456         SYND_COPY_FROM = new SyndCopyFrom(SyndFeedI.class,basePropInterfaceMap,basePropClassImplMap);
457     }
458 
459 }