KickJava   Java API By Example, From Geeks To Geeks.

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


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.rss.Channel;
21 import com.sun.syndication.feed.rss.Description;
22 import com.sun.syndication.feed.rss.Image;
23 import com.sun.syndication.feed.rss.Item;
24 import org.jdom.Attribute;
25 import org.jdom.Document;
26 import org.jdom.Element;
27 import org.jdom.Namespace;
28
29 import java.util.*;
30
31 /**
32  */

33 public class RSS091UserlandParser extends RSS090Parser {
34
35     public RSS091UserlandParser() {
36         this("rss_0.91U");
37     }
38
39     protected RSS091UserlandParser(String JavaDoc type) {
40         super(type);
41     }
42
43     public boolean isMyType(Document document) {
44         boolean ok = false;
45         Element rssRoot = document.getRootElement();
46         ok = rssRoot.getName().equals("rss");
47         if (ok) {
48             ok = false;
49             Attribute version = rssRoot.getAttribute("version");
50             if (version!=null) {
51                 ok = version.getValue().equals(getRSSVersion());
52             }
53         }
54         return ok;
55     }
56
57     protected String JavaDoc getRSSVersion() {
58             return "0.91";
59     }
60
61     protected Namespace getRSSNamespace() {
62         return Namespace.getNamespace("");
63     }
64
65     /**
66      * To be overriden by RSS 0.91 Netscape and RSS 0.94
67      */

68     protected boolean isHourFormat24(Element rssRoot) {
69         return true;
70     }
71
72     /**
73      * Parses the root element of an RSS document into a Channel bean.
74      * <p/>
75      * It first invokes super.parseChannel and then parses and injects the following
76      * properties if present: language, pubDate, rating and copyright.
77      * <p/>
78      *
79      * @param rssRoot the root element of the RSS document to parse.
80      * @return the parsed Channel bean.
81      */

82     protected WireFeed parseChannel(Element rssRoot) {
83         Channel channel = (Channel) super.parseChannel(rssRoot);
84
85         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
86
87         Element e = eChannel.getChild("language",getRSSNamespace());
88         if (e!=null) {
89             channel.setLanguage(e.getText());
90         }
91         e = eChannel.getChild("rating",getRSSNamespace());
92         if (e!=null) {
93             channel.setRating(e.getText());
94         }
95         e = eChannel.getChild("copyright",getRSSNamespace());
96         if (e!=null) {
97             channel.setCopyright(e.getText());
98         }
99         e = eChannel.getChild("pubDate",getRSSNamespace());
100         if (e!=null) {
101             channel.setPubDate(DateParser.parseRFC822(e.getText()));
102         }
103         e = eChannel.getChild("lastBuildDate",getRSSNamespace());
104         if (e!=null) {
105             channel.setLastBuildDate(DateParser.parseRFC822(e.getText()));
106         }
107         e = eChannel.getChild("docs",getRSSNamespace());
108         if (e!=null) {
109             channel.setDocs(e.getText());
110         }
111         e = eChannel.getChild("docs",getRSSNamespace());
112         if (e!=null) {
113             channel.setDocs(e.getText());
114         }
115         e = eChannel.getChild("managingEditor",getRSSNamespace());
116         if (e!=null) {
117             channel.setManagingEditor(e.getText());
118         }
119         e = eChannel.getChild("webMaster",getRSSNamespace());
120         if (e!=null) {
121             channel.setWebMaster(e.getText());
122         }
123         e = eChannel.getChild("skipHours");
124         if (e!=null) {
125             List skipHours = new ArrayList();
126             List eHours = e.getChildren("hour",getRSSNamespace());
127             for (int i=0;i<eHours.size();i++) {
128                 Element eHour = (Element) eHours.get(i);
129                 skipHours.add(new Integer JavaDoc(eHour.getText()));
130             }
131             channel.setSkipHours(skipHours);
132         }
133
134         e = eChannel.getChild("skipDays");
135         if (e!=null) {
136             List skipDays = new ArrayList();
137             List eDays = e.getChildren("day",getRSSNamespace());
138             for (int i=0;i<eDays.size();i++) {
139                 Element eDay = (Element) eDays.get(i);
140                 skipDays.add(eDay.getText());
141             }
142             channel.setSkipDays(skipDays);
143         }
144         return channel;
145     }
146
147     /**
148      * Parses the root element of an RSS document looking for image information.
149      * <p/>
150      * It first invokes super.parseImage and then parses and injects the following
151      * properties if present: url, link, width, height and description.
152      * <p/>
153      *
154      * @param rssRoot the root element of the RSS document to parse for image information.
155      * @return the parsed RSSImage bean.
156      */

157     protected Image parseImage(Element rssRoot) {
158         Image image = super.parseImage(rssRoot);
159         if (image!=null) {
160             Element eImage = getImage(rssRoot);
161             Element e = eImage.getChild("width",getRSSNamespace());
162             if (e!=null) {
163                 image.setWidth(Integer.parseInt(e.getText()));
164             }
165             e = eImage.getChild("height",getRSSNamespace());
166             if (e!=null) {
167                 image.setHeight(Integer.parseInt(e.getText()));
168             }
169             e = eImage.getChild("description",getRSSNamespace());
170             if (e!=null) {
171                 image.setDescription(e.getText());
172             }
173         }
174         return image;
175     }
176
177
178     /**
179      * It looks for the 'item' elements under the 'channel' elemment.
180      */

181     protected List getItems(Element rssRoot) {
182         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
183         return (eChannel!=null) ? eChannel.getChildren("item",getRSSNamespace()) : Collections.EMPTY_LIST;
184     }
185
186     /**
187      * It looks for the 'image' elements under the 'channel' elemment.
188      */

189     protected Element getImage(Element rssRoot) {
190         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
191         return (eChannel!=null) ? eChannel.getChild("image",getRSSNamespace()) : null;
192     }
193
194     /**
195      * To be overriden by RSS 0.91 Netscape parser
196      */

197     protected String JavaDoc getTextInputLabel() {
198         return "textInput";
199     }
200
201     /**
202      * It looks for the 'textinput' elements under the 'channel' elemment.
203      */

204     protected Element getTextInput(Element rssRoot) {
205         String JavaDoc elementName = getTextInputLabel();
206         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
207         return (eChannel!=null) ? eChannel.getChild(elementName,getRSSNamespace()) : null;
208     }
209
210     /**
211      * Parses an item element of an RSS document looking for item information.
212      * <p/>
213      * It first invokes super.parseItem and then parses and injects the description property if present.
214      * <p/>
215      *
216      * @param rssRoot the root element of the RSS document in case it's needed for context.
217      * @param eItem the item element to parse.
218      * @return the parsed RSSItem bean.
219      */

220     protected Item parseItem(Element rssRoot,Element eItem) {
221         Item item = super.parseItem(rssRoot,eItem);
222         Element e = eItem.getChild("description",getRSSNamespace());
223         if (e!=null) {
224             item.setDescription(parseItemDescription(rssRoot,e));
225         }
226         return item;
227     }
228
229     protected Description parseItemDescription(Element rssRoot,Element eDesc) {
230         Description desc = new Description();
231         desc.setType("text/plain");
232         desc.setValue(eDesc.getText());
233         return desc;
234     }
235
236 }
237
Popular Tags