1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.io.impl;
18
19 import com.sun.syndication.feed.WireFeed;
20 import com.sun.syndication.feed.atom.*;
21 import com.sun.syndication.io.FeedException;
22 import org.jdom.Document;
23 import org.jdom.Element;
24 import org.jdom.Namespace;
25 import org.jdom.output.XMLOutputter;
26
27 import java.util.*;
28
29 /***
30 */
31 public class Atom03Parser extends BaseWireFeedParser {
32 private static final String ATOM_03_URI = "http://purl.org/atom/ns#";
33
34 public Atom03Parser() {
35 this("atom_0.3");
36 }
37
38 protected Atom03Parser(String type) {
39 super(type);
40 }
41
42 protected Namespace getAtomNamespace() {
43 return Namespace.getNamespace(ATOM_03_URI);
44 }
45
46 public boolean isMyType(Document document) {
47 Element rssRoot = document.getRootElement();
48 Namespace defaultNS = rssRoot.getNamespace();
49 return (defaultNS!=null) && defaultNS.equals(getAtomNamespace());
50 }
51
52 public WireFeed parse(Document document, boolean validate) throws IllegalArgumentException,FeedException {
53 if (validate) {
54 validateFeed(document);
55 }
56 Element rssRoot = document.getRootElement();
57 return parseFeed(rssRoot);
58 }
59
60 protected void validateFeed(Document document) throws FeedException {
61
62
63
64
65
66
67 }
68
69 protected WireFeed parseFeed(Element eFeed) {
70
71 com.sun.syndication.feed.atom.Feed feed = new com.sun.syndication.feed.atom.Feed(getType());
72
73 Element e = eFeed.getChild("title",getAtomNamespace());
74 if (e!=null) {
75 feed.setTitle(e.getText());
76 }
77
78 List eList = eFeed.getChildren("link",getAtomNamespace());
79 feed.setAlternateLinks(parseAlternateLinks(eList));
80 feed.setOtherLinks(parseOtherLinks(eList));
81
82 e = eFeed.getChild("author",getAtomNamespace());
83 if (e!=null) {
84 List authors = new ArrayList();
85 authors.add(parsePerson(e));
86 feed.setAuthors(authors);
87 }
88
89 eList = eFeed.getChildren("contributor",getAtomNamespace());
90 if (eList.size()>0) {
91 feed.setContributors(parsePersons(eList));
92 }
93
94 e = eFeed.getChild("tagline",getAtomNamespace());
95 if (e!=null) {
96 feed.setTagline(parseContent(e));
97 }
98
99 e = eFeed.getChild("id",getAtomNamespace());
100 if (e!=null) {
101 feed.setId(e.getText());
102 }
103
104 e = eFeed.getChild("generator",getAtomNamespace());
105 if (e!=null) {
106 Generator gen = new Generator();
107 gen.setValue(e.getText());
108 String att = e.getAttributeValue("url");
109 if (att!=null) {
110 gen.setUrl(att);
111 }
112 att = e.getAttributeValue("version");
113 if (att!=null) {
114 gen.setVersion(att);
115 }
116 feed.setGenerator(gen);
117 }
118
119 e = eFeed.getChild("copyright",getAtomNamespace());
120 if (e!=null) {
121 feed.setCopyright(e.getText());
122 }
123
124 e = eFeed.getChild("info",getAtomNamespace());
125 if (e!=null) {
126 feed.setInfo(parseContent(e));
127 }
128
129 e = eFeed.getChild("modified",getAtomNamespace());
130 if (e!=null) {
131 feed.setModified(DateParser.parseDate(e.getText()));
132 }
133
134 eList = eFeed.getChildren("entry",getAtomNamespace());
135 if (eList.size()>0) {
136 feed.setEntries(parseEntries(eList));
137 }
138
139 feed.setModules(parseFeedModules(eFeed));
140
141 return feed;
142 }
143
144 private Link parseLink(Element eLink) {
145 Link link = new Link();
146 String att = eLink.getAttributeValue("rel");
147 if (att!=null) {
148 link.setRel(att);
149 }
150 att = eLink.getAttributeValue("type");
151 if (att!=null) {
152 link.setType(att);
153 }
154 att = eLink.getAttributeValue("href");
155 if (att!=null) {
156 link.setHref(att);
157 }
158 return link;
159 }
160
161
162 private List parseLinks(List eLinks,boolean alternate) {
163 List links = new ArrayList();
164 for (int i=0;i<eLinks.size();i++) {
165 Element eLink = (Element) eLinks.get(i);
166
167 String rel = eLink.getAttributeValue("rel");
168 if (alternate) {
169 if ("alternate".equals(rel)) {
170 links.add(parseLink(eLink));
171 }
172 }
173 else {
174 if (!("alternate".equals(rel))) {
175 links.add(parseLink(eLink));
176 }
177 }
178 }
179 return (links.size()>0) ? links : null;
180 }
181
182
183 private List parseAlternateLinks(List eLinks) {
184 return parseLinks(eLinks,true);
185 }
186
187
188 private List parseOtherLinks(List eLinks) {
189 return parseLinks(eLinks,false);
190 }
191
192 private Person parsePerson(Element ePerson) {
193 Person person = new Person();
194 Element e = ePerson.getChild("name",getAtomNamespace());
195 if (e!=null) {
196 person.setName(e.getText());
197 }
198 e = ePerson.getChild("url",getAtomNamespace());
199 if (e!=null) {
200 person.setUrl(e.getText());
201 }
202 e = ePerson.getChild("email",getAtomNamespace());
203 if (e!=null) {
204 person.setEmail(e.getText());
205 }
206 return person;
207 }
208
209
210 private List parsePersons(List ePersons) {
211 List persons = new ArrayList();
212 for (int i=0;i<ePersons.size();i++) {
213 persons.add(parsePerson((Element)ePersons.get(i)));
214 }
215 return (persons.size()>0) ? persons : null;
216 }
217
218 private Content parseContent(Element e) {
219 String value = null;
220 String type = e.getAttributeValue("type");
221 type = (type!=null) ? type : "text/plain";
222 String mode = e.getAttributeValue("mode");
223 if (mode == null) {
224 mode = Content.XML;
225 }
226 if (mode.equals(Content.ESCAPED)) {
227
228 value = e.getText();
229 }
230 else
231 if (mode.equals(Content.BASE64)) {
232 value = Base64.decode(e.getText());
233 }
234 else
235 if (mode.equals(Content.XML)) {
236 XMLOutputter outputter = new XMLOutputter();
237 List eContent = e.getContent();
238 Iterator i = eContent.iterator();
239 while (i.hasNext()) {
240 org.jdom.Content c = (org.jdom.Content) i.next();
241 if (c instanceof Element) {
242 Element eC = (Element) c;
243 if (eC.getNamespace().equals(getAtomNamespace())) {
244 ((Element)c).setNamespace(Namespace.NO_NAMESPACE);
245 }
246 }
247 }
248 value = outputter.outputString(eContent);
249 }
250
251 Content content = new Content();
252 content.setType(type);
253 content.setMode(mode);
254 content.setValue(value);
255 return content;
256 }
257
258
259 private List parseEntries(List eEntries) {
260 List entries = new ArrayList();
261 for (int i=0;i<eEntries.size();i++) {
262 entries.add(parseEntry((Element)eEntries.get(i)));
263 }
264 return (entries.size()>0) ? entries : null;
265 }
266
267 private Entry parseEntry(Element eEntry) {
268 Entry entry = new Entry();
269
270 Element e = eEntry.getChild("title",getAtomNamespace());
271 if (e!=null) {
272 entry.setTitle(e.getText());
273 }
274
275 List eList = eEntry.getChildren("link",getAtomNamespace());
276 entry.setAlternateLinks(parseAlternateLinks(eList));
277 entry.setOtherLinks(parseOtherLinks(eList));
278
279 e = eEntry.getChild("author",getAtomNamespace());
280 if (e!=null) {
281 List authors = new ArrayList();
282 authors.add(parsePerson(e));
283 entry.setAuthors(authors);
284 }
285
286 eList = eEntry.getChildren("contributor",getAtomNamespace());
287 if (eList.size()>0) {
288 entry.setContributors(parsePersons(eList));
289 }
290
291 e = eEntry.getChild("id",getAtomNamespace());
292 if (e!=null) {
293 entry.setId(e.getText());
294 }
295
296 e = eEntry.getChild("modified",getAtomNamespace());
297 if (e!=null) {
298 entry.setModified(DateParser.parseDate(e.getText()));
299 }
300
301 e = eEntry.getChild("issued",getAtomNamespace());
302 if (e!=null) {
303 entry.setIssued(DateParser.parseDate(e.getText()));
304 }
305
306 e = eEntry.getChild("created",getAtomNamespace());
307 if (e!=null) {
308 entry.setCreated(DateParser.parseDate(e.getText()));
309 }
310
311 e = eEntry.getChild("summary",getAtomNamespace());
312 if (e!=null) {
313 entry.setSummary(parseContent(e));
314 }
315
316 eList = eEntry.getChildren("content",getAtomNamespace());
317 if (eList.size()>0) {
318 List content = new ArrayList();
319 for (int i=0;i<eList.size();i++) {
320 content.add(parseContent((Element)eList.get(i)));
321 }
322 entry.setContents(content);
323 }
324
325 entry.setModules(parseItemModules(eEntry));
326
327 return entry;
328 }
329
330
331 }