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.feed.module.impl.ModuleUtils;
26  import com.sun.syndication.feed.synd.SyndFeed;
27  import com.sun.syndication.feed.synd.Converter;
28  import com.sun.syndication.feed.synd.SyndEntry;
29  import com.sun.syndication.feed.synd.SyndContentImpl;
30  import com.sun.syndication.feed.synd.SyndEntryImpl;
31  import com.sun.syndication.feed.synd.SyndContent;
32  import com.sun.syndication.feed.synd.SyndPerson;
33  import com.sun.syndication.feed.synd.SyndPersonImpl;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  import java.util.Date;
38  import java.util.Iterator;
39  
40  /***
41   */
42  public class ConverterForAtom03 implements Converter {
43      private String _type;
44  
45      public ConverterForAtom03() {
46          this("atom_0.3");
47      }
48  
49      protected ConverterForAtom03(String type) {
50          _type = type;
51      }
52  
53      public String getType() {
54          return _type;
55      }
56  
57      public void copyInto(WireFeed feed,SyndFeed syndFeed) {
58          Feed aFeed = (Feed) feed;
59  
60          syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
61  
62          syndFeed.setEncoding(aFeed.getEncoding());
63  
64          syndFeed.setUri(aFeed.getId());
65  
66          syndFeed.setTitle(aFeed.getTitle());
67  
68          String linkHref = null;
69          if (aFeed.getAlternateLinks().size() > 0) {
70              linkHref = ((Link) aFeed.getAlternateLinks().get(0)).getHref();
71          }
72          syndFeed.setLink(linkHref);
73  
74          Content tagline = aFeed.getTagline();
75          if (tagline!=null) {
76              syndFeed.setDescription(tagline.getValue());
77          }
78  
79  
80          List aEntries = aFeed.getEntries();
81          if (aEntries!=null) {
82              syndFeed.setEntries(createSyndEntries(aEntries));
83          }
84  
85          // Core Atom language/author/copyright/modified elements have precedence
86          // over DC equivalent info.
87  
88          String language = aFeed.getLanguage();
89          if (language!=null) {
90              syndFeed.setLanguage(language);
91          }
92  
93          List authors = aFeed.getAuthors();
94          if (authors!=null && authors.size() > 0) {
95              syndFeed.setAuthors(createSyndPersons(authors));
96          }
97  
98          String copyright = aFeed.getCopyright();
99          if (copyright!=null) {
100             syndFeed.setCopyright(copyright);
101         }
102 
103         Date date = aFeed.getModified();
104         if (date!=null) {
105             syndFeed.setPublishedDate(date);
106         }
107 
108     }
109 
110     protected List createSyndEntries(List atomEntries) {
111         List syndEntries = new ArrayList();
112         for (int i=0;i<atomEntries.size();i++) {
113             syndEntries.add(createSyndEntry((Entry) atomEntries.get(i)));
114         }
115         return syndEntries;
116     }
117 
118     protected SyndEntry createSyndEntry(Entry entry) {
119         SyndEntry syndEntry = new SyndEntryImpl();
120         syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
121 
122         syndEntry.setTitle(entry.getTitle());
123 
124         String linkHref = null;
125         if (entry.getAlternateLinks().size() > 0) {
126             linkHref = ((Link) entry.getAlternateLinks().get(0)).getHref();
127         }
128         syndEntry.setLink(linkHref);
129 
130 
131         String id = entry.getId();
132         if (id!=null) {
133             syndEntry.setUri(entry.getId());
134         }
135         else {
136             syndEntry.setUri(syndEntry.getLink());
137         }
138 
139         Content content = entry.getSummary();
140         if (content==null) {
141             List contents = entry.getContents();
142             if (contents!=null && contents.size()>0) {
143                 content = (Content) contents.get(0);
144             }
145         }
146         if (content!=null) {
147             SyndContent sContent = new SyndContentImpl();
148             sContent.setType(content.getType());
149             sContent.setValue(content.getValue());
150             syndEntry.setDescription(sContent);
151         }
152 
153         List contents = entry.getContents();
154         if (contents.size()>0) {
155             List sContents = new ArrayList();
156             for (int i=0;i<contents.size();i++) {
157                 content = (Content) contents.get(i);
158                 SyndContent sContent = new SyndContentImpl();
159                 sContent.setType(content.getType());
160                 sContent.setValue(content.getValue());
161                 sContents.add(sContent);
162             }
163             syndEntry.setContents(sContents);
164         }
165 
166         List authors = entry.getAuthors();
167         if (authors!=null && authors.size() > 0) {
168             syndEntry.setAuthors(createSyndPersons(authors));
169             SyndPerson person0 = (SyndPerson)syndEntry.getAuthors().get(0);
170             syndEntry.setAuthor(person0.getName());
171         }
172 
173         Date date = entry.getModified();
174         if (date==null) {
175             date = entry.getIssued();
176             if (date==null) {
177                 date = entry.getCreated();
178             }
179         }
180         if (date!=null) {
181             syndEntry.setPublishedDate(date);
182         }
183 
184         return syndEntry;
185     }
186 
187     public WireFeed createRealFeed(SyndFeed syndFeed) {
188         Feed aFeed = new Feed(getType());
189         aFeed.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
190 
191         aFeed.setEncoding(syndFeed.getEncoding());
192 
193         aFeed.setId(syndFeed.getUri());
194 
195         aFeed.setTitle(syndFeed.getTitle());
196 
197         String sLink = syndFeed.getLink();
198         if (sLink!=null) {
199             Link link = new Link();
200             link.setRel("alternate");
201             link.setHref(sLink);
202             List list = new ArrayList();
203             list.add(link);
204             aFeed.setAlternateLinks(list);
205         }
206 
207         String sDesc = syndFeed.getDescription();
208         if (sDesc!=null) {
209             Content tagline = new Content();
210             tagline.setValue(sDesc);
211             aFeed.setTagline(tagline);
212         }
213 
214         aFeed.setLanguage(syndFeed.getLanguage());
215 
216         List authors = syndFeed.getAuthors();
217         if (authors!=null && authors.size() > 0) {
218             aFeed.setAuthors(createAtomPersons(authors));
219         }
220 
221         aFeed.setCopyright(syndFeed.getCopyright());
222 
223         aFeed.setModified(syndFeed.getPublishedDate());
224 
225         List sEntries = syndFeed.getEntries();
226         if (sEntries!=null) {
227             aFeed.setEntries(createAtomEntries(sEntries));
228         }
229 
230         return aFeed;
231     }
232 
233     protected static List createAtomPersons(List sPersons) {
234         List persons = new ArrayList();
235         for (Iterator iter = sPersons.iterator(); iter.hasNext(); ) {
236             SyndPerson sPerson = (SyndPerson)iter.next();
237             Person person = new Person();
238             person.setName(sPerson.getName());
239             person.setUri(sPerson.getUri());
240             person.setEmail(sPerson.getEmail());
241             persons.add(person);
242         }
243         return persons;
244     }
245     
246     protected static List createSyndPersons(List aPersons) {
247         List persons = new ArrayList();
248         for (Iterator iter = aPersons.iterator(); iter.hasNext(); ) {
249             Person aPerson = (Person)iter.next();
250             SyndPerson person = new SyndPersonImpl();
251             person.setName(aPerson.getName());
252             person.setUri(aPerson.getUri());
253             person.setEmail(aPerson.getEmail());
254             persons.add(person);
255         }
256         return persons;
257     }
258     
259     protected List createAtomEntries(List syndEntries) {
260         List atomEntries = new ArrayList();
261         for (int i=0;i<syndEntries.size();i++) {
262             atomEntries.add(createAtomEntry((SyndEntry)syndEntries.get(i)));
263         }
264         return atomEntries;
265     }
266 
267     protected Entry createAtomEntry(SyndEntry sEntry) {
268         Entry aEntry = new Entry();
269         aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
270 
271         aEntry.setId(sEntry.getUri());
272 
273         aEntry.setTitle(sEntry.getTitle());
274 
275         String sLink = sEntry.getLink();
276         if (sLink!=null) {
277             Link link = new Link();
278             link.setRel("alternate");
279             link.setHref(sLink);
280             List list = new ArrayList();
281             list.add(link);
282             aEntry.setAlternateLinks(list);
283         }
284 
285         SyndContent sContent = sEntry.getDescription();
286         if (sContent!=null) {
287             Content content = new Content();
288             content.setType(sContent.getType());
289             content.setValue(sContent.getValue());
290             content.setMode(Content.ESCAPED);
291             aEntry.setSummary(content);
292         }
293 
294         List contents = sEntry.getContents();
295         if (contents.size()>0) {
296             List aContents = new ArrayList();
297             for (int i=0;i<contents.size();i++) {
298                 sContent = (SyndContentImpl) contents.get(i);
299                 Content content = new Content();
300                 content.setType(sContent.getType());
301                 content.setValue(sContent.getValue());
302                 content.setMode(Content.ESCAPED);
303                 aContents.add(content);
304 
305             }
306             aEntry.setContents(aContents);
307         }
308 
309         List sAuthors = sEntry.getAuthors();
310         if (sAuthors!=null && sAuthors.size() > 0) {
311             aEntry.setAuthors(createAtomPersons(sAuthors));
312         } else if (sEntry.getAuthor() != null) {
313             Person person = new Person();
314             person.setName(sEntry.getAuthor()); 
315             List authors = new ArrayList();
316             authors.add(person);
317             aEntry.setAuthors(authors);
318         }
319 
320         aEntry.setModified(sEntry.getPublishedDate());
321         aEntry.setIssued(sEntry.getPublishedDate());
322 
323         return aEntry;
324     }
325 
326 }