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