KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > io > impl > RSS10Parser


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 JavaDoc;
28
29 /**
30  */

31 public class RSS10Parser extends RSS090Parser {
32
33     private static final String JavaDoc RSS_URI = "http://purl.org/rss/1.0/";
34
35     public RSS10Parser() {
36         this("rss_1.0");
37     }
38
39     protected RSS10Parser(String JavaDoc 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 JavaDoc 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         return item;
101     }
102
103     protected WireFeed parseChannel(Element rssRoot) {
104         Channel channel = (Channel) super.parseChannel(rssRoot);
105
106         Element eChannel = rssRoot.getChild("channel", getRSSNamespace());
107         String JavaDoc uri = eChannel.getAttributeValue("about", getRDFNamespace());
108         if (uri != null) {
109             channel.setUri(uri);
110         }
111
112         return channel;
113     }
114
115     protected Description parseItemDescription(Element rssRoot,Element eDesc) {
116         Description desc = new Description();
117         desc.setType("text/plain");
118         desc.setValue(eDesc.getText());
119         return desc;
120     }
121
122 }
123
Popular Tags