001: /*
002: * Copyright 2004 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.feed.synd.impl;
018:
019: import java.util.ArrayList;
020: import java.util.Date;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import com.sun.syndication.feed.WireFeed;
025: import com.sun.syndication.feed.atom.Category;
026: import com.sun.syndication.feed.atom.Content;
027: import com.sun.syndication.feed.atom.Entry;
028: import com.sun.syndication.feed.atom.Feed;
029: import com.sun.syndication.feed.atom.Link;
030: import com.sun.syndication.feed.atom.Person;
031: import com.sun.syndication.feed.module.impl.ModuleUtils;
032: import com.sun.syndication.feed.synd.Converter;
033: import com.sun.syndication.feed.synd.SyndCategory;
034: import com.sun.syndication.feed.synd.SyndCategoryImpl;
035: import com.sun.syndication.feed.synd.SyndContent;
036: import com.sun.syndication.feed.synd.SyndContentImpl;
037: import com.sun.syndication.feed.synd.SyndEntry;
038: import com.sun.syndication.feed.synd.SyndEntryImpl;
039: import com.sun.syndication.feed.synd.SyndFeed;
040: import com.sun.syndication.feed.synd.SyndLink;
041: import com.sun.syndication.feed.synd.SyndLinkImpl;
042: import com.sun.syndication.feed.synd.SyndPerson;
043: import com.sun.syndication.feed.synd.SyndEnclosure;
044: import com.sun.syndication.feed.synd.SyndEnclosureImpl;
045:
046: /**
047: */
048: public class ConverterForAtom10 implements Converter {
049: private String _type;
050:
051: public ConverterForAtom10() {
052: this ("atom_1.0");
053: }
054:
055: protected ConverterForAtom10(String type) {
056: _type = type;
057: }
058:
059: public String getType() {
060: return _type;
061: }
062:
063: public void copyInto(WireFeed feed, SyndFeed syndFeed) {
064: Feed aFeed = (Feed) feed;
065:
066: syndFeed.setModules(ModuleUtils
067: .cloneModules(aFeed.getModules()));
068:
069: if (((List) feed.getForeignMarkup()).size() > 0) {
070: syndFeed.setForeignMarkup((List) feed.getForeignMarkup());
071: }
072:
073: syndFeed.setEncoding(aFeed.getEncoding());
074:
075: syndFeed.setUri(aFeed.getId());
076:
077: Content aTitle = aFeed.getTitleEx();
078: if (aTitle != null) {
079: SyndContent c = new SyndContentImpl();
080: c.setType(aTitle.getType());
081: c.setValue(aTitle.getValue());
082: syndFeed.setTitleEx(c);
083: }
084:
085: Content aSubtitle = aFeed.getSubtitle();
086: if (aSubtitle != null) {
087: SyndContent c = new SyndContentImpl();
088: c.setType(aSubtitle.getType());
089: c.setValue(aSubtitle.getValue());
090: syndFeed.setDescriptionEx(c);
091: }
092:
093: // if there is exactly one alternate link, use that as THE link
094: if (aFeed.getAlternateLinks() != null
095: && aFeed.getAlternateLinks().size() == 1) {
096: Link theLink = (Link) aFeed.getAlternateLinks().get(0);
097: syndFeed.setLink(theLink.getHref());
098: }
099: // lump alternate and other links together
100: List syndLinks = new ArrayList();
101: if (aFeed.getAlternateLinks() != null
102: && aFeed.getAlternateLinks().size() > 0) {
103: syndLinks
104: .addAll(createSyndLinks(aFeed.getAlternateLinks()));
105: }
106: if (aFeed.getOtherLinks() != null
107: && aFeed.getOtherLinks().size() > 0) {
108: syndLinks.addAll(createSyndLinks(aFeed.getOtherLinks()));
109: }
110:
111: List aEntries = aFeed.getEntries();
112: if (aEntries != null) {
113: syndFeed.setEntries(createSyndEntries(aFeed, aEntries));
114: }
115:
116: // Core Atom language/author/copyright/modified elements have precedence
117: // over DC equivalent info.
118:
119: List authors = aFeed.getAuthors();
120: if (authors != null && authors.size() > 0) {
121: syndFeed.setAuthors(ConverterForAtom03
122: .createSyndPersons(authors));
123: }
124:
125: String rights = aFeed.getRights();
126: if (rights != null) {
127: syndFeed.setCopyright(rights);
128: }
129:
130: Date date = aFeed.getUpdated();
131: if (date != null) {
132: syndFeed.setPublishedDate(date);
133: }
134:
135: }
136:
137: protected List createSyndLinks(List aLinks) {
138: ArrayList sLinks = new ArrayList();
139: for (Iterator iter = aLinks.iterator(); iter.hasNext();) {
140: Link link = (Link) iter.next();
141: SyndLink sLink = new SyndLinkImpl();
142: sLink.setRel(link.getRel());
143: sLink.setHref(link.getHref());
144: sLink.setType(link.getType());
145: sLink.setTitle(link.getTitle());
146: sLink.setLength(link.getLength());
147: sLink.setHreflang(link.getHref());
148: sLinks.add(sLink);
149: }
150: return sLinks;
151: }
152:
153: protected List createSyndEntries(Feed feed, List atomEntries) {
154: List syndEntries = new ArrayList();
155: for (int i = 0; i < atomEntries.size(); i++) {
156: syndEntries.add(createSyndEntry(feed, (Entry) atomEntries
157: .get(i)));
158: }
159: return syndEntries;
160: }
161:
162: protected SyndEntry createSyndEntry(Feed feed, Entry entry) {
163: SyndEntry syndEntry = new SyndEntryImpl();
164: syndEntry.setModules(ModuleUtils.cloneModules(entry
165: .getModules()));
166:
167: if (((List) entry.getForeignMarkup()).size() > 0) {
168: syndEntry.setForeignMarkup((List) entry.getForeignMarkup());
169: }
170:
171: Content eTitle = entry.getTitleEx();
172: if (eTitle != null) {
173: syndEntry.setTitleEx(createSyndContent(eTitle));
174: }
175:
176: Content summary = entry.getSummary();
177: if (summary != null) {
178: syndEntry.setDescription(createSyndContent(summary));
179: }
180:
181: String id = entry.getId();
182: if (id != null) {
183: syndEntry.setUri(entry.getId());
184: } else {
185: syndEntry.setUri(syndEntry.getLink());
186: }
187:
188: List contents = entry.getContents();
189: if (contents != null && contents.size() > 0) {
190: List sContents = new ArrayList();
191: for (Iterator iter = contents.iterator(); iter.hasNext();) {
192: Content content = (Content) iter.next();
193: sContents.add(createSyndContent(content));
194: }
195: syndEntry.setContents(sContents);
196: }
197:
198: List authors = entry.getAuthors();
199: if (authors != null && authors.size() > 0) {
200: syndEntry.setAuthors(ConverterForAtom03
201: .createSyndPersons(authors));
202: SyndPerson person0 = (SyndPerson) syndEntry.getAuthors()
203: .get(0);
204: syndEntry.setAuthor(person0.getName());
205: }
206:
207: Date date = entry.getPublished();
208: if (date != null) {
209: syndEntry.setPublishedDate(date);
210: }
211:
212: date = entry.getUpdated();
213: if (date != null) {
214: syndEntry.setUpdatedDate(date);
215: }
216:
217: List categories = entry.getCategories();
218: if (categories != null) {
219: List syndCategories = new ArrayList();
220: for (Iterator iter = categories.iterator(); iter.hasNext();) {
221: Category c = (Category) iter.next();
222: SyndCategory syndCategory = new SyndCategoryImpl();
223: syndCategory.setName(c.getTerm());
224: syndCategory.setTaxonomyUri(c.getScheme());
225: // TODO: categories MAY have labels
226: // syndCategory.setLabel(c.getLabel());
227: syndCategories.add(syndCategory);
228: }
229: syndEntry.setCategories(syndCategories);
230: }
231:
232: // if there is exactly one alternate link, use that as THE link
233: if (entry.getAlternateLinks() != null
234: && entry.getAlternateLinks().size() == 1) {
235: Link theLink = (Link) entry.getAlternateLinks().get(0);
236: syndEntry.setLink(theLink.getHref());
237: }
238:
239: // Create synd enclosures from enclosure links
240: List syndEnclosures = new ArrayList();
241: if (entry.getOtherLinks() != null
242: && entry.getOtherLinks().size() > 0) {
243: List oLinks = entry.getOtherLinks();
244: for (Iterator iter = oLinks.iterator(); iter.hasNext();) {
245: Link this Link = (Link) iter.next();
246: if ("enclosure".equals(this Link.getRel()))
247: syndEnclosures.add(createSyndEnclosure(feed, entry,
248: this Link));
249: }
250: }
251: syndEntry.setEnclosures(syndEnclosures);
252:
253: // lump alternate and other links together
254: List syndLinks = new ArrayList();
255: if (entry.getAlternateLinks() != null
256: && entry.getAlternateLinks().size() > 0) {
257: syndLinks
258: .addAll(createSyndLinks(entry.getAlternateLinks()));
259: }
260: if (entry.getOtherLinks() != null
261: && entry.getOtherLinks().size() > 0) {
262: syndLinks.addAll(createSyndLinks(entry.getOtherLinks()));
263: }
264: syndEntry.setLinks(syndLinks);
265:
266: return syndEntry;
267: }
268:
269: public SyndEnclosure createSyndEnclosure(Feed feed, Entry entry,
270: Link link) {
271: SyndEnclosure syndEncl = new SyndEnclosureImpl();
272: syndEncl.setUrl(link.getHref());
273: syndEncl.setType(link.getType());
274: syndEncl.setLength(link.getLength());
275: return syndEncl;
276: }
277:
278: public SyndLink createSyndLink(Feed feed, Entry entry, Link link) {
279: SyndLink syndLink = new SyndLinkImpl();
280: syndLink.setRel(link.getRel());
281: syndLink.setType(link.getType());
282: syndLink.setHref(link.getHref());
283: syndLink.setHreflang(link.getHreflang());
284: syndLink.setLength(link.getLength());
285: return syndLink;
286: }
287:
288: public WireFeed createRealFeed(SyndFeed syndFeed) {
289: Feed aFeed = new Feed(getType());
290: aFeed.setModules(ModuleUtils
291: .cloneModules(syndFeed.getModules()));
292:
293: aFeed.setEncoding(syndFeed.getEncoding());
294:
295: aFeed.setId(syndFeed.getUri());
296:
297: SyndContent sTitle = syndFeed.getTitleEx();
298: if (sTitle != null) {
299: Content title = new Content();
300: title.setType(sTitle.getType());
301: title.setValue(sTitle.getValue());
302: aFeed.setTitleEx(title);
303: }
304:
305: SyndContent sDesc = syndFeed.getDescriptionEx();
306: if (sDesc != null) {
307: Content subtitle = new Content();
308: subtitle.setType(sDesc.getType());
309: subtitle.setValue(sDesc.getValue());
310: aFeed.setSubtitle(subtitle);
311: }
312:
313: // separate SyndEntry's links collection into alternate and other links
314: List alternateLinks = new ArrayList();
315: List otherLinks = new ArrayList();
316: String sLink = syndFeed.getLink();
317: List slinks = syndFeed.getLinks();
318: if (slinks != null) {
319: for (Iterator iter = slinks.iterator(); iter.hasNext();) {
320: SyndLink syndLink = (SyndLink) iter.next();
321: Link link = new Link();
322: link.setRel(syndLink.getRel());
323: link.setHref(syndLink.getHref());
324: link.setHreflang(syndLink.getHreflang());
325: link.setLength(syndLink.getLength());
326: if (link.getRel() == null
327: || "".equals(link.getRel().trim())
328: || "alternate".equals(syndLink.getRel())) {
329: alternateLinks.add(link);
330: } else {
331: otherLinks.add(link);
332: }
333: }
334: }
335: // no alternate link? then use THE link if there is one
336: if (alternateLinks.size() == 0 && syndFeed.getLink() != null) {
337: Link link = new Link();
338: link.setRel("alternate");
339: link.setHref(syndFeed.getLink());
340: alternateLinks.add(link);
341: }
342: if (alternateLinks.size() > 0)
343: aFeed.setAlternateLinks(alternateLinks);
344: if (otherLinks.size() > 0)
345: aFeed.setOtherLinks(otherLinks);
346:
347: List sCats = syndFeed.getCategories();
348: List aCats = new ArrayList();
349: if (sCats != null) {
350: for (Iterator iter = sCats.iterator(); iter.hasNext();) {
351: SyndCategory sCat = (SyndCategory) iter.next();
352: Category aCat = new Category();
353: aCat.setTerm(sCat.getName());
354: // TODO: aCat.setLabel(sCat.getLabel());
355: aCat.setScheme(sCat.getTaxonomyUri());
356: aCats.add(aCat);
357: }
358: }
359: if (aCats.size() > 0)
360: aFeed.setCategories(aCats);
361:
362: List authors = syndFeed.getAuthors();
363: if (authors != null && authors.size() > 0) {
364: aFeed.setAuthors(ConverterForAtom03
365: .createAtomPersons(authors));
366: }
367:
368: aFeed.setRights(syndFeed.getCopyright());
369:
370: aFeed.setUpdated(syndFeed.getPublishedDate());
371:
372: List sEntries = syndFeed.getEntries();
373: if (sEntries != null) {
374: aFeed.setEntries(createAtomEntries(sEntries));
375: }
376:
377: if (((List) syndFeed.getForeignMarkup()).size() > 0) {
378: aFeed.setForeignMarkup(syndFeed.getForeignMarkup());
379: }
380: return aFeed;
381: }
382:
383: protected SyndContent createSyndContent(Content content) {
384: SyndContent sContent = new SyndContentImpl();
385: sContent.setType(content.getType());
386: sContent.setValue(content.getValue());
387: return sContent;
388: }
389:
390: protected List createAtomEntries(List syndEntries) {
391: List atomEntries = new ArrayList();
392: for (int i = 0; i < syndEntries.size(); i++) {
393: atomEntries.add(createAtomEntry((SyndEntry) syndEntries
394: .get(i)));
395: }
396: return atomEntries;
397: }
398:
399: protected Content createAtomContent(SyndContent sContent) {
400: Content content = new Content();
401: content.setType(sContent.getType());
402: content.setValue(sContent.getValue());
403: return content;
404: }
405:
406: protected List createAtomContents(List syndContents) {
407: List atomContents = new ArrayList();
408: for (int i = 0; i < syndContents.size(); i++) {
409: atomContents
410: .add(createAtomContent((SyndContent) syndContents
411: .get(i)));
412: }
413: return atomContents;
414: }
415:
416: protected Entry createAtomEntry(SyndEntry sEntry) {
417: Entry aEntry = new Entry();
418: aEntry
419: .setModules(ModuleUtils.cloneModules(sEntry
420: .getModules()));
421:
422: aEntry.setId(sEntry.getUri());
423:
424: SyndContent sTitle = sEntry.getTitleEx();
425: if (sTitle != null) {
426: Content title = new Content();
427: title.setType(sTitle.getType());
428: title.setValue(sTitle.getValue());
429: aEntry.setTitleEx(title);
430: }
431:
432: SyndContent sDescription = sEntry.getDescription();
433: if (sDescription != null) {
434: Content summary = new Content();
435: summary.setType(sDescription.getType());
436: summary.setValue(sDescription.getValue());
437: aEntry.setSummary(summary);
438: }
439:
440: // separate SyndEntry's links collection into alternate and other links
441: List alternateLinks = new ArrayList();
442: List otherLinks = new ArrayList();
443: List slinks = sEntry.getLinks();
444: if (slinks != null) {
445: for (Iterator iter = slinks.iterator(); iter.hasNext();) {
446: SyndLink syndLink = (SyndLink) iter.next();
447: Link link = new Link();
448: link.setRel(syndLink.getRel());
449: link.setHref(syndLink.getHref());
450: link.setHreflang(syndLink.getHreflang());
451: link.setLength(syndLink.getLength());
452: link.setType(syndLink.getType());
453: if (link.getRel() == null
454: || "".equals(link.getRel().trim())
455: || "alternate".equals(syndLink.getRel())) {
456: alternateLinks.add(link);
457: } else {
458: otherLinks.add(link);
459: }
460: }
461: }
462: // no alternate link? then use THE link if there is one
463: if (alternateLinks.size() == 0 && sEntry.getLink() != null) {
464: Link link = new Link();
465: link.setRel("alternate");
466: link.setHref(sEntry.getLink());
467: alternateLinks.add(link);
468: }
469: if (alternateLinks.size() > 0)
470: aEntry.setAlternateLinks(alternateLinks);
471: if (otherLinks.size() > 0)
472: aEntry.setOtherLinks(otherLinks);
473:
474: List sCats = sEntry.getCategories();
475: List aCats = new ArrayList();
476: if (sCats != null) {
477: for (Iterator iter = sCats.iterator(); iter.hasNext();) {
478: SyndCategory sCat = (SyndCategory) iter.next();
479: Category aCat = new Category();
480: aCat.setTerm(sCat.getName());
481: // TODO: aCat.setLabel(sCat.getLabel());
482: aCat.setScheme(sCat.getTaxonomyUri());
483: aCats.add(aCat);
484: }
485: }
486: if (aCats.size() > 0)
487: aEntry.setCategories(aCats);
488:
489: List syndContents = sEntry.getContents();
490: aEntry.setContents(createAtomContents(syndContents));
491:
492: List authors = sEntry.getAuthors();
493: if (authors != null && authors.size() > 0) {
494: aEntry.setAuthors(ConverterForAtom03
495: .createAtomPersons(authors));
496: } else if (sEntry.getAuthor() != null) {
497: Person person = new Person();
498: person.setName(sEntry.getAuthor());
499: authors = new ArrayList();
500: authors.add(person);
501: aEntry.setAuthors(authors);
502: }
503:
504: aEntry.setPublished(sEntry.getPublishedDate());
505:
506: // Fix for issue #41 "Use updated instead of published"
507: // And issue #42 "Atom 1.0 Date (Updated or Published) Not Set"
508: // Atom requires an updated date, if it's missing use the published date
509: if (sEntry.getUpdatedDate() != null) {
510: aEntry.setUpdated(sEntry.getUpdatedDate());
511: } else {
512: aEntry.setUpdated(sEntry.getPublishedDate());
513: }
514:
515: if (((List) sEntry.getForeignMarkup()).size() > 0) {
516: aEntry.setForeignMarkup((List) sEntry.getForeignMarkup());
517: }
518: return aEntry;
519: }
520:
521: }
|