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.atom.Content;
21  import com.sun.syndication.feed.atom.Entry;
22  import com.sun.syndication.feed.atom.Feed;
23  import com.sun.syndication.feed.atom.Link;
24  import com.sun.syndication.feed.atom.Person;
25  import com.sun.syndication.io.impl.ModuleUtils;
26  import com.sun.syndication.feed.synd.SyndFeedI;
27  import com.sun.syndication.feed.synd.Converter;
28  import com.sun.syndication.feed.synd.SyndEntryI;
29  import com.sun.syndication.feed.synd.SyndContent;
30  import com.sun.syndication.feed.synd.SyndEntry;
31  import com.sun.syndication.feed.synd.SyndContentI;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.Date;
36  
37  /***
38   */
39  public class ConverterForAtom03 implements Converter {
40  
41      public String getType() {
42          return "atom_0.3";
43      }
44  
45      public void copyInto(WireFeed feed,SyndFeedI syndFeed) {
46          Feed aFeed = (Feed) feed;
47          syndFeed.setTitle(aFeed.getTitle());
48  
49          Link link = (Link) aFeed.getAlternateLinks().get(0);
50          syndFeed.setLink(link.getHref());
51  
52          Content info = aFeed.getInfo();
53          if (info!=null) {
54              syndFeed.setDescription(info.getValue());
55          }
56  
57          syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
58  
59          List aEntries = aFeed.getEntries();
60          if (aEntries!=null) {
61              syndFeed.setEntries(createSyndEntries(aEntries));
62          }
63  
64          // Core Atom language/author/copyright/modified elements have precedence
65          // over DC equivalent info.
66  
67          String language = aFeed.getLanguage();
68          if (language!=null) {
69              syndFeed.setLanguage(language);
70          }
71  
72          Person author = aFeed.getAuthor();
73          if (author!=null && author.getName()!=null) {
74              syndFeed.setAuthor(author.getName());
75          }
76  
77          String copyright = aFeed.getCopyright();
78          if (copyright!=null) {
79              syndFeed.setCopyright(copyright);
80          }
81  
82          Date date = aFeed.getModified();
83          if (date!=null) {
84              syndFeed.setPublishedDate(date);
85          }
86  
87      }
88  
89      protected List createSyndEntries(List atomEntries) {
90          List syndEntries = new ArrayList();
91          for (int i=0;i<atomEntries.size();i++) {
92              syndEntries.add(createSyndEntry((Entry) atomEntries.get(i)));
93          }
94          return syndEntries;
95      }
96  
97      protected SyndEntryI createSyndEntry(Entry entry) {
98          SyndEntryI syndEntry = new SyndEntry();
99  
100         syndEntry.setTitle(entry.getTitle());
101 
102         Link link = (Link) entry.getAlternateLinks().get(0);
103         syndEntry.setLink(link.getHref());
104 
105         Content content = entry.getSummary();
106         if (content==null) {
107             List contents = entry.getContents();
108             if (contents!=null && contents.size()>0) {
109                 content = (Content) contents.get(0);
110             }
111         }
112 
113         List contents = entry.getContents();
114         if (contents.size()>0) {
115             List sContents = new ArrayList();
116             for (int i=0;i<contents.size();i++) {
117                 content = (Content) contents.get(i);
118                 SyndContentI sContent = new SyndContent();
119                 sContent.setType(content.getType());
120                 sContent.setValue(content.getValue());
121                 sContents.add(sContent);
122             }
123             syndEntry.setContents(sContents);
124         }
125 
126         syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
127 
128         // Core Atom author/modified elements have precedence
129         // over DC equivalent info.
130 
131         Person author = entry.getAuthor();
132         if (author!=null && author.getName()!=null) {
133             syndEntry.setAuthor(author.getName());
134         }
135 
136         Date date = entry.getModified();
137         if (date!=null) {
138             syndEntry.setPublishedDate(date);
139         }
140 
141 
142         return syndEntry;
143     }
144 
145     public WireFeed createRealFeed(SyndFeedI syndFeed) {
146         Feed aFeed = new Feed(getType());
147 
148         aFeed.setTitle(syndFeed.getTitle());
149 
150         String sLink = syndFeed.getLink();
151         if (sLink!=null) {
152             Link link = new Link();
153             link.setRel(Link.ALTERNATE);
154             link.setHref(sLink);
155             List list = new ArrayList();
156             list.add(link);
157             aFeed.setAlternateLinks(list);
158         }
159 
160         String sDesc = syndFeed.getDescription();
161         if (sDesc!=null) {
162             Content info = new Content();
163             info.setValue(sDesc);
164             aFeed.setInfo(info);
165         }
166 
167         aFeed.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
168 
169         aFeed.setLanguage(syndFeed.getLanguage());
170 
171         String sAuthor = syndFeed.getAuthor();
172         if (sAuthor!=null) {
173             Person person = new Person();
174             person.setName(sAuthor);
175             aFeed.setAuthor(person);
176         }
177 
178         aFeed.setCopyright(syndFeed.getCopyright());
179 
180         aFeed.setModified(syndFeed.getPublishedDate());
181 
182         List sEntries = syndFeed.getEntries();
183         if (sEntries!=null) {
184             aFeed.setEntries(createAtomEntries(sEntries));
185         }
186 
187         return aFeed;
188     }
189 
190 
191     protected List createAtomEntries(List syndEntries) {
192         List atomEntries = new ArrayList();
193         for (int i=0;i<syndEntries.size();i++) {
194             atomEntries.add(createAtomEntry((SyndEntryI)syndEntries.get(i)));
195         }
196         return atomEntries;
197     }
198 
199     protected Entry createAtomEntry(SyndEntryI sEntry) {
200         Entry aEntry = new Entry();
201 
202         aEntry.setTitle(sEntry.getTitle());
203 
204         String sLink = sEntry.getLink();
205         if (sLink!=null) {
206             Link link = new Link();
207             link.setRel(Link.ALTERNATE);
208             link.setHref(sLink);
209             List list = new ArrayList();
210             list.add(link);
211             aEntry.setAlternateLinks(list);
212         }
213 
214         SyndContentI sContent = sEntry.getDescription();
215         if (sContent!=null) {
216             Content content = new Content();
217             content.setType(sContent.getType());
218             content.setValue(sContent.getValue());
219             content.setMode(Content.ESCAPED);
220             aEntry.setSummary(content);
221         }
222 
223         List contents = sEntry.getContents();
224         if (contents.size()>0) {
225             List aContents = new ArrayList();
226             for (int i=0;i<contents.size();i++) {
227                 sContent = (SyndContent) contents.get(i);
228                 Content content = new Content();
229                 content.setType(sContent.getType());
230                 content.setValue(sContent.getValue());
231                 content.setMode(Content.ESCAPED);
232                 aContents.add(content);
233 
234             }
235             aEntry.setContents(aContents);
236         }
237 
238         aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
239 
240         String sAuthor = sEntry.getAuthor();
241         if (sAuthor!=null) {
242             Person person = new Person();
243             person.setName(sAuthor);
244             aEntry.setAuthor(person);
245         }
246 
247         aEntry.setModified(sEntry.getPublishedDate());
248 
249         return aEntry;
250     }
251 
252 }