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