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 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.SyndEnclosure;
29 import com.sun.syndication.feed.synd.SyndEnclosureImpl;
30 import com.sun.syndication.feed.synd.SyndEntry;
31 import com.sun.syndication.feed.synd.SyndContentImpl;
32 import com.sun.syndication.feed.synd.SyndEntryImpl;
33 import com.sun.syndication.feed.synd.SyndContent;
34 import com.sun.syndication.feed.synd.SyndLink;
35 import com.sun.syndication.feed.synd.SyndLinkImpl;
36 import com.sun.syndication.feed.synd.SyndPerson;
37 import com.sun.syndication.feed.synd.SyndPersonImpl;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Date;
42 import java.util.Iterator;
43
44 /***
45 */
46 public class ConverterForAtom03 implements Converter {
47 private String _type;
48
49 public ConverterForAtom03() {
50 this("atom_0.3");
51 }
52
53 protected ConverterForAtom03(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 if (((List)feed.getForeignMarkup()).size() > 0) {
67 syndFeed.setForeignMarkup(feed.getForeignMarkup());
68 }
69
70 syndFeed.setEncoding(aFeed.getEncoding());
71
72 syndFeed.setUri(aFeed.getId());
73
74 syndFeed.setTitle(aFeed.getTitle());
75
76
77 if (aFeed.getAlternateLinks() != null
78 && aFeed.getAlternateLinks().size() > 0) {
79 Link theLink = (Link)aFeed.getAlternateLinks().get(0);
80 syndFeed.setLink(theLink.getHrefResolved());
81 }
82
83 List syndLinks = new ArrayList();
84 if (aFeed.getAlternateLinks() != null
85 && aFeed.getAlternateLinks().size() > 0) {
86 syndLinks.addAll(createSyndLinks(aFeed.getAlternateLinks()));
87 }
88 if (aFeed.getOtherLinks() != null
89 && aFeed.getOtherLinks().size() > 0) {
90 syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks()));
91 }
92 syndFeed.setLinks(syndLinks);
93
94 Content tagline = aFeed.getTagline();
95 if (tagline!=null) {
96 syndFeed.setDescription(tagline.getValue());
97 }
98
99
100 List aEntries = aFeed.getEntries();
101 if (aEntries!=null) {
102 syndFeed.setEntries(createSyndEntries(aEntries));
103 }
104
105
106
107
108 String language = aFeed.getLanguage();
109 if (language!=null) {
110 syndFeed.setLanguage(language);
111 }
112
113 List authors = aFeed.getAuthors();
114 if (authors!=null && authors.size() > 0) {
115 syndFeed.setAuthors(createSyndPersons(authors));
116 }
117
118 String copyright = aFeed.getCopyright();
119 if (copyright!=null) {
120 syndFeed.setCopyright(copyright);
121 }
122
123 Date date = aFeed.getModified();
124 if (date!=null) {
125 syndFeed.setPublishedDate(date);
126 }
127
128 }
129
130 protected List createSyndLinks(List aLinks) {
131 ArrayList sLinks = new ArrayList();
132 for (Iterator iter = aLinks.iterator(); iter.hasNext();) {
133 Link link = (Link)iter.next();
134 if (!link.getRel().equals("enclosure")) {
135 SyndLink sLink = createSyndLink(link);
136 sLinks.add(sLink);
137 }
138 }
139 return sLinks;
140 }
141
142 public SyndLink createSyndLink(Link link) {
143 SyndLink syndLink = new SyndLinkImpl();
144 syndLink.setRel( link.getRel());
145 syndLink.setType( link.getType());
146 syndLink.setHref( link.getHrefResolved());
147 syndLink.setTitle( link.getTitle());
148 return syndLink;
149 }
150
151 protected List createSyndEntries(List atomEntries) {
152 List syndEntries = new ArrayList();
153 for (int i=0;i<atomEntries.size();i++) {
154 syndEntries.add(createSyndEntry((Entry) atomEntries.get(i)));
155 }
156 return syndEntries;
157 }
158
159 protected SyndEntry createSyndEntry(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 syndEntry.setTitle(entry.getTitle());
168
169
170 if (entry.getAlternateLinks() != null
171 && entry.getAlternateLinks().size() == 1) {
172 Link theLink = (Link)entry.getAlternateLinks().get(0);
173 syndEntry.setLink(theLink.getHrefResolved());
174 }
175
176
177 List syndEnclosures = new ArrayList();
178 if (entry.getOtherLinks() != null && entry.getOtherLinks().size() > 0) {
179 List oLinks = entry.getOtherLinks();
180 for (Iterator iter = oLinks.iterator(); iter.hasNext(); ) {
181 Link thisLink = (Link)iter.next();
182 if ("enclosure".equals(thisLink.getRel()))
183 syndEnclosures.add(createSyndEnclosure(entry, thisLink));
184 }
185 }
186 syndEntry.setEnclosures(syndEnclosures);
187
188
189 List syndLinks = new ArrayList();
190 if (entry.getAlternateLinks() != null
191 && entry.getAlternateLinks().size() > 0) {
192 syndLinks.addAll(createSyndLinks(entry.getAlternateLinks()));
193 }
194 if (entry.getOtherLinks() != null
195 && entry.getOtherLinks().size() > 0) {
196 syndLinks.addAll(createSyndLinks(entry.getOtherLinks()));
197 }
198 syndEntry.setLinks(syndLinks);
199
200
201 String id = entry.getId();
202 if (id!=null) {
203 syndEntry.setUri(entry.getId());
204 }
205 else {
206 syndEntry.setUri(syndEntry.getLink());
207 }
208
209 Content content = entry.getSummary();
210 if (content==null) {
211 List contents = entry.getContents();
212 if (contents!=null && contents.size()>0) {
213 content = (Content) contents.get(0);
214 }
215 }
216 if (content!=null) {
217 SyndContent sContent = new SyndContentImpl();
218 sContent.setType(content.getType());
219 sContent.setValue(content.getValue());
220 syndEntry.setDescription(sContent);
221 }
222
223 List contents = entry.getContents();
224 if (contents.size()>0) {
225 List sContents = new ArrayList();
226 for (int i=0;i<contents.size();i++) {
227 content = (Content) contents.get(i);
228 SyndContent sContent = new SyndContentImpl();
229 sContent.setType(content.getType());
230 sContent.setValue(content.getValue());
231 sContent.setMode(content.getMode());
232 sContents.add(sContent);
233 }
234 syndEntry.setContents(sContents);
235 }
236
237 List authors = entry.getAuthors();
238 if (authors!=null && authors.size() > 0) {
239 syndEntry.setAuthors(createSyndPersons(authors));
240 SyndPerson person0 = (SyndPerson)syndEntry.getAuthors().get(0);
241 syndEntry.setAuthor(person0.getName());
242 }
243
244 Date date = entry.getModified();
245 if (date==null) {
246 date = entry.getIssued();
247 if (date==null) {
248 date = entry.getCreated();
249 }
250 }
251 if (date!=null) {
252 syndEntry.setPublishedDate(date);
253 }
254
255 return syndEntry;
256 }
257
258 public SyndEnclosure createSyndEnclosure(Entry entry, Link link) {
259 SyndEnclosure syndEncl = new SyndEnclosureImpl();
260 syndEncl.setUrl(link.getHrefResolved());
261 syndEncl.setType(link.getType());
262 syndEncl.setLength(link.getLength());
263 return syndEncl;
264 }
265
266 public WireFeed createRealFeed(SyndFeed syndFeed) {
267 Feed aFeed = new Feed(getType());
268 aFeed.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
269
270 aFeed.setEncoding(syndFeed.getEncoding());
271
272 aFeed.setId(syndFeed.getUri());
273
274 SyndContent sTitle = syndFeed.getTitleEx();
275 if (sTitle != null) {
276 Content title = new Content();
277 if (sTitle.getType() != null) {
278 title.setType(sTitle.getType());
279 }
280
281 if (sTitle.getMode() != null) {
282 title.setMode(sTitle.getMode());
283 }
284
285 title.setValue(sTitle.getValue());
286 aFeed.setTitleEx(title);
287 }
288
289
290 List alternateLinks = new ArrayList();
291 List otherLinks = new ArrayList();
292 List slinks = syndFeed.getLinks();
293 if (slinks != null) {
294 for (Iterator iter=slinks.iterator(); iter.hasNext();) {
295 SyndLink syndLink = (SyndLink)iter.next();
296 Link link = createAtomLink(syndLink);
297 if (link.getRel() == null ||
298 "".equals(link.getRel().trim()) ||
299 "alternate".equals(link.getRel())) {
300 alternateLinks.add(link);
301 } else {
302 otherLinks.add(link);
303 }
304 }
305 }
306
307 if (alternateLinks.size() == 0 && syndFeed.getLink() != null) {
308 Link link = new Link();
309 link.setRel("alternate");
310 link.setHref(syndFeed.getLink());
311 alternateLinks.add(link);
312 }
313
314 if (alternateLinks.size() > 0) aFeed.setAlternateLinks(alternateLinks);
315 if (otherLinks.size() > 0) aFeed.setOtherLinks(otherLinks);
316
317 String sDesc = syndFeed.getDescription();
318 if (sDesc!=null) {
319 Content tagline = new Content();
320 tagline.setValue(sDesc);
321 aFeed.setTagline(tagline);
322 }
323
324 aFeed.setLanguage(syndFeed.getLanguage());
325
326 List authors = syndFeed.getAuthors();
327 if (authors!=null && authors.size() > 0) {
328 aFeed.setAuthors(createAtomPersons(authors));
329 }
330
331 aFeed.setCopyright(syndFeed.getCopyright());
332
333 aFeed.setModified(syndFeed.getPublishedDate());
334
335 List sEntries = syndFeed.getEntries();
336 if (sEntries!=null) {
337 aFeed.setEntries(createAtomEntries(sEntries));
338 }
339
340 return aFeed;
341 }
342
343 protected static List createAtomPersons(List sPersons) {
344 List persons = new ArrayList();
345 for (Iterator iter = sPersons.iterator(); iter.hasNext(); ) {
346 SyndPerson sPerson = (SyndPerson)iter.next();
347 Person person = new Person();
348 person.setName(sPerson.getName());
349 person.setUri(sPerson.getUri());
350 person.setEmail(sPerson.getEmail());
351 persons.add(person);
352 }
353 return persons;
354 }
355
356 protected static List createSyndPersons(List aPersons) {
357 List persons = new ArrayList();
358 for (Iterator iter = aPersons.iterator(); iter.hasNext(); ) {
359 Person aPerson = (Person)iter.next();
360 SyndPerson person = new SyndPersonImpl();
361 person.setName(aPerson.getName());
362 person.setUri(aPerson.getUri());
363 person.setEmail(aPerson.getEmail());
364 persons.add(person);
365 }
366 return persons;
367 }
368
369 protected List createAtomEntries(List syndEntries) {
370 List atomEntries = new ArrayList();
371 for (int i=0;i<syndEntries.size();i++) {
372 atomEntries.add(createAtomEntry((SyndEntry)syndEntries.get(i)));
373 }
374 return atomEntries;
375 }
376
377 protected Entry createAtomEntry(SyndEntry sEntry) {
378 Entry aEntry = new Entry();
379 aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
380
381 aEntry.setId(sEntry.getUri());
382
383 SyndContent sTitle = sEntry.getTitleEx();
384 if (sTitle!=null) {
385 Content title = new Content();
386 if (sTitle.getType() != null) {
387 title.setType(sTitle.getType());
388 }
389
390 if (sTitle.getMode() != null) {
391 title.setMode(sTitle.getMode());
392 }
393
394 title.setValue(sTitle.getValue());
395 aEntry.setTitleEx(title);
396 }
397
398
399 List alternateLinks = new ArrayList();
400 List otherLinks = new ArrayList();
401 List slinks = sEntry.getLinks();
402 if (slinks != null) {
403 for (Iterator iter=slinks.iterator(); iter.hasNext();) {
404 SyndLink syndLink = (SyndLink)iter.next();
405 Link link = createAtomLink(syndLink);
406 if (link.getRel() == null ||
407 "".equals(link.getRel().trim()) ||
408 "alternate".equals(link.getRel())) {
409 alternateLinks.add(link);
410 } else {
411 otherLinks.add(link);
412 }
413 }
414 }
415
416 if (alternateLinks.size() == 0 && sEntry.getLink() != null) {
417 Link link = new Link();
418 link.setRel("alternate");
419 link.setHref(sEntry.getLink());
420 alternateLinks.add(link);
421 }
422
423 List sEnclosures = sEntry.getEnclosures();
424 if (sEnclosures != null) {
425 for (Iterator iter=sEnclosures.iterator(); iter.hasNext();) {
426 SyndEnclosure syndEnclosure = (SyndEnclosure) iter.next();
427 Link link = createAtomEnclosure(syndEnclosure);
428 otherLinks.add(link);
429 }
430 }
431
432 if (alternateLinks.size() > 0) aEntry.setAlternateLinks(alternateLinks);
433 if (otherLinks.size() > 0) aEntry.setOtherLinks(otherLinks);
434
435
436 SyndContent sContent = sEntry.getDescription();
437 if (sContent!=null) {
438 Content content = new Content();
439 content.setType(sContent.getType());
440 content.setValue(sContent.getValue());
441 content.setMode(Content.ESCAPED);
442 aEntry.setSummary(content);
443 }
444
445 List contents = sEntry.getContents();
446 if (contents.size()>0) {
447 List aContents = new ArrayList();
448 for (int i=0;i<contents.size();i++) {
449 sContent = (SyndContentImpl) contents.get(i);
450 Content content = new Content();
451 content.setType(sContent.getType());
452 content.setValue(sContent.getValue());
453 content.setMode(sContent.getMode());
454 aContents.add(content);
455
456 }
457 aEntry.setContents(aContents);
458 }
459
460 List sAuthors = sEntry.getAuthors();
461 if (sAuthors!=null && sAuthors.size() > 0) {
462 aEntry.setAuthors(createAtomPersons(sAuthors));
463 } else if (sEntry.getAuthor() != null) {
464 Person person = new Person();
465 person.setName(sEntry.getAuthor());
466 List authors = new ArrayList();
467 authors.add(person);
468 aEntry.setAuthors(authors);
469 }
470
471 aEntry.setModified(sEntry.getPublishedDate());
472 aEntry.setIssued(sEntry.getPublishedDate());
473
474 return aEntry;
475 }
476
477 public Link createAtomLink(SyndLink syndLink) {
478 Link link = new Link();
479 link.setRel( syndLink.getRel());
480 link.setType( syndLink.getType());
481 link.setHref( syndLink.getHref());
482 link.setTitle( syndLink.getTitle());
483 return link;
484 }
485
486 public Link createAtomEnclosure(SyndEnclosure syndEnclosure) {
487 Link link = new Link();
488 link.setRel( "enclosure");
489 link.setType( syndEnclosure.getType());
490 link.setHref( syndEnclosure.getUrl());
491 link.setLength( syndEnclosure.getLength());
492 return link;
493 }
494
495 }