1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44 /***
45 */
46 public class ConverterForAtom10 implements Converter {
47 private String _type;
48
49 public ConverterForAtom10() {
50 this("atom_1.0");
51 }
52
53 protected ConverterForAtom10(String type) {
54 _type = type;
55 }
56
57 public String getType() {
58 return _type;
59 }
60
61 public void copyInto(WireFeed feed, SyndFeed syndFeed) {
62 Feed aFeed = (Feed) feed;
63
64 syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
65
66 syndFeed.setEncoding(aFeed.getEncoding());
67
68 syndFeed.setUri(aFeed.getId());
69
70 syndFeed.setTitle(aFeed.getTitle());
71
72 Content aSubtitle = aFeed.getSubtitle();
73 if (aSubtitle!=null) {
74 syndFeed.setDescription(aSubtitle.getValue());
75 }
76
77
78 if (aFeed.getAlternateLinks() != null
79 && aFeed.getAlternateLinks().size() == 1) {
80 Link theLink = (Link)aFeed.getAlternateLinks().get(0);
81 syndFeed.setLink(theLink.getHref());
82 }
83
84 List syndLinks = new ArrayList();
85 if (aFeed.getAlternateLinks() != null
86 && aFeed.getAlternateLinks().size() > 0) {
87 syndLinks.addAll(createSyndLinks(aFeed.getAlternateLinks()));
88 }
89 if (aFeed.getOtherLinks() != null
90 && aFeed.getOtherLinks().size() > 0) {
91 syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks()));
92 }
93
94 List aEntries = aFeed.getEntries();
95 if (aEntries!=null) {
96 syndFeed.setEntries(createSyndEntries(aFeed, aEntries));
97 }
98
99
100
101
102 List authors = aFeed.getAuthors();
103 if (authors!=null && authors.size() > 0) {
104 syndFeed.setAuthors(ConverterForAtom03.createSyndPersons(authors));
105 }
106
107 String rights = aFeed.getRights();
108 if (rights!=null) {
109 syndFeed.setCopyright(rights);
110 }
111
112 Date date = aFeed.getUpdated();
113 if (date!=null) {
114 syndFeed.setPublishedDate(date);
115 }
116 }
117
118 protected List createSyndLinks(List aLinks) {
119 ArrayList sLinks = new ArrayList();
120 for (Iterator iter = aLinks.iterator(); iter.hasNext();) {
121 Link link = (Link)iter.next();
122 SyndLink sLink = new SyndLinkImpl();
123 sLink.setHref( link.getHref());
124 sLink.setType( link.getType());
125 sLink.setLength( link.getLength());
126 sLink.setHreflang( link.getHref());
127 sLinks.add(sLink);
128 }
129 return sLinks;
130 }
131
132 protected List createSyndEntries(Feed feed, List atomEntries) {
133 List syndEntries = new ArrayList();
134 for (int i=0;i<atomEntries.size();i++) {
135 syndEntries.add(createSyndEntry(feed, (Entry) atomEntries.get(i)));
136 }
137 return syndEntries;
138 }
139
140 protected SyndEntry createSyndEntry(Feed feed, Entry entry) {
141 SyndEntry syndEntry = new SyndEntryImpl();
142 syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
143
144 syndEntry.setTitle(entry.getTitle());
145
146 String id = entry.getId();
147 if (id!=null) {
148 syndEntry.setUri(entry.getId());
149 }
150 else {
151 syndEntry.setUri(syndEntry.getLink());
152 }
153
154 List contents = entry.getContents();
155 if (contents != null && contents.size() > 0) {
156 List sContents = new ArrayList();
157 for (Iterator iter=contents.iterator(); iter.hasNext();) {
158 Content content = (Content)iter.next();
159 sContents.add(createSyndContent(content));
160 }
161 syndEntry.setContents(sContents);
162 }
163
164 Content summary = entry.getSummary();
165 if (summary!=null) {
166 syndEntry.setDescription(createSyndContent(entry.getSummary()));
167 }
168
169 List authors = entry.getAuthors();
170 if (authors!=null && authors.size() > 0) {
171 syndEntry.setAuthors(ConverterForAtom03.createSyndPersons(authors));
172 SyndPerson person0 = (SyndPerson)syndEntry.getAuthors().get(0);
173 syndEntry.setAuthor(person0.getName());
174 }
175
176 Date date = entry.getPublished();
177 if (date!=null) {
178 syndEntry.setPublishedDate(date);
179 }
180
181 date = entry.getUpdated();
182 if (date!=null) {
183 syndEntry.setUpdatedDate(date);
184 }
185
186 List categories = entry.getCategories();
187 if (categories!=null) {
188 List syndCategories = new ArrayList();
189 for (Iterator iter=categories.iterator(); iter.hasNext();) {
190 Category c = (Category)iter.next();
191 SyndCategory syndCategory = new SyndCategoryImpl();
192 syndCategory.setName(c.getTerm());
193 syndCategory.setTaxonomyUri(c.getScheme());
194
195
196 syndCategories.add(syndCategory);
197 }
198 syndEntry.setCategories(syndCategories);
199 }
200
201
202 if (entry.getAlternateLinks() != null
203 && entry.getAlternateLinks().size() == 1) {
204 Link theLink = (Link)entry.getAlternateLinks().get(0);
205 syndEntry.setLink(theLink.getHref());
206 }
207
208
209 List syndLinks = new ArrayList();
210 if (entry.getAlternateLinks() != null
211 && entry.getAlternateLinks().size() > 0) {
212 syndLinks.addAll(createSyndLinks(entry.getAlternateLinks()));
213 }
214 if (entry.getOtherLinks() != null
215 && entry.getOtherLinks().size() > 0) {
216 syndLinks.addAll(createSyndLinks(entry.getOtherLinks()));
217 }
218 syndEntry.setLinks(syndLinks);
219
220 return syndEntry;
221 }
222
223 public SyndLink createSyndLink(Feed feed, Entry entry, Link link) {
224 SyndLink syndLink = new SyndLinkImpl();
225 syndLink.setRel(link.getRel());
226 syndLink.setType(link.getType());
227 syndLink.setHref(link.getHref());
228 syndLink.setHreflang(link.getHreflang());
229 syndLink.setLength(link.getLength());
230 return syndLink;
231 }
232
233 public WireFeed createRealFeed(SyndFeed syndFeed) {
234 Feed aFeed = new Feed(getType());
235 aFeed.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
236
237 aFeed.setEncoding(syndFeed.getEncoding());
238
239 aFeed.setId(syndFeed.getUri());
240
241 aFeed.setTitle(syndFeed.getTitle());
242
243
244 List alternateLinks = new ArrayList();
245 List otherLinks = new ArrayList();
246 String sLink = syndFeed.getLink();
247 List slinks = syndFeed.getLinks();
248 if (slinks != null) {
249 for (Iterator iter=slinks.iterator(); iter.hasNext();) {
250 SyndLink syndLink = (SyndLink)iter.next();
251 Link link = new Link();
252 link.setRel(syndLink.getRel());
253 link.setHref(syndLink.getHref());
254 link.setHreflang(syndLink.getHreflang());
255 link.setLength(syndLink.getLength());
256 if (link.getRel() == null ||
257 "".equals(link.getRel().trim()) ||
258 "alternate".equals(syndLink.getRel())) {
259 alternateLinks.add(link);
260 } else {
261 otherLinks.add(link);
262 }
263 }
264 }
265
266 if (alternateLinks.size() == 0 && syndFeed.getLink() != null) {
267 Link link = new Link();
268 link.setRel("alternate");
269 link.setHref(syndFeed.getLink());
270 alternateLinks.add(link);
271 }
272 if (alternateLinks.size() > 0) aFeed.setAlternateLinks(alternateLinks);
273 if (otherLinks.size() > 0) aFeed.setOtherLinks(otherLinks);
274
275 List sCats = syndFeed.getCategories();
276 List aCats = new ArrayList();
277 if (sCats != null) {
278 for (Iterator iter=sCats.iterator(); iter.hasNext();) {
279 SyndCategory sCat = (SyndCategory)iter.next();
280 Category aCat = new Category();
281 aCat.setTerm(sCat.getName());
282
283 aCat.setScheme(sCat.getTaxonomyUri());
284 aCats.add(aCat);
285 }
286 }
287 if (aCats.size() > 0) aFeed.setCategories(aCats);
288
289 String sDesc = syndFeed.getDescription();
290 if (sDesc != null) {
291 Content subtitle = new Content();
292 subtitle.setType(Content.TEXT);
293 subtitle.setValue(sDesc);
294 aFeed.setSubtitle(subtitle);
295 }
296
297 List authors = syndFeed.getAuthors();
298 if (authors!=null && authors.size() > 0) {
299 aFeed.setAuthors(ConverterForAtom03.createAtomPersons(authors));
300 }
301
302 aFeed.setRights(syndFeed.getCopyright());
303
304 aFeed.setUpdated(syndFeed.getPublishedDate());
305
306 List sEntries = syndFeed.getEntries();
307 if (sEntries!=null) {
308 aFeed.setEntries(createAtomEntries(sEntries));
309 }
310
311 return aFeed;
312 }
313
314 protected SyndContent createSyndContent(Content content) {
315 SyndContent sContent = new SyndContentImpl();
316 sContent.setType(content.getType());
317 sContent.setValue(content.getValue());
318 return sContent;
319 }
320
321 protected List createAtomEntries(List syndEntries) {
322 List atomEntries = new ArrayList();
323 for (int i=0;i<syndEntries.size();i++) {
324 atomEntries.add(createAtomEntry((SyndEntry)syndEntries.get(i)));
325 }
326 return atomEntries;
327 }
328
329 protected Content createAtomContent(SyndContent sContent) {
330 Content content = new Content();
331 content.setType(sContent.getType());
332 content.setValue(sContent.getValue());
333 return content;
334 }
335
336 protected List createAtomContents(List syndContents) {
337 List atomContents = new ArrayList();
338 for (int i=0;i<syndContents.size();i++) {
339 atomContents.add(createAtomContent((SyndContent)syndContents.get(i)));
340 }
341 return atomContents;
342 }
343
344 protected Entry createAtomEntry(SyndEntry sEntry) {
345 Entry aEntry = new Entry();
346 aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
347
348 aEntry.setId(sEntry.getUri());
349
350 aEntry.setTitle(sEntry.getTitle());
351
352
353 List alternateLinks = new ArrayList();
354 List otherLinks = new ArrayList();
355 List slinks = sEntry.getLinks();
356 if (slinks != null) {
357 for (Iterator iter=slinks.iterator(); iter.hasNext();) {
358 SyndLink syndLink = (SyndLink)iter.next();
359 Link link = new Link();
360 link.setRel(syndLink.getRel());
361 link.setHref(syndLink.getHref());
362 link.setHreflang(syndLink.getHreflang());
363 link.setLength(syndLink.getLength());
364 link.setType(syndLink.getType());
365 if (link.getRel() == null ||
366 "".equals(link.getRel().trim()) ||
367 "alternate".equals(syndLink.getRel())) {
368 alternateLinks.add(link);
369 } else {
370 otherLinks.add(link);
371 }
372 }
373 }
374
375 if (alternateLinks.size() == 0 && sEntry.getLink() != null) {
376 Link link = new Link();
377 link.setRel("alternate");
378 link.setHref(sEntry.getLink());
379 alternateLinks.add(link);
380 }
381 if (alternateLinks.size() > 0) aEntry.setAlternateLinks(alternateLinks);
382 if (otherLinks.size() > 0) aEntry.setOtherLinks(otherLinks);
383
384 List sCats = sEntry.getCategories();
385 List aCats = new ArrayList();
386 if (sCats != null) {
387 for (Iterator iter=sCats.iterator(); iter.hasNext();) {
388 SyndCategory sCat = (SyndCategory)iter.next();
389 Category aCat = new Category();
390 aCat.setTerm(sCat.getName());
391
392 aCat.setScheme(sCat.getTaxonomyUri());
393 aCats.add(aCat);
394 }
395 }
396 if (aCats.size() > 0) aEntry.setCategories(aCats);
397
398 SyndContent sDescription = sEntry.getDescription();
399 if (sDescription!=null) {
400 Content summary = new Content();
401 summary.setType(sDescription.getType());
402 summary.setValue(sDescription.getValue());
403 aEntry.setSummary(summary);
404 }
405
406 List syndContents = sEntry.getContents();
407 aEntry.setContents(createAtomContents(syndContents));
408
409 List authors = sEntry.getAuthors();
410 if (authors!=null && authors.size() > 0) {
411 aEntry.setAuthors(ConverterForAtom03.createAtomPersons(authors));
412 } else if (sEntry.getAuthor() != null) {
413 Person person = new Person();
414 person.setName(sEntry.getAuthor());
415 authors = new ArrayList();
416 authors.add(person);
417 aEntry.setAuthors(authors);
418 }
419
420 aEntry.setPublished(sEntry.getPublishedDate());
421 aEntry.setUpdated(sEntry.getUpdatedDate());
422
423 return aEntry;
424 }
425
426 }