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.Item;
23  import org.jdom.Document;
24  import org.jdom.Element;
25  import org.jdom.Namespace;
26  
27  import java.util.List;
28  
29  /***
30   */
31  public class RSS10Parser extends RSS090Parser {
32  
33      /***
34       * rss_1.0.feed.ModuleParser.classes=  [className] ...
35       *
36       */
37      public static final String FEED_MODULE_PARSERS_KEY = "rss_1.0.feed.ModuleParser.classes";
38  
39      /***
40       * rss_1.0.item.ModuleParser.classes= [className] ...
41       *
42       */
43      public static final String ITEM_MODULE_PARSERS_KEY = "rss_1.0.item.ModuleParser.classes";
44  
45      private static ModuleParsers FEED_MODULES_PARSER = new ModuleParsers(FEED_MODULE_PARSERS_KEY);
46      private static ModuleParsers ITEM_MODULES_PARSER = new ModuleParsers(ITEM_MODULE_PARSERS_KEY);
47  
48  
49      private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
50      private static final String RSS_URI = "http://purl.org/rss/1.0/";
51  
52      public RSS10Parser() {
53          this("rss_1.0");
54      }
55  
56      protected RSS10Parser(String type) {
57          super(type);
58      }
59  
60      /***
61       * Indicates if a JDom document is an RSS instance that can be parsed with the parser.
62       * <p/>
63       * It checks for RDF ("http://www.w3.org/1999/02/22-rdf-syntax-ns#") and
64       * RSS ("http://purl.org/rss/1.0/") namespaces being defined in the root element.
65       *
66       * @param document document to check if it can be parsed with this parser implementation.
67       * @return <b>true</b> if the document is RSS1., <b>false</b> otherwise.
68       */
69      public boolean isMyType(Document document) {
70          boolean ok = false;
71          Namespace rdfNS = Namespace.getNamespace(RDF_URI);
72          Namespace rssNS = Namespace.getNamespace(RSS_URI);
73  
74          Element rssRoot = document.getRootElement();
75          Namespace defaultNS = rssRoot.getNamespace();
76          List additionalNSs = rssRoot.getAdditionalNamespaces();
77  
78          ok = defaultNS!=null && defaultNS.equals(rdfNS);
79          if (ok) {
80              if (additionalNSs==null) {
81                  ok = false;
82              }
83              else {
84                  ok = false;
85                  for (int i=0;!ok && i<additionalNSs.size();i++) {
86                      ok = rssNS.equals(additionalNSs.get(i));
87                  }
88              }
89          }
90          return ok;
91      }
92  
93      /***
94       * Returns the namespace used by RSS elements in document of the RSS 1.0
95       * <P>
96       *
97       * @return returns "http://purl.org/rss/1.0/".
98       */
99      protected Namespace getRSSNamespace() {
100         return Namespace.getNamespace(RSS_URI);
101     }
102 
103     protected WireFeed parseChannel(Element rssRoot) {
104         Channel channel = (Channel) super.parseChannel(rssRoot);
105 
106         List modules = FEED_MODULES_PARSER.parseModules(rssRoot.getChild("channel",getRSSNamespace()));
107         if (modules!=null) {
108             channel.setModules(modules);
109         }
110 
111         return channel;
112     }
113 
114     /***
115      * Parses an item element of an RSS document looking for item information.
116      * <p/>
117      * It first invokes super.parseItem and then parses and injects the description property if present.
118      * <p/>
119      *
120      * @param rssRoot the root element of the RSS document in case it's needed for context.
121      * @param eItem the item element to parse.
122      * @return the parsed RSSItem bean.
123      */
124     protected Item parseItem(Element rssRoot,Element eItem) {
125         Item item = super.parseItem(rssRoot,eItem);
126         Element e = eItem.getChild("description",getRSSNamespace());
127         if (e!=null) {
128             item.setDescription(parseItemDescription(rssRoot,e));
129         }
130         List modules = ITEM_MODULES_PARSER.parseModules(eItem);
131         if (modules!=null) {
132             item.setModules(modules);
133         }
134         return item;
135     }
136 
137     protected Description parseItemDescription(Element rssRoot,Element eDesc) {
138         Description desc = new Description();
139         desc.setType("text/plain");
140         desc.setValue(eDesc.getText());
141         return desc;
142     }
143 
144 }