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