001: /*
002: * Copyright 2004 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.io.impl;
018:
019: import com.sun.syndication.feed.WireFeed;
020: import com.sun.syndication.feed.rss.Channel;
021: import com.sun.syndication.feed.rss.Content;
022: import com.sun.syndication.feed.rss.Description;
023: import com.sun.syndication.feed.rss.Item;
024: import org.jdom.Document;
025: import org.jdom.Element;
026: import org.jdom.Namespace;
027:
028: import java.util.List;
029:
030: /**
031: */
032: public class RSS10Parser extends RSS090Parser {
033:
034: private static final String RSS_URI = "http://purl.org/rss/1.0/";
035:
036: public RSS10Parser() {
037: this ("rss_1.0");
038: }
039:
040: protected RSS10Parser(String type) {
041: super (type);
042: }
043:
044: /**
045: * Indicates if a JDom document is an RSS instance that can be parsed with the parser.
046: * <p/>
047: * It checks for RDF ("http://www.w3.org/1999/02/22-rdf-syntax-ns#") and
048: * RSS ("http://purl.org/rss/1.0/") namespaces being defined in the root element.
049: *
050: * @param document document to check if it can be parsed with this parser implementation.
051: * @return <b>true</b> if the document is RSS1., <b>false</b> otherwise.
052: */
053: public boolean isMyType(Document document) {
054: boolean ok = false;
055:
056: Element rssRoot = document.getRootElement();
057: Namespace defaultNS = rssRoot.getNamespace();
058: List additionalNSs = rssRoot.getAdditionalNamespaces();
059:
060: ok = defaultNS != null && defaultNS.equals(getRDFNamespace());
061: if (ok) {
062: if (additionalNSs == null) {
063: ok = false;
064: } else {
065: ok = false;
066: for (int i = 0; !ok && i < additionalNSs.size(); i++) {
067: ok = getRSSNamespace().equals(additionalNSs.get(i));
068: }
069: }
070: }
071: return ok;
072: }
073:
074: /**
075: * Returns the namespace used by RSS elements in document of the RSS 1.0
076: * <P>
077: *
078: * @return returns "http://purl.org/rss/1.0/".
079: */
080: protected Namespace getRSSNamespace() {
081: return Namespace.getNamespace(RSS_URI);
082: }
083:
084: /**
085: * Parses an item element of an RSS document looking for item information.
086: * <p/>
087: * It first invokes super.parseItem and then parses and injects the description property if present.
088: * <p/>
089: *
090: * @param rssRoot the root element of the RSS document in case it's needed for context.
091: * @param eItem the item element to parse.
092: * @return the parsed RSSItem bean.
093: */
094: protected Item parseItem(Element rssRoot, Element eItem) {
095: Item item = super .parseItem(rssRoot, eItem);
096: Element e = eItem.getChild("description", getRSSNamespace());
097: if (e != null) {
098: item.setDescription(parseItemDescription(rssRoot, e));
099: }
100: Element ce = eItem.getChild("encoded", getContentNamespace());
101: if (ce != null) {
102: Content content = new Content();
103: content.setType(Content.HTML);
104: content.setValue(ce.getText());
105: item.setContent(content);
106: }
107:
108: String uri = eItem
109: .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",
121: getRSSNamespace());
122: String uri = eChannel.getAttributeValue("about",
123: getRDFNamespace());
124: if (uri != null) {
125: channel.setUri(uri);
126: }
127:
128: return channel;
129: }
130:
131: protected Description parseItemDescription(Element rssRoot,
132: Element eDesc) {
133: Description desc = new Description();
134: desc.setType("text/plain");
135: desc.setValue(eDesc.getText());
136: return desc;
137: }
138:
139: }
|