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.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 import com.sun.syndication.feed.synd.SyndPerson;
33 import com.sun.syndication.feed.synd.SyndPersonImpl;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Date;
38 import java.util.Iterator;
39
40 /***
41 */
42 public class ConverterForAtom03 implements Converter {
43 private String _type;
44
45 public ConverterForAtom03() {
46 this("atom_0.3");
47 }
48
49 protected ConverterForAtom03(String type) {
50 _type = type;
51 }
52
53 public String getType() {
54 return _type;
55 }
56
57 public void copyInto(WireFeed feed,SyndFeed syndFeed) {
58 Feed aFeed = (Feed) feed;
59
60 syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
61
62 if (((List)feed.getForeignMarkup()).size() > 0) {
63 syndFeed.setForeignMarkup(feed.getForeignMarkup());
64 }
65
66 syndFeed.setEncoding(aFeed.getEncoding());
67
68 syndFeed.setUri(aFeed.getId());
69
70 syndFeed.setTitle(aFeed.getTitle());
71
72 String linkHref = null;
73 if (aFeed.getAlternateLinks().size() > 0) {
74 linkHref = ((Link) aFeed.getAlternateLinks().get(0)).getHref();
75 }
76 syndFeed.setLink(linkHref);
77
78 Content tagline = aFeed.getTagline();
79 if (tagline!=null) {
80 syndFeed.setDescription(tagline.getValue());
81 }
82
83
84 List aEntries = aFeed.getEntries();
85 if (aEntries!=null) {
86 syndFeed.setEntries(createSyndEntries(aEntries));
87 }
88
89
90
91
92 String language = aFeed.getLanguage();
93 if (language!=null) {
94 syndFeed.setLanguage(language);
95 }
96
97 List authors = aFeed.getAuthors();
98 if (authors!=null && authors.size() > 0) {
99 syndFeed.setAuthors(createSyndPersons(authors));
100 }
101
102 String copyright = aFeed.getCopyright();
103 if (copyright!=null) {
104 syndFeed.setCopyright(copyright);
105 }
106
107 Date date = aFeed.getModified();
108 if (date!=null) {
109 syndFeed.setPublishedDate(date);
110 }
111
112 }
113
114 protected List createSyndEntries(List atomEntries) {
115 List syndEntries = new ArrayList();
116 for (int i=0;i<atomEntries.size();i++) {
117 syndEntries.add(createSyndEntry((Entry) atomEntries.get(i)));
118 }
119 return syndEntries;
120 }
121
122 protected SyndEntry createSyndEntry(Entry entry) {
123 SyndEntry syndEntry = new SyndEntryImpl();
124 syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
125
126 if (((List)entry.getForeignMarkup()).size() > 0) {
127 syndEntry.setForeignMarkup((List)entry.getForeignMarkup());
128 }
129
130 syndEntry.setTitle(entry.getTitle());
131
132 String linkHref = null;
133 if (entry.getAlternateLinks().size() > 0) {
134 linkHref = ((Link) entry.getAlternateLinks().get(0)).getHref();
135 }
136 syndEntry.setLink(linkHref);
137
138
139 String id = entry.getId();
140 if (id!=null) {
141 syndEntry.setUri(entry.getId());
142 }
143 else {
144 syndEntry.setUri(syndEntry.getLink());
145 }
146
147 Content content = entry.getSummary();
148 if (content==null) {
149 List contents = entry.getContents();
150 if (contents!=null && contents.size()>0) {
151 content = (Content) contents.get(0);
152 }
153 }
154 if (content!=null) {
155 SyndContent sContent = new SyndContentImpl();
156 sContent.setType(content.getType());
157 sContent.setValue(content.getValue());
158 syndEntry.setDescription(sContent);
159 }
160
161 List contents = entry.getContents();
162 if (contents.size()>0) {
163 List sContents = new ArrayList();
164 for (int i=0;i<contents.size();i++) {
165 content = (Content) contents.get(i);
166 SyndContent sContent = new SyndContentImpl();
167 sContent.setType(content.getType());
168 sContent.setValue(content.getValue());
169 sContent.setMode(content.getMode());
170 sContents.add(sContent);
171 }
172 syndEntry.setContents(sContents);
173 }
174
175 List authors = entry.getAuthors();
176 if (authors!=null && authors.size() > 0) {
177 syndEntry.setAuthors(createSyndPersons(authors));
178 SyndPerson person0 = (SyndPerson)syndEntry.getAuthors().get(0);
179 syndEntry.setAuthor(person0.getName());
180 }
181
182 Date date = entry.getModified();
183 if (date==null) {
184 date = entry.getIssued();
185 if (date==null) {
186 date = entry.getCreated();
187 }
188 }
189 if (date!=null) {
190 syndEntry.setPublishedDate(date);
191 }
192
193 return syndEntry;
194 }
195
196 public WireFeed createRealFeed(SyndFeed syndFeed) {
197 Feed aFeed = new Feed(getType());
198 aFeed.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
199
200 aFeed.setEncoding(syndFeed.getEncoding());
201
202 aFeed.setId(syndFeed.getUri());
203
204 aFeed.setTitle(syndFeed.getTitle());
205
206 String sLink = syndFeed.getLink();
207 if (sLink!=null) {
208 Link link = new Link();
209 link.setRel("alternate");
210 link.setHref(sLink);
211 List list = new ArrayList();
212 list.add(link);
213 aFeed.setAlternateLinks(list);
214 }
215
216 String sDesc = syndFeed.getDescription();
217 if (sDesc!=null) {
218 Content tagline = new Content();
219 tagline.setValue(sDesc);
220 aFeed.setTagline(tagline);
221 }
222
223 aFeed.setLanguage(syndFeed.getLanguage());
224
225 List authors = syndFeed.getAuthors();
226 if (authors!=null && authors.size() > 0) {
227 aFeed.setAuthors(createAtomPersons(authors));
228 }
229
230 aFeed.setCopyright(syndFeed.getCopyright());
231
232 aFeed.setModified(syndFeed.getPublishedDate());
233
234 List sEntries = syndFeed.getEntries();
235 if (sEntries!=null) {
236 aFeed.setEntries(createAtomEntries(sEntries));
237 }
238
239 return aFeed;
240 }
241
242 protected static List createAtomPersons(List sPersons) {
243 List persons = new ArrayList();
244 for (Iterator iter = sPersons.iterator(); iter.hasNext(); ) {
245 SyndPerson sPerson = (SyndPerson)iter.next();
246 Person person = new Person();
247 person.setName(sPerson.getName());
248 person.setUri(sPerson.getUri());
249 person.setEmail(sPerson.getEmail());
250 persons.add(person);
251 }
252 return persons;
253 }
254
255 protected static List createSyndPersons(List aPersons) {
256 List persons = new ArrayList();
257 for (Iterator iter = aPersons.iterator(); iter.hasNext(); ) {
258 Person aPerson = (Person)iter.next();
259 SyndPerson person = new SyndPersonImpl();
260 person.setName(aPerson.getName());
261 person.setUri(aPerson.getUri());
262 person.setEmail(aPerson.getEmail());
263 persons.add(person);
264 }
265 return persons;
266 }
267
268 protected List createAtomEntries(List syndEntries) {
269 List atomEntries = new ArrayList();
270 for (int i=0;i<syndEntries.size();i++) {
271 atomEntries.add(createAtomEntry((SyndEntry)syndEntries.get(i)));
272 }
273 return atomEntries;
274 }
275
276 protected Entry createAtomEntry(SyndEntry sEntry) {
277 Entry aEntry = new Entry();
278 aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
279
280 aEntry.setId(sEntry.getUri());
281
282 aEntry.setTitle(sEntry.getTitle());
283
284 String sLink = sEntry.getLink();
285 if (sLink!=null) {
286 Link link = new Link();
287 link.setRel("alternate");
288 link.setHref(sLink);
289 List list = new ArrayList();
290 list.add(link);
291 aEntry.setAlternateLinks(list);
292 }
293
294 SyndContent sContent = sEntry.getDescription();
295 if (sContent!=null) {
296 Content content = new Content();
297 content.setType(sContent.getType());
298 content.setValue(sContent.getValue());
299 content.setMode(Content.ESCAPED);
300 aEntry.setSummary(content);
301 }
302
303 List contents = sEntry.getContents();
304 if (contents.size()>0) {
305 List aContents = new ArrayList();
306 for (int i=0;i<contents.size();i++) {
307 sContent = (SyndContentImpl) contents.get(i);
308 Content content = new Content();
309 content.setType(sContent.getType());
310 content.setValue(sContent.getValue());
311 content.setMode(sContent.getMode());
312 aContents.add(content);
313
314 }
315 aEntry.setContents(aContents);
316 }
317
318 List sAuthors = sEntry.getAuthors();
319 if (sAuthors!=null && sAuthors.size() > 0) {
320 aEntry.setAuthors(createAtomPersons(sAuthors));
321 } else if (sEntry.getAuthor() != null) {
322 Person person = new Person();
323 person.setName(sEntry.getAuthor());
324 List authors = new ArrayList();
325 authors.add(person);
326 aEntry.setAuthors(authors);
327 }
328
329 aEntry.setModified(sEntry.getPublishedDate());
330 aEntry.setIssued(sEntry.getPublishedDate());
331
332 return aEntry;
333 }
334
335 }