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.impl;
18  
19  import com.sun.syndication.feed.WireFeed;
20  import com.sun.syndication.feed.module.DCModule;
21  import com.sun.syndication.feed.rss.Channel;
22  import com.sun.syndication.feed.rss.Content;
23  import com.sun.syndication.feed.rss.Description;
24  import com.sun.syndication.feed.rss.Image;
25  import com.sun.syndication.feed.rss.Item;
26  import com.sun.syndication.feed.synd.SyndFeed;
27  import com.sun.syndication.feed.synd.SyndContent;
28  import com.sun.syndication.feed.synd.SyndEntry;
29  import com.sun.syndication.feed.synd.SyndImage;
30  import com.sun.syndication.feed.synd.SyndContentImpl;
31  import com.sun.syndication.feed.synd.SyndPerson;
32  
33  import java.util.*;
34  
35  /**
36   */
37  public class ConverterForRSS091Userland extends ConverterForRSS090 {
38  
39      public ConverterForRSS091Userland() {
40          this("rss_0.91U");
41      }
42  
43      protected ConverterForRSS091Userland(String type) {
44          super(type);
45      }
46  
47      public void copyInto(WireFeed feed,SyndFeed syndFeed) {
48          Channel channel = (Channel) feed;
49          super.copyInto(channel,syndFeed);
50          syndFeed.setLanguage(channel.getLanguage());        //c
51          syndFeed.setCopyright(channel.getCopyright());      //c
52          Date pubDate = channel.getPubDate();
53          if (pubDate!=null) {
54              syndFeed.setPublishedDate(pubDate);  //c
55          } else if (channel.getLastBuildDate() != null) {
56              syndFeed.setPublishedDate(channel.getLastBuildDate());  //c
57          }
58          
59  
60          String author = channel.getManagingEditor();
61          if (author!=null) {    
62              List creators = ((DCModule) syndFeed.getModule(DCModule.URI)).getCreators();
63              if (!creators.contains(author)) {
64                  Set s = new HashSet(); // using a set to remove duplicates
65                  s.addAll(creators);    // DC creators
66                  s.add(author);         // feed native author
67                  creators.clear();
68                  creators.addAll(s);
69              }
70          }
71  
72      }
73  
74      protected SyndImage createSyndImage(Image rssImage) {
75          SyndImage syndImage = super.createSyndImage(rssImage);
76          syndImage.setDescription(rssImage.getDescription());
77          return syndImage;
78      }
79  
80      // for rss -> synd
81      // rss.content -> synd.content
82      // rss.description -> synd.description
83      
84      protected SyndEntry createSyndEntry(Item item) {
85          SyndEntry syndEntry = super.createSyndEntry(item);
86          Description desc = item.getDescription();
87          if (desc != null) {
88              SyndContent descContent = new SyndContentImpl();
89              descContent.setType(desc.getType());
90              descContent.setValue(desc.getValue());
91              syndEntry.setDescription(descContent);
92          }
93          Content cont = item.getContent();
94          if (cont != null) {
95              SyndContent content = new SyndContentImpl();
96              content.setType(cont.getType());
97              content.setValue(cont.getValue());
98              List syndContents = new ArrayList();
99              syndContents.add(content);
100             syndEntry.setContents(syndContents);
101         }
102         return syndEntry;
103     }
104 
105     protected WireFeed createRealFeed(String type,SyndFeed syndFeed) {
106         Channel channel = (Channel) super.createRealFeed(type,syndFeed);
107         channel.setLanguage(syndFeed.getLanguage());        //c
108         channel.setCopyright(syndFeed.getCopyright());      //c
109         channel.setPubDate(syndFeed.getPublishedDate());    //c        
110         if (syndFeed.getAuthors()!=null && syndFeed.getAuthors().size() > 0) {
111             SyndPerson author = (SyndPerson)syndFeed.getAuthors().get(0);
112             channel.setManagingEditor(author.getName());  
113         }        
114         return channel;
115     }
116 
117     protected Image createRSSImage(SyndImage sImage) {
118         Image image = super.createRSSImage(sImage);
119         image.setDescription(sImage.getDescription());
120         return image;
121     }
122 
123     // for synd -> rss
124     // synd.content -> rss.content
125     // synd.description -> rss.description
126     
127     protected Item createRSSItem(SyndEntry sEntry) {
128         Item item = super.createRSSItem(sEntry);
129 
130         SyndContent sContent = sEntry.getDescription();
131         if (sContent!=null) {
132             item.setDescription(createItemDescription(sContent));
133         }
134         List contents = sEntry.getContents();
135         if (contents != null && contents.size() > 0) {
136             SyndContent syndContent = (SyndContent)contents.get(0);
137             Content cont = new Content();
138             cont.setValue(syndContent.getValue());
139             cont.setType(syndContent.getType());
140             item.setContent(cont);
141         }
142         return item;
143     }
144 
145     protected Description createItemDescription(SyndContent sContent) {
146         Description desc = new Description();
147         desc.setValue(sContent.getValue());
148         desc.setType(sContent.getType());
149         return desc;
150     }
151 
152 
153 }