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.Content;
22  import com.sun.syndication.feed.rss.Description;
23  import com.sun.syndication.feed.rss.Image;
24  import com.sun.syndication.feed.rss.Item;
25  import org.jdom.Attribute;
26  import org.jdom.Document;
27  import org.jdom.Element;
28  import org.jdom.Namespace;
29  
30  import java.util.*;
31  
32  /***
33   */
34  public class RSS091UserlandParser extends RSS090Parser {
35  
36      public RSS091UserlandParser() {
37          this("rss_0.91U");
38      }
39  
40      protected RSS091UserlandParser(String type) {
41          super(type);
42      }
43  
44      public boolean isMyType(Document document) {
45          boolean ok;
46          Element rssRoot = document.getRootElement();
47          ok = rssRoot.getName().equals("rss");
48          if (ok) {
49              ok = false;
50              Attribute version = rssRoot.getAttribute("version");
51              if (version!=null) {
52                  ok = version.getValue().equals(getRSSVersion());
53              }
54          }
55          return ok;
56      }
57  
58      protected String getRSSVersion() {
59              return "0.91";
60      }
61  
62      protected Namespace getRSSNamespace() {
63          return Namespace.getNamespace("");
64      }
65  
66      /***
67       * To be overriden by RSS 0.91 Netscape and RSS 0.94
68       */
69      protected boolean isHourFormat24(Element rssRoot) {
70          return true;
71      }
72  
73      /***
74       * Parses the root element of an RSS document into a Channel bean.
75       * <p/>
76       * It first invokes super.parseChannel and then parses and injects the following
77       * properties if present: language, pubDate, rating and copyright.
78       * <p/>
79       *
80       * @param rssRoot the root element of the RSS document to parse.
81       * @return the parsed Channel bean.
82       */
83      protected WireFeed parseChannel(Element rssRoot)  {
84          Channel channel = (Channel) super.parseChannel(rssRoot);
85  
86          Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
87  
88          Element e = eChannel.getChild("language",getRSSNamespace());
89          if (e!=null) {
90              channel.setLanguage(e.getText());
91          }
92          e = eChannel.getChild("rating",getRSSNamespace());
93          if (e!=null) {
94              channel.setRating(e.getText());
95          }
96          e = eChannel.getChild("copyright",getRSSNamespace());
97          if (e!=null) {
98              channel.setCopyright(e.getText());
99          }
100         e = eChannel.getChild("pubDate",getRSSNamespace());
101         if (e!=null) {
102             channel.setPubDate(DateParser.parseDate(e.getText()));
103         }
104         e = eChannel.getChild("lastBuildDate",getRSSNamespace());
105         if (e!=null) {
106             channel.setLastBuildDate(DateParser.parseDate(e.getText()));
107         }
108         e = eChannel.getChild("docs",getRSSNamespace());
109         if (e!=null) {
110             channel.setDocs(e.getText());
111         }
112         e = eChannel.getChild("docs",getRSSNamespace());
113         if (e!=null) {
114             channel.setDocs(e.getText());
115         }
116         e = eChannel.getChild("managingEditor",getRSSNamespace());
117         if (e!=null) {
118             channel.setManagingEditor(e.getText());
119         }
120         e = eChannel.getChild("webMaster",getRSSNamespace());
121         if (e!=null) {
122             channel.setWebMaster(e.getText());
123         }
124         e = eChannel.getChild("skipHours");
125         if (e!=null) {
126             List skipHours = new ArrayList();
127             List eHours = e.getChildren("hour",getRSSNamespace());
128             for (int i=0;i<eHours.size();i++) {
129                 Element eHour = (Element) eHours.get(i);
130                 skipHours.add(new Integer(eHour.getText().trim()));
131             }
132             channel.setSkipHours(skipHours);
133         }
134 
135         e = eChannel.getChild("skipDays");
136         if (e!=null) {
137             List skipDays = new ArrayList();
138             List eDays = e.getChildren("day",getRSSNamespace());
139             for (int i=0;i<eDays.size();i++) {
140                 Element eDay = (Element) eDays.get(i);
141                 skipDays.add(eDay.getText().trim());
142             }
143             channel.setSkipDays(skipDays);
144         }
145         return channel;
146     }
147 
148     /***
149      * Parses the root element of an RSS document looking for  image information.
150      * <p/>
151      * It first invokes super.parseImage and then parses and injects the following
152      * properties if present: url, link, width, height and description.
153      * <p/>
154      *
155      * @param rssRoot the root element of the RSS document to parse for image information.
156      * @return the parsed RSSImage bean.
157      */
158     protected Image parseImage(Element rssRoot) {
159         Image image = super.parseImage(rssRoot);
160         if (image!=null) {
161             Element eImage = getImage(rssRoot);
162             Element e = eImage.getChild("width",getRSSNamespace());
163             if (e!=null) {
164                 image.setWidth(Integer.parseInt(e.getText().trim()));
165             }
166             e = eImage.getChild("height",getRSSNamespace());
167             if (e!=null) {
168                 image.setHeight(Integer.parseInt(e.getText().trim()));
169             }
170             e = eImage.getChild("description",getRSSNamespace());
171             if (e!=null) {
172                 image.setDescription(e.getText());
173             }
174         }
175         return image;
176     }
177 
178 
179     /***
180      * It looks for the 'item' elements under the 'channel' elemment.
181      */
182     protected List getItems(Element rssRoot) {
183         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
184         return (eChannel!=null) ? eChannel.getChildren("item",getRSSNamespace()) : Collections.EMPTY_LIST;
185     }
186 
187     /***
188      * It looks for the 'image' elements under the 'channel' elemment.
189      */
190     protected Element getImage(Element rssRoot) {
191         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
192         return (eChannel!=null) ? eChannel.getChild("image",getRSSNamespace()) : null;
193     }
194 
195     /***
196      * To be overriden by RSS 0.91 Netscape parser
197      */
198     protected String getTextInputLabel() {
199         return "textInput";
200     }
201 
202     /***
203      * It looks for the 'textinput' elements under the 'channel' elemment.
204      */
205     protected Element getTextInput(Element rssRoot) {
206         String elementName = getTextInputLabel();
207         Element eChannel = rssRoot.getChild("channel",getRSSNamespace());
208         return (eChannel!=null) ? eChannel.getChild(elementName,getRSSNamespace()) : null;
209     }
210 
211     /***
212      * Parses an item element of an RSS document looking for item information.
213      * <p/>
214      * It first invokes super.parseItem and then parses and injects the description property if present.
215      * <p/>
216      *
217      * @param rssRoot the root element of the RSS document in case it's needed for context.
218      * @param eItem the item element to parse.
219      * @return the parsed RSSItem bean.
220      */
221     protected Item parseItem(Element rssRoot, Element eItem) {
222         Item item = super.parseItem(rssRoot,eItem);
223         Element e = eItem.getChild("description", getRSSNamespace());
224         if (e!=null) {
225             item.setDescription(parseItemDescription(rssRoot,e));
226         }
227         Element ce = eItem.getChild("encoded", getContentNamespace());
228         if (ce != null) {
229             Content content = new Content();
230             content.setType(Content.HTML);
231             content.setValue(ce.getText());
232             item.setContent(content);
233         }
234         return item;
235     }
236 
237     protected Description parseItemDescription(Element rssRoot,Element eDesc) {
238         Description desc = new Description();
239         desc.setType("text/plain");
240         desc.setValue(eDesc.getText());
241         return desc;
242     }
243 
244 }