View Javadoc

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 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 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(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(DAYS.get(eDay.getText()));
141             }
142             channel.setSkipDays(skipDays);
143         }
144         return channel;
145     }
146 
147     private static final Map DAYS = new HashMap();
148 
149     static {
150         DAYS.put(Channel.SUNDAY.toString(),Channel.SUNDAY);
151         DAYS.put(Channel.MONDAY.toString(),Channel.MONDAY);
152         DAYS.put(Channel.TUESDAY.toString(),Channel.TUESDAY);
153         DAYS.put(Channel.WEDNESDAY.toString(),Channel.WEDNESDAY);
154         DAYS.put(Channel.THURSDAY.toString(),Channel.THURSDAY);
155         DAYS.put(Channel.FRIDAY.toString(),Channel.FRIDAY);
156         DAYS.put(Channel.SATURDAY.toString(),Channel.SATURDAY);
157     }
158 
159     /***
160      * Parses the root element of an RSS document looking for  image information.
161      * <p/>
162      * It first invokes super.parseImage and then parses and injects the following
163      * properties if present: url, link, width, height and description.
164      * <p/>
165      *
166      * @param rssRoot the root element of the RSS document to parse for image information.
167      * @return the parsed RSSImage bean.
168      */
169     protected Image parseImage(Element rssRoot) {
170         Image image = super.parseImage(rssRoot);
171         if (image!=null) {
172             Element eImage = getImage(rssRoot);
173             Element e = eImage.getChild("width",getRSSNamespace());
174             if (e!=null) {
175                 image.setWidth(Integer.parseInt(e.getText()));
176             }
177             e = eImage.getChild("height",getRSSNamespace());
178             if (e!=null) {
179                 image.setHeight(Integer.parseInt(e.getText()));
180             }
181             e = eImage.getChild("description",getRSSNamespace());
182             if (e!=null) {
183                 image.setDescription(e.getText());
184             }
185         }
186         return image;
187     }
188 
189 
190     /***
191      * It looks for the 'item' elements under the 'channel' elemment.
192      */
193     protected List getItems(Element rssRoot) {
194         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
195         return (eChannel!=null) ? eChannel.getChildren("item",getRSSNamespace()) : Collections.EMPTY_LIST;
196     }
197 
198     /***
199      * It looks for the 'image' elements under the 'channel' elemment.
200      */
201     protected Element getImage(Element rssRoot) {
202         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
203         return (eChannel!=null) ? eChannel.getChild("image",getRSSNamespace()) : null;
204     }
205 
206     /***
207      * To be overriden by RSS 0.91 Netscape parser
208      */
209     protected String getTextInputLabel() {
210         return "textInput";
211     }
212 
213     /***
214      * It looks for the 'textinput' elements under the 'channel' elemment.
215      */
216     protected Element getTextInput(Element rssRoot) {
217         String elementName = getTextInputLabel();
218         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
219         return (eChannel!=null) ? eChannel.getChild(elementName,getRSSNamespace()) : null;
220     }
221 
222     /***
223      * Parses an item element of an RSS document looking for item information.
224      * <p/>
225      * It first invokes super.parseItem and then parses and injects the description property if present.
226      * <p/>
227      *
228      * @param rssRoot the root element of the RSS document in case it's needed for context.
229      * @param eItem the item element to parse.
230      * @return the parsed RSSItem bean.
231      */
232     protected Item parseItem(Element rssRoot,Element eItem) {
233         Item item = super.parseItem(rssRoot,eItem);
234         Element e = eItem.getChild("description",getRSSNamespace());
235         if (e!=null) {
236             item.setDescription(parseItemDescription(rssRoot,e));
237         }
238         return item;
239     }
240 
241     protected Description parseItemDescription(Element rssRoot,Element eDesc) {
242         Description desc = new Description();
243         desc.setType("text/plain");
244         desc.setValue(eDesc.getText());
245         return desc;
246     }
247 
248 }