KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > io > impl > Atom03Parser


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.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 JavaDoc ATOM_URI = "http://purl.org/atom/ns#";
33
34     public Atom03Parser() {
35         this("atom_0.3");
36     }
37
38     protected Atom03Parser(String JavaDoc type) {
39         super(type);
40     }
41
42     protected Namespace getAtomNamespace() {
43         return Namespace.getNamespace(ATOM_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 JavaDoc,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         // TBD
62
// here we have to validate the Feed against a schema or whatever
63
// not sure how to do it
64
// one posibility would be to produce an ouput and attempt to parse it again
65
// with validation turned on.
66
// otherwise will have to check the document elements by hand.
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             feed.setAuthor(parsePerson(e));
85         }
86
87         eList = eFeed.getChildren("contributor",getAtomNamespace());
88         if (eList.size()>0) {
89             feed.setContributors(parsePersons(eList));
90         }
91
92         e = eFeed.getChild("tagline",getAtomNamespace());
93         if (e!=null) {
94             feed.setTagline(parseContent(e));
95         }
96
97         e = eFeed.getChild("id",getAtomNamespace());
98         if (e!=null) {
99             feed.setId(e.getText());
100         }
101
102         e = eFeed.getChild("generator",getAtomNamespace());
103         if (e!=null) {
104             Generator gen = new Generator();
105             gen.setValue(e.getText());
106             String JavaDoc att = e.getAttributeValue("url");//getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
107
if (att!=null) {
108                 gen.setUrl(att);
109             }
110             att = e.getAttributeValue("version");//getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
111
if (att!=null) {
112                 gen.setVersion(att);
113             }
114             feed.setGenerator(gen);
115         }
116
117         e = eFeed.getChild("copyright",getAtomNamespace());
118         if (e!=null) {
119             feed.setCopyright(e.getText());
120         }
121
122         e = eFeed.getChild("info",getAtomNamespace());
123         if (e!=null) {
124             feed.setInfo(parseContent(e));
125         }
126
127         e = eFeed.getChild("modified",getAtomNamespace());
128         if (e!=null) {
129             feed.setModified(DateParser.parseW3CDateTime(e.getText()));
130         }
131
132         eList = eFeed.getChildren("entry",getAtomNamespace());
133         if (eList.size()>0) {
134             feed.setEntries(parseEntries(eList));
135         }
136
137         feed.setModules(parseFeedModules(eFeed));
138
139         return feed;
140     }
141
142     private Link parseLink(Element eLink) {
143         Link link = new Link();
144         String JavaDoc att = eLink.getAttributeValue("rel");//getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
145
if (att!=null) {
146             link.setRel(att);
147         }
148         att = eLink.getAttributeValue("type");//getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
149
if (att!=null) {
150             link.setType(att);
151         }
152         att = eLink.getAttributeValue("href");//getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
153
if (att!=null) {
154             link.setHref(att);
155         }
156         return link;
157     }
158
159     // List(Elements) -> List(Link)
160
private List parseLinks(List eLinks,boolean alternate) {
161         List links = new ArrayList();
162         for (int i=0;i<eLinks.size();i++) {
163             Element eLink = (Element) eLinks.get(i);
164             //Namespace ns = getAtomNamespace();
165
String JavaDoc rel = eLink.getAttributeValue("rel");//getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
166
if (alternate) {
167                 if ("alternate".equals(rel)) {
168                     links.add(parseLink(eLink));
169                 }
170             }
171             else {
172                 if (!("alternate".equals(rel))) {
173                     links.add(parseLink(eLink));
174                 }
175             }
176         }
177         return (links.size()>0) ? links : null;
178     }
179
180     // List(Elements) -> List(Link)
181
private List parseAlternateLinks(List eLinks) {
182         return parseLinks(eLinks,true);
183     }
184
185     // List(Elements) -> List(Link)
186
private List parseOtherLinks(List eLinks) {
187         return parseLinks(eLinks,false);
188     }
189
190     private Person parsePerson(Element ePerson) {
191         Person person = new Person();
192         Element e = ePerson.getChild("name",getAtomNamespace());
193         if (e!=null) {
194             person.setName(e.getText());
195         }
196         e = ePerson.getChild("url",getAtomNamespace());
197         if (e!=null) {
198             person.setUrl(e.getText());
199         }
200         e = ePerson.getChild("email",getAtomNamespace());
201         if (e!=null) {
202             person.setEmail(e.getText());
203         }
204         return person;
205     }
206
207     // List(Elements) -> List(Persons)
208
private List parsePersons(List ePersons) {
209         List persons = new ArrayList();
210         for (int i=0;i<ePersons.size();i++) {
211             persons.add(parsePerson((Element)ePersons.get(i)));
212         }
213         return (persons.size()>0) ? persons : null;
214     }
215
216     private Content parseContent(Element e) {
217         String JavaDoc value = null;
218         String JavaDoc type = e.getAttributeValue("type");//getAtomNamespace()); DONT KNOW WHY DOESN'T WORK
219
type = (type!=null) ? type : "text/plain";
220         String JavaDoc mode = e.getAttributeValue("mode");//getAtomNamespace())); DONT KNOW WHY DOESN'T WORK
221
if (mode == null) {
222             mode = Content.XML; // default to xml content
223
}
224         if (mode.equals(Content.ESCAPED)) {
225             // do nothing XML Parser took care of this
226
value = e.getText();
227         }
228         else
229         if (mode.equals(Content.BASE64)) {
230                 value = Base64.decode(e.getText());
231         }
232         else
233         if (mode.equals(Content.XML)) {
234             XMLOutputter outputter = new XMLOutputter();
235             List eContent = e.getContent();
236             Iterator i = eContent.iterator();
237             while (i.hasNext()) {
238                 org.jdom.Content c = (org.jdom.Content) i.next();
239                 if (c instanceof Element) {
240                     Element eC = (Element) c;
241                     if (eC.getNamespace().equals(getAtomNamespace())) {
242                         ((Element)c).setNamespace(Namespace.NO_NAMESPACE);
243                     }
244                 }
245             }
246             value = outputter.outputString(eContent);
247         }
248
249         Content content = new Content();
250         content.setType(type);
251         content.setMode(mode);
252         content.setValue(value);
253         return content;
254     }
255
256     // List(Elements) -> List(Entries)
257
private List parseEntries(List eEntries) {
258         List entries = new ArrayList();
259         for (int i=0;i<eEntries.size();i++) {
260             entries.add(parseEntry((Element)eEntries.get(i)));
261         }
262         return (entries.size()>0) ? entries : null;
263     }
264
265     private Entry parseEntry(Element eEntry) {
266         Entry entry = new Entry();
267
268         Element e = eEntry.getChild("title",getAtomNamespace());
269         if (e!=null) {
270             entry.setTitle(e.getText());
271         }
272
273         List eList = eEntry.getChildren("link",getAtomNamespace());
274         entry.setAlternateLinks(parseAlternateLinks(eList));
275         entry.setOtherLinks(parseOtherLinks(eList));
276
277         e = eEntry.getChild("author",getAtomNamespace());
278         if (e!=null) {
279             entry.setAuthor(parsePerson(e));
280         }
281
282         eList = eEntry.getChildren("contributor",getAtomNamespace());
283         if (eList.size()>0) {
284             entry.setContributors(parsePersons(eList));
285         }
286
287         e = eEntry.getChild("id",getAtomNamespace());
288         if (e!=null) {
289             entry.setId(e.getText());
290         }
291
292         e = eEntry.getChild("modified",getAtomNamespace());
293         if (e!=null) {
294             entry.setModified(DateParser.parseW3CDateTime(e.getText()));
295         }
296
297         e = eEntry.getChild("issued",getAtomNamespace());
298         if (e!=null) {
299             entry.setIssued(DateParser.parseW3CDateTime(e.getText()));
300         }
301
302         e = eEntry.getChild("summary",getAtomNamespace());
303         if (e!=null) {
304             entry.setSummary(parseContent(e));
305         }
306
307         eList = eEntry.getChildren("content",getAtomNamespace());
308         if (eList.size()>0) {
309             List content = new ArrayList();
310             for (int i=0;i<eList.size();i++) {
311                 content.add(parseContent((Element)eList.get(i)));
312             }
313             entry.setContents(content);
314         }
315
316         entry.setModules(parseItemModules(eEntry));
317
318         return entry;
319     }
320
321
322 }
323
Popular Tags