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