KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > feed > synd > impl > ConverterForAtom03


1 /*
2  * Copyright 2004 Sun Microsystems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Date JavaDoc;
36
37 /**
38  */

39 public class ConverterForAtom03 implements Converter {
40     private String JavaDoc _type;
41
42     public ConverterForAtom03() {
43         this("atom_0.3");
44     }
45
46     protected ConverterForAtom03(String JavaDoc type) {
47         _type = type;
48     }
49
50     public String JavaDoc getType() {
51         return _type;
52     }
53
54     public void copyInto(WireFeed feed,SyndFeed syndFeed) {
55         Feed aFeed = (Feed) feed;
56
57         syndFeed.setModules(ModuleUtils.cloneModules(aFeed.getModules()));
58
59         syndFeed.setEncoding(aFeed.getEncoding());
60
61         syndFeed.setUri(aFeed.getId());
62
63         syndFeed.setTitle(aFeed.getTitle());
64
65         Link link = (Link) aFeed.getAlternateLinks().get(0);
66         syndFeed.setLink(link.getHref());
67
68         Content info = aFeed.getInfo();
69         if (info!=null) {
70             syndFeed.setDescription(info.getValue());
71         }
72
73
74         List JavaDoc aEntries = aFeed.getEntries();
75         if (aEntries!=null) {
76             syndFeed.setEntries(createSyndEntries(aEntries));
77         }
78
79         // Core Atom language/author/copyright/modified elements have precedence
80
// over DC equivalent info.
81

82         String JavaDoc language = aFeed.getLanguage();
83         if (language!=null) {
84             syndFeed.setLanguage(language);
85         }
86
87         Person author = aFeed.getAuthor();
88         if (author!=null && author.getName()!=null) {
89             syndFeed.setAuthor(author.getName());
90         }
91
92         String JavaDoc copyright = aFeed.getCopyright();
93         if (copyright!=null) {
94             syndFeed.setCopyright(copyright);
95         }
96
97         Date JavaDoc date = aFeed.getModified();
98         if (date!=null) {
99             syndFeed.setPublishedDate(date);
100         }
101
102     }
103
104     protected List JavaDoc createSyndEntries(List JavaDoc atomEntries) {
105         List JavaDoc syndEntries = new ArrayList JavaDoc();
106         for (int i=0;i<atomEntries.size();i++) {
107             syndEntries.add(createSyndEntry((Entry) atomEntries.get(i)));
108         }
109         return syndEntries;
110     }
111
112     protected SyndEntry createSyndEntry(Entry entry) {
113         SyndEntry syndEntry = new SyndEntryImpl();
114         syndEntry.setModules(ModuleUtils.cloneModules(entry.getModules()));
115
116         syndEntry.setTitle(entry.getTitle());
117
118         Link link = (Link) entry.getAlternateLinks().get(0);
119         syndEntry.setLink(link.getHref());
120
121         String JavaDoc id = entry.getId();
122         if (id!=null) {
123             syndEntry.setUri(entry.getId());
124         }
125         else {
126             syndEntry.setUri(syndEntry.getLink());
127         }
128
129         Content content = entry.getSummary();
130         if (content==null) {
131             List JavaDoc contents = entry.getContents();
132             if (contents!=null && contents.size()>0) {
133                 content = (Content) contents.get(0);
134             }
135         }
136         if (content!=null) {
137             SyndContent sContent = new SyndContentImpl();
138             sContent.setType(content.getType());
139             sContent.setValue(content.getValue());
140             syndEntry.setDescription(sContent);
141         }
142
143         List JavaDoc contents = entry.getContents();
144         if (contents.size()>0) {
145             List JavaDoc sContents = new ArrayList JavaDoc();
146             for (int i=0;i<contents.size();i++) {
147                 content = (Content) contents.get(i);
148                 SyndContent sContent = new SyndContentImpl();
149                 sContent.setType(content.getType());
150                 sContent.setValue(content.getValue());
151                 sContents.add(sContent);
152             }
153             syndEntry.setContents(sContents);
154         }
155
156         Person author = entry.getAuthor();
157         if (author!=null && author.getName()!=null) {
158             syndEntry.setAuthor(author.getName());
159         }
160
161         Date JavaDoc date = entry.getModified();
162         if (date!=null) {
163             syndEntry.setPublishedDate(date);
164         }
165
166         return syndEntry;
167     }
168
169     public WireFeed createRealFeed(SyndFeed syndFeed) {
170         Feed aFeed = new Feed(getType());
171         aFeed.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
172
173         aFeed.setEncoding(syndFeed.getEncoding());
174
175         aFeed.setId(syndFeed.getUri());
176
177         aFeed.setTitle(syndFeed.getTitle());
178
179         String JavaDoc sLink = syndFeed.getLink();
180         if (sLink!=null) {
181             Link link = new Link();
182             link.setRel("alternate");
183             link.setHref(sLink);
184             List JavaDoc list = new ArrayList JavaDoc();
185             list.add(link);
186             aFeed.setAlternateLinks(list);
187         }
188
189         String JavaDoc sDesc = syndFeed.getDescription();
190         if (sDesc!=null) {
191             Content info = new Content();
192             info.setValue(sDesc);
193             aFeed.setInfo(info);
194         }
195
196         aFeed.setLanguage(syndFeed.getLanguage());
197
198         String JavaDoc sAuthor = syndFeed.getAuthor();
199         if (sAuthor!=null) {
200             Person person = new Person();
201             person.setName(sAuthor);
202             aFeed.setAuthor(person);
203         }
204
205         aFeed.setCopyright(syndFeed.getCopyright());
206
207         aFeed.setModified(syndFeed.getPublishedDate());
208
209         List JavaDoc sEntries = syndFeed.getEntries();
210         if (sEntries!=null) {
211             aFeed.setEntries(createAtomEntries(sEntries));
212         }
213
214         return aFeed;
215     }
216
217
218     protected List JavaDoc createAtomEntries(List JavaDoc syndEntries) {
219         List JavaDoc atomEntries = new ArrayList JavaDoc();
220         for (int i=0;i<syndEntries.size();i++) {
221             atomEntries.add(createAtomEntry((SyndEntry)syndEntries.get(i)));
222         }
223         return atomEntries;
224     }
225
226     protected Entry createAtomEntry(SyndEntry sEntry) {
227         Entry aEntry = new Entry();
228         aEntry.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
229
230         aEntry.setId(sEntry.getUri());
231
232         aEntry.setTitle(sEntry.getTitle());
233
234         String JavaDoc sLink = sEntry.getLink();
235         if (sLink!=null) {
236             Link link = new Link();
237             link.setRel("alternate");
238             link.setHref(sLink);
239             List JavaDoc list = new ArrayList JavaDoc();
240             list.add(link);
241             aEntry.setAlternateLinks(list);
242         }
243
244         SyndContent sContent = sEntry.getDescription();
245         if (sContent!=null) {
246             Content content = new Content();
247             content.setType(sContent.getType());
248             content.setValue(sContent.getValue());
249             content.setMode(Content.ESCAPED);
250             aEntry.setSummary(content);
251         }
252
253         List JavaDoc contents = sEntry.getContents();
254         if (contents.size()>0) {
255             List JavaDoc aContents = new ArrayList JavaDoc();
256             for (int i=0;i<contents.size();i++) {
257                 sContent = (SyndContentImpl) contents.get(i);
258                 Content content = new Content();
259                 content.setType(sContent.getType());
260                 content.setValue(sContent.getValue());
261                 content.setMode(Content.ESCAPED);
262                 aContents.add(content);
263
264             }
265             aEntry.setContents(aContents);
266         }
267
268         String JavaDoc sAuthor = sEntry.getAuthor();
269         if (sAuthor!=null) {
270             Person person = new Person();
271             person.setName(sAuthor);
272             aEntry.setAuthor(person);
273         }
274
275         aEntry.setModified(sEntry.getPublishedDate());
276
277         return aEntry;
278     }
279
280 }
281
Popular Tags