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