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 java.util.ArrayList;
20  import java.util.Date;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import com.sun.syndication.feed.WireFeed;
25  import com.sun.syndication.feed.atom.Category;
26  import com.sun.syndication.feed.atom.Content;
27  import com.sun.syndication.feed.atom.Entry;
28  import com.sun.syndication.feed.atom.Feed;
29  import com.sun.syndication.feed.atom.Link;
30  import com.sun.syndication.feed.atom.Person;
31  import com.sun.syndication.feed.module.impl.ModuleUtils;
32  import com.sun.syndication.feed.synd.Converter;
33  import com.sun.syndication.feed.synd.SyndCategory;
34  import com.sun.syndication.feed.synd.SyndCategoryImpl;
35  import com.sun.syndication.feed.synd.SyndContent;
36  import com.sun.syndication.feed.synd.SyndContentImpl;
37  import com.sun.syndication.feed.synd.SyndEntry;
38  import com.sun.syndication.feed.synd.SyndEntryImpl;
39  import com.sun.syndication.feed.synd.SyndFeed;
40  import com.sun.syndication.feed.synd.SyndLink;
41  import com.sun.syndication.feed.synd.SyndLinkImpl;
42  import com.sun.syndication.feed.synd.SyndPerson;
43  import com.sun.syndication.feed.synd.SyndEnclosure;
44  import com.sun.syndication.feed.synd.SyndEnclosureImpl;
45  
46  
47  /***
48   */
49  public class ConverterForAtom10 implements Converter {
50      private String _type;
51  
52      public ConverterForAtom10() {
53          this("atom_1.0");
54      }
55  
56      protected ConverterForAtom10(String type) {
57          _type = type;
58      }
59  
60      public String getType() {
61          return _type;
62      }
63  
64      public void copyInto(WireFeed feed, SyndFeed syndFeed) {
65          Feed aFeed = (Feed) feed;
66  
67          syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
68          
69          if (((List)feed.getForeignMarkup()).size() > 0) {
70              syndFeed.setForeignMarkup((List)feed.getForeignMarkup());
71          }
72  
73          syndFeed.setEncoding(aFeed.getEncoding());
74  
75          syndFeed.setUri(aFeed.getId());
76  
77          Content aTitle = aFeed.getTitleEx();
78          if (aTitle != null) {
79              SyndContent c = new SyndContentImpl();
80              c.setType(aTitle.getType());
81              c.setValue(aTitle.getValue());
82              syndFeed.setTitleEx(c);
83          }
84  
85          Content aSubtitle = aFeed.getSubtitle();
86          if (aSubtitle!=null) {
87              SyndContent c = new SyndContentImpl();
88              c.setType(aSubtitle.getType());
89              c.setValue(aSubtitle.getValue());
90              syndFeed.setDescriptionEx(c);
91          }
92  
93          // if there is exactly one alternate link, use that as THE link
94          if (aFeed.getAlternateLinks() != null 
95                  && aFeed.getAlternateLinks().size() == 1) {
96              Link theLink = (Link)aFeed.getAlternateLinks().get(0);
97              syndFeed.setLink(theLink.getHref());
98          }
99          // lump alternate and other links together
100         List syndLinks = new ArrayList();
101         if (aFeed.getAlternateLinks() != null 
102                 && aFeed.getAlternateLinks().size() > 0) {
103             syndLinks.addAll(createSyndLinks(aFeed.getAlternateLinks()));
104         }
105         if (aFeed.getOtherLinks() != null 
106                 && aFeed.getOtherLinks().size() > 0) {
107             syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks()));
108         }
109             
110         List aEntries = aFeed.getEntries();
111         if (aEntries!=null) {
112             syndFeed.setEntries(createSyndEntries(aFeed, aEntries));
113         }
114 
115         // Core Atom language/author/copyright/modified elements have precedence
116         // over DC equivalent info.
117 
118         List authors = aFeed.getAuthors();
119         if (authors!=null && authors.size() > 0) {
120             syndFeed.setAuthors(ConverterForAtom03.createSyndPersons(authors));
121         }
122 
123         String rights = aFeed.getRights();
124         if (rights!=null) {
125             syndFeed.setCopyright(rights);
126         }
127 
128         Date date = aFeed.getUpdated();
129         if (date!=null) {
130             syndFeed.setPublishedDate(date);
131         }
132         
133     }
134 
135     protected List createSyndLinks(List aLinks) {
136         ArrayList sLinks = new ArrayList();
137         for (Iterator iter = aLinks.iterator(); iter.hasNext();) {
138             Link link = (Link)iter.next();
139             SyndLink sLink = new SyndLinkImpl();
140             sLink.setRel(      link.getRel());
141             sLink.setHref(     link.getHref());
142             sLink.setType(     link.getType());
143             sLink.setTitle( link.getTitle());
144             sLink.setLength(   link.getLength());
145             sLink.setHreflang( link.getHref());
146             sLinks.add(sLink);
147         }
148         return sLinks;
149     }
150     
151     protected List createSyndEntries(Feed feed, List atomEntries) {
152         List syndEntries = new ArrayList();
153         for (int i=0;i<atomEntries.size();i++) {
154             syndEntries.add(createSyndEntry(feed, (Entry) atomEntries.get(i)));
155         }
156         return syndEntries;
157     }
158 
159     protected SyndEntry createSyndEntry(Feed feed, Entry entry) {
160         SyndEntry syndEntry = new SyndEntryImpl();
161         syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
162         
163         if (((List)entry.getForeignMarkup()).size() >  0) {
164             syndEntry.setForeignMarkup((List)entry.getForeignMarkup());
165         }
166 
167         Content eTitle = entry.getTitleEx();
168         if (eTitle != null) {
169             syndEntry.setTitleEx(createSyndContent(eTitle));
170         }
171         
172         Content summary = entry.getSummary();
173         if (summary!=null) {
174             syndEntry.setDescription(createSyndContent(summary));
175         }
176 
177         String id = entry.getId();
178         if (id!=null) {
179             syndEntry.setUri(entry.getId());
180         }
181         else {
182             syndEntry.setUri(syndEntry.getLink());
183         }
184 
185         List contents = entry.getContents();
186         if (contents != null && contents.size() > 0) {
187             List sContents = new ArrayList();
188             for (Iterator iter=contents.iterator(); iter.hasNext();) {
189                 Content content = (Content)iter.next();
190                 sContents.add(createSyndContent(content));
191             }
192             syndEntry.setContents(sContents);
193         }
194 
195         List authors = entry.getAuthors();
196         if (authors!=null && authors.size() > 0) {
197             syndEntry.setAuthors(ConverterForAtom03.createSyndPersons(authors));
198             SyndPerson person0 = (SyndPerson)syndEntry.getAuthors().get(0);
199             syndEntry.setAuthor(person0.getName());
200         }
201 
202         Date date = entry.getPublished();
203         if (date!=null) {
204             syndEntry.setPublishedDate(date);
205         }
206 
207         date = entry.getUpdated();
208         if (date!=null) {
209             syndEntry.setUpdatedDate(date);
210         }
211         
212         List categories = entry.getCategories();
213         if (categories!=null) {
214             List syndCategories = new ArrayList();
215             for (Iterator iter=categories.iterator(); iter.hasNext();) {
216                 Category c = (Category)iter.next();
217                 SyndCategory syndCategory = new SyndCategoryImpl();
218                 syndCategory.setName(c.getTerm()); 
219                 syndCategory.setTaxonomyUri(c.getScheme());
220                 // TODO: categories MAY have labels 
221                 //       syndCategory.setLabel(c.getLabel());
222                 syndCategories.add(syndCategory);
223             }
224             syndEntry.setCategories(syndCategories);
225         }
226                 
227         // if there is exactly one alternate link, use that as THE link
228         if (entry.getAlternateLinks() != null
229 				&& entry.getAlternateLinks().size() == 1) {
230             Link theLink = (Link)entry.getAlternateLinks().get(0);
231             syndEntry.setLink(theLink.getHref());
232         }
233 
234         // Create synd enclosures from enclosure links
235         List syndEnclosures = new ArrayList();
236         if (entry.getOtherLinks() != null && entry.getOtherLinks().size() > 0) {
237             List oLinks = entry.getOtherLinks();
238             for (Iterator iter = oLinks.iterator(); iter.hasNext(); ) {
239                 Link thisLink = (Link)iter.next();
240                 if ("enclosure".equals(thisLink.getRel()))
241                     syndEnclosures.add(
242                             createSyndEnclosure(feed, entry, thisLink));
243             }
244         }
245         syndEntry.setEnclosures(syndEnclosures);
246 
247         // lump alternate and other links together
248         List syndLinks = new ArrayList();
249         if (entry.getAlternateLinks() != null 
250                 && entry.getAlternateLinks().size() > 0) {
251             syndLinks.addAll(createSyndLinks(entry.getAlternateLinks()));
252         }
253         if (entry.getOtherLinks() != null 
254                 && entry.getOtherLinks().size() > 0) {
255             syndLinks.addAll(createSyndLinks(entry.getOtherLinks()));
256         }
257         syndEntry.setLinks(syndLinks);
258 
259         return syndEntry;
260     }
261 
262     public SyndEnclosure createSyndEnclosure(Feed feed, Entry entry,
263             Link link) {
264         SyndEnclosure syndEncl = new SyndEnclosureImpl();
265         syndEncl.setUrl(link.getHref());
266         syndEncl.setType(link.getType());
267         syndEncl.setLength(link.getLength());
268         return syndEncl;
269     }
270     
271     public SyndLink createSyndLink(Feed feed, Entry entry, Link link) {
272         SyndLink syndLink = new SyndLinkImpl(); 
273         syndLink.setRel(link.getRel());
274         syndLink.setType(link.getType());
275         syndLink.setHref(link.getHref());
276         syndLink.setHreflang(link.getHreflang());
277         syndLink.setLength(link.getLength());
278         return syndLink;
279     }
280     
281     public WireFeed createRealFeed(SyndFeed syndFeed) {
282         Feed aFeed = new Feed(getType());
283         aFeed.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
284 
285         aFeed.setEncoding(syndFeed.getEncoding());
286 
287         aFeed.setId(syndFeed.getUri());
288 
289         SyndContent sTitle = syndFeed.getTitleEx();
290         if (sTitle != null) {
291             Content title = new Content();
292             title.setType(sTitle.getType());
293             title.setValue(sTitle.getValue());
294             aFeed.setTitleEx(title);
295         }
296         
297         SyndContent sDesc = syndFeed.getDescriptionEx();
298         if (sDesc != null) {
299             Content subtitle = new Content();
300             subtitle.setType(sDesc.getType());
301             subtitle.setValue(sDesc.getValue());
302             aFeed.setSubtitle(subtitle);
303         }
304 
305         // separate SyndEntry's links collection into alternate and other links
306         List alternateLinks = new ArrayList();
307         List otherLinks = new ArrayList();
308         String sLink = syndFeed.getLink();
309         List slinks = syndFeed.getLinks();
310         if (slinks != null) {
311             for (Iterator iter=slinks.iterator(); iter.hasNext();) {       
312                 SyndLink syndLink = (SyndLink)iter.next();                
313                 Link link = new Link();
314                 link.setRel(syndLink.getRel());
315                 link.setHref(syndLink.getHref());
316                 link.setHreflang(syndLink.getHreflang());
317                 link.setLength(syndLink.getLength());                
318                 if (link.getRel() == null ||
319                         "".equals(link.getRel().trim()) ||
320                         "alternate".equals(syndLink.getRel())) {
321                     alternateLinks.add(link);
322                 } else {
323                     otherLinks.add(link);
324                 }
325             }
326         }
327         // no alternate link? then use THE link if there is one
328         if (alternateLinks.size() == 0 && syndFeed.getLink() != null) {
329             Link link = new Link();
330             link.setRel("alternate");
331             link.setHref(syndFeed.getLink());
332             alternateLinks.add(link);
333         }
334         if (alternateLinks.size() > 0) aFeed.setAlternateLinks(alternateLinks);
335         if (otherLinks.size() > 0) aFeed.setOtherLinks(otherLinks);
336         
337         List sCats = syndFeed.getCategories();
338         List aCats = new ArrayList();
339         if (sCats != null) {
340             for (Iterator iter=sCats.iterator(); iter.hasNext();) { 
341                 SyndCategory sCat = (SyndCategory)iter.next();
342                 Category aCat = new Category();
343                 aCat.setTerm(sCat.getName());
344                 // TODO: aCat.setLabel(sCat.getLabel());
345                 aCat.setScheme(sCat.getTaxonomyUri());
346                 aCats.add(aCat);
347             }
348         }
349         if (aCats.size() > 0) aFeed.setCategories(aCats);
350 
351         List authors = syndFeed.getAuthors();
352         if (authors!=null && authors.size() > 0) {
353             aFeed.setAuthors(ConverterForAtom03.createAtomPersons(authors));
354         }
355 
356         aFeed.setRights(syndFeed.getCopyright());
357 
358         aFeed.setUpdated(syndFeed.getPublishedDate());
359 
360         List sEntries = syndFeed.getEntries();
361         if (sEntries!=null) {
362             aFeed.setEntries(createAtomEntries(sEntries));
363         }
364 
365         if (((List)syndFeed.getForeignMarkup()).size() > 0) {
366             aFeed.setForeignMarkup(syndFeed.getForeignMarkup());
367         }
368         return aFeed;
369     }
370 
371     protected SyndContent createSyndContent(Content content) {
372         SyndContent sContent = new SyndContentImpl();
373         sContent.setType(content.getType());
374         sContent.setValue(content.getValue());
375         return sContent;
376     }
377 
378     protected List createAtomEntries(List syndEntries) {
379         List atomEntries = new ArrayList();
380         for (int i=0;i<syndEntries.size();i++) {
381             atomEntries.add(createAtomEntry((SyndEntry)syndEntries.get(i)));
382         }
383         return atomEntries;
384     }
385 
386     protected Content createAtomContent(SyndContent sContent) {
387         Content content = new Content();
388         content.setType(sContent.getType());
389         content.setValue(sContent.getValue());
390         return content;
391     }
392 
393     protected List createAtomContents(List syndContents) {
394         List atomContents = new ArrayList();
395         for (int i=0;i<syndContents.size();i++) {
396             atomContents.add(createAtomContent((SyndContent)syndContents.get(i)));
397         }
398         return atomContents;
399     }
400 
401     protected Entry createAtomEntry(SyndEntry sEntry) {
402         Entry aEntry = new Entry();
403         aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
404 
405         aEntry.setId(sEntry.getUri());
406 
407         SyndContent sTitle = sEntry.getTitleEx();
408         if (sTitle!=null) {
409             Content title = new Content();
410             title.setType(sTitle.getType());
411             title.setValue(sTitle.getValue());
412             aEntry.setTitleEx(title);
413         }
414         
415         SyndContent sDescription = sEntry.getDescription();
416         if (sDescription!=null) {
417             Content summary = new Content();
418             summary.setType(sDescription.getType());
419             summary.setValue(sDescription.getValue());
420             aEntry.setSummary(summary);
421         }
422 
423         // separate SyndEntry's links collection into alternate and other links
424         List alternateLinks = new ArrayList();
425         List otherLinks = new ArrayList();
426         List slinks = sEntry.getLinks();
427         if (slinks != null) {
428             for (Iterator iter=slinks.iterator(); iter.hasNext();) {       
429                 SyndLink syndLink = (SyndLink)iter.next();                
430                 Link link = new Link();
431                 link.setRel(syndLink.getRel());
432                 link.setHref(syndLink.getHref());
433                 link.setHreflang(syndLink.getHreflang());
434                 link.setLength(syndLink.getLength());                
435                 link.setType(syndLink.getType());
436                 if (link.getRel() == null ||
437                         "".equals(link.getRel().trim()) ||
438                         "alternate".equals(syndLink.getRel())) {
439                     alternateLinks.add(link);
440                 } else {
441                     otherLinks.add(link);
442                 }
443             }
444         }
445         // no alternate link? then use THE link if there is one
446         if (alternateLinks.size() == 0 && sEntry.getLink() != null) {
447             Link link = new Link();
448             link.setRel("alternate");
449             link.setHref(sEntry.getLink());
450             alternateLinks.add(link);
451         }
452         if (alternateLinks.size() > 0) aEntry.setAlternateLinks(alternateLinks);
453         if (otherLinks.size() > 0) aEntry.setOtherLinks(otherLinks);
454        
455         List sCats = sEntry.getCategories();
456         List aCats = new ArrayList();
457         if (sCats != null) {
458             for (Iterator iter=sCats.iterator(); iter.hasNext();) { 
459                 SyndCategory sCat = (SyndCategory)iter.next();
460                 Category aCat = new Category();
461                 aCat.setTerm(sCat.getName());
462                 // TODO: aCat.setLabel(sCat.getLabel());
463                 aCat.setScheme(sCat.getTaxonomyUri());
464                 aCats.add(aCat);
465             }
466         }
467         if (aCats.size() > 0) aEntry.setCategories(aCats);
468         
469         List syndContents = sEntry.getContents();
470         aEntry.setContents(createAtomContents(syndContents));
471 
472         List authors = sEntry.getAuthors();
473         if (authors!=null && authors.size() > 0) {
474             aEntry.setAuthors(ConverterForAtom03.createAtomPersons(authors));
475         } else if (sEntry.getAuthor() != null) {
476             Person person = new Person();
477             person.setName(sEntry.getAuthor()); 
478             authors = new ArrayList();
479             authors.add(person);
480             aEntry.setAuthors(authors);
481         }
482 
483         aEntry.setPublished(sEntry.getPublishedDate());
484 
485         // Fix for issue #41 "Use updated instead of published"
486         // And issue #42 "Atom 1.0 Date (Updated or Published) Not Set"
487         // Atom requires an updated date, if it's missing use the published date
488         if (sEntry.getUpdatedDate() != null) {
489             aEntry.setUpdated(sEntry.getUpdatedDate());         
490         } else {
491             aEntry.setUpdated(sEntry.getPublishedDate());
492         }
493 
494         if (((List)sEntry.getForeignMarkup()).size() > 0) {
495             aEntry.setForeignMarkup((List)sEntry.getForeignMarkup());
496         }
497         return aEntry;
498     }
499 
500 }