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 com.sun.syndication.feed.WireFeed;
020: import com.sun.syndication.feed.atom.Content;
021: import com.sun.syndication.feed.atom.Entry;
022: import com.sun.syndication.feed.atom.Feed;
023: import com.sun.syndication.feed.atom.Link;
024: import com.sun.syndication.feed.atom.Person;
025: import com.sun.syndication.feed.module.impl.ModuleUtils;
026: import com.sun.syndication.feed.synd.SyndFeed;
027: import com.sun.syndication.feed.synd.Converter;
028: import com.sun.syndication.feed.synd.SyndEntry;
029: import com.sun.syndication.feed.synd.SyndContentImpl;
030: import com.sun.syndication.feed.synd.SyndEntryImpl;
031: import com.sun.syndication.feed.synd.SyndContent;
032: import com.sun.syndication.feed.synd.SyndPerson;
033: import com.sun.syndication.feed.synd.SyndPersonImpl;
034:
035: import java.util.ArrayList;
036: import java.util.List;
037: import java.util.Date;
038: import java.util.Iterator;
039:
040: /**
041: */
042: public class ConverterForAtom03 implements Converter {
043: private String _type;
044:
045: public ConverterForAtom03() {
046: this ("atom_0.3");
047: }
048:
049: protected ConverterForAtom03(String type) {
050: _type = type;
051: }
052:
053: public String getType() {
054: return _type;
055: }
056:
057: public void copyInto(WireFeed feed, SyndFeed syndFeed) {
058: Feed aFeed = (Feed) feed;
059:
060: syndFeed.setModules(ModuleUtils
061: .cloneModules(aFeed.getModules()));
062:
063: if (((List) feed.getForeignMarkup()).size() > 0) {
064: syndFeed.setForeignMarkup(feed.getForeignMarkup());
065: }
066:
067: syndFeed.setEncoding(aFeed.getEncoding());
068:
069: syndFeed.setUri(aFeed.getId());
070:
071: syndFeed.setTitle(aFeed.getTitle());
072:
073: String linkHref = null;
074: if (aFeed.getAlternateLinks().size() > 0) {
075: linkHref = ((Link) aFeed.getAlternateLinks().get(0))
076: .getHref();
077: }
078: syndFeed.setLink(linkHref);
079:
080: Content tagline = aFeed.getTagline();
081: if (tagline != null) {
082: syndFeed.setDescription(tagline.getValue());
083: }
084:
085: List aEntries = aFeed.getEntries();
086: if (aEntries != null) {
087: syndFeed.setEntries(createSyndEntries(aEntries));
088: }
089:
090: // Core Atom language/author/copyright/modified elements have precedence
091: // over DC equivalent info.
092:
093: String language = aFeed.getLanguage();
094: if (language != null) {
095: syndFeed.setLanguage(language);
096: }
097:
098: List authors = aFeed.getAuthors();
099: if (authors != null && authors.size() > 0) {
100: syndFeed.setAuthors(createSyndPersons(authors));
101: }
102:
103: String copyright = aFeed.getCopyright();
104: if (copyright != null) {
105: syndFeed.setCopyright(copyright);
106: }
107:
108: Date date = aFeed.getModified();
109: if (date != null) {
110: syndFeed.setPublishedDate(date);
111: }
112:
113: }
114:
115: protected List createSyndEntries(List atomEntries) {
116: List syndEntries = new ArrayList();
117: for (int i = 0; i < atomEntries.size(); i++) {
118: syndEntries
119: .add(createSyndEntry((Entry) atomEntries.get(i)));
120: }
121: return syndEntries;
122: }
123:
124: protected SyndEntry createSyndEntry(Entry entry) {
125: SyndEntry syndEntry = new SyndEntryImpl();
126: syndEntry.setModules(ModuleUtils.cloneModules(entry
127: .getModules()));
128:
129: if (((List) entry.getForeignMarkup()).size() > 0) {
130: syndEntry.setForeignMarkup((List) entry.getForeignMarkup());
131: }
132:
133: syndEntry.setTitle(entry.getTitle());
134:
135: String linkHref = null;
136: if (entry.getAlternateLinks().size() > 0) {
137: linkHref = ((Link) entry.getAlternateLinks().get(0))
138: .getHref();
139: }
140: syndEntry.setLink(linkHref);
141:
142: String id = entry.getId();
143: if (id != null) {
144: syndEntry.setUri(entry.getId());
145: } else {
146: syndEntry.setUri(syndEntry.getLink());
147: }
148:
149: Content content = entry.getSummary();
150: if (content == null) {
151: List contents = entry.getContents();
152: if (contents != null && contents.size() > 0) {
153: content = (Content) contents.get(0);
154: }
155: }
156: if (content != null) {
157: SyndContent sContent = new SyndContentImpl();
158: sContent.setType(content.getType());
159: sContent.setValue(content.getValue());
160: syndEntry.setDescription(sContent);
161: }
162:
163: List contents = entry.getContents();
164: if (contents.size() > 0) {
165: List sContents = new ArrayList();
166: for (int i = 0; i < contents.size(); i++) {
167: content = (Content) contents.get(i);
168: SyndContent sContent = new SyndContentImpl();
169: sContent.setType(content.getType());
170: sContent.setValue(content.getValue());
171: sContent.setMode(content.getMode());
172: sContents.add(sContent);
173: }
174: syndEntry.setContents(sContents);
175: }
176:
177: List authors = entry.getAuthors();
178: if (authors != null && authors.size() > 0) {
179: syndEntry.setAuthors(createSyndPersons(authors));
180: SyndPerson person0 = (SyndPerson) syndEntry.getAuthors()
181: .get(0);
182: syndEntry.setAuthor(person0.getName());
183: }
184:
185: Date date = entry.getModified();
186: if (date == null) {
187: date = entry.getIssued();
188: if (date == null) {
189: date = entry.getCreated();
190: }
191: }
192: if (date != null) {
193: syndEntry.setPublishedDate(date);
194: }
195:
196: return syndEntry;
197: }
198:
199: public WireFeed createRealFeed(SyndFeed syndFeed) {
200: Feed aFeed = new Feed(getType());
201: aFeed.setModules(ModuleUtils
202: .cloneModules(syndFeed.getModules()));
203:
204: aFeed.setEncoding(syndFeed.getEncoding());
205:
206: aFeed.setId(syndFeed.getUri());
207:
208: aFeed.setTitle(syndFeed.getTitle());
209:
210: String sLink = syndFeed.getLink();
211: if (sLink != null) {
212: Link link = new Link();
213: link.setRel("alternate");
214: link.setHref(sLink);
215: List list = new ArrayList();
216: list.add(link);
217: aFeed.setAlternateLinks(list);
218: }
219:
220: String sDesc = syndFeed.getDescription();
221: if (sDesc != null) {
222: Content tagline = new Content();
223: tagline.setValue(sDesc);
224: aFeed.setTagline(tagline);
225: }
226:
227: aFeed.setLanguage(syndFeed.getLanguage());
228:
229: List authors = syndFeed.getAuthors();
230: if (authors != null && authors.size() > 0) {
231: aFeed.setAuthors(createAtomPersons(authors));
232: }
233:
234: aFeed.setCopyright(syndFeed.getCopyright());
235:
236: aFeed.setModified(syndFeed.getPublishedDate());
237:
238: List sEntries = syndFeed.getEntries();
239: if (sEntries != null) {
240: aFeed.setEntries(createAtomEntries(sEntries));
241: }
242:
243: return aFeed;
244: }
245:
246: protected static List createAtomPersons(List sPersons) {
247: List persons = new ArrayList();
248: for (Iterator iter = sPersons.iterator(); iter.hasNext();) {
249: SyndPerson sPerson = (SyndPerson) iter.next();
250: Person person = new Person();
251: person.setName(sPerson.getName());
252: person.setUri(sPerson.getUri());
253: person.setEmail(sPerson.getEmail());
254: persons.add(person);
255: }
256: return persons;
257: }
258:
259: protected static List createSyndPersons(List aPersons) {
260: List persons = new ArrayList();
261: for (Iterator iter = aPersons.iterator(); iter.hasNext();) {
262: Person aPerson = (Person) iter.next();
263: SyndPerson person = new SyndPersonImpl();
264: person.setName(aPerson.getName());
265: person.setUri(aPerson.getUri());
266: person.setEmail(aPerson.getEmail());
267: persons.add(person);
268: }
269: return persons;
270: }
271:
272: protected List createAtomEntries(List syndEntries) {
273: List atomEntries = new ArrayList();
274: for (int i = 0; i < syndEntries.size(); i++) {
275: atomEntries.add(createAtomEntry((SyndEntry) syndEntries
276: .get(i)));
277: }
278: return atomEntries;
279: }
280:
281: protected Entry createAtomEntry(SyndEntry sEntry) {
282: Entry aEntry = new Entry();
283: aEntry
284: .setModules(ModuleUtils.cloneModules(sEntry
285: .getModules()));
286:
287: aEntry.setId(sEntry.getUri());
288:
289: aEntry.setTitle(sEntry.getTitle());
290:
291: String sLink = sEntry.getLink();
292: if (sLink != null) {
293: Link link = new Link();
294: link.setRel("alternate");
295: link.setHref(sLink);
296: List list = new ArrayList();
297: list.add(link);
298: aEntry.setAlternateLinks(list);
299: }
300:
301: SyndContent sContent = sEntry.getDescription();
302: if (sContent != null) {
303: Content content = new Content();
304: content.setType(sContent.getType());
305: content.setValue(sContent.getValue());
306: content.setMode(Content.ESCAPED);
307: aEntry.setSummary(content);
308: }
309:
310: List contents = sEntry.getContents();
311: if (contents.size() > 0) {
312: List aContents = new ArrayList();
313: for (int i = 0; i < contents.size(); i++) {
314: sContent = (SyndContentImpl) contents.get(i);
315: Content content = new Content();
316: content.setType(sContent.getType());
317: content.setValue(sContent.getValue());
318: content.setMode(sContent.getMode());
319: aContents.add(content);
320:
321: }
322: aEntry.setContents(aContents);
323: }
324:
325: List sAuthors = sEntry.getAuthors();
326: if (sAuthors != null && sAuthors.size() > 0) {
327: aEntry.setAuthors(createAtomPersons(sAuthors));
328: } else if (sEntry.getAuthor() != null) {
329: Person person = new Person();
330: person.setName(sEntry.getAuthor());
331: List authors = new ArrayList();
332: authors.add(person);
333: aEntry.setAuthors(authors);
334: }
335:
336: aEntry.setModified(sEntry.getPublishedDate());
337: aEntry.setIssued(sEntry.getPublishedDate());
338:
339: return aEntry;
340: }
341:
342: }
|