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.Image;
024: import com.sun.syndication.feed.rss.Item;
025: import org.jdom.Attribute;
026: import org.jdom.Document;
027: import org.jdom.Element;
028: import org.jdom.Namespace;
029:
030: import java.util.*;
031:
032: /**
033: */
034: public class RSS091UserlandParser extends RSS090Parser {
035:
036: public RSS091UserlandParser() {
037: this ("rss_0.91U");
038: }
039:
040: protected RSS091UserlandParser(String type) {
041: super (type);
042: }
043:
044: public boolean isMyType(Document document) {
045: boolean ok;
046: Element rssRoot = document.getRootElement();
047: ok = rssRoot.getName().equals("rss");
048: if (ok) {
049: ok = false;
050: Attribute version = rssRoot.getAttribute("version");
051: if (version != null) {
052: ok = version.getValue().equals(getRSSVersion());
053: }
054: }
055: return ok;
056: }
057:
058: protected String getRSSVersion() {
059: return "0.91";
060: }
061:
062: protected Namespace getRSSNamespace() {
063: return Namespace.getNamespace("");
064: }
065:
066: /**
067: * To be overriden by RSS 0.91 Netscape and RSS 0.94
068: */
069: protected boolean isHourFormat24(Element rssRoot) {
070: return true;
071: }
072:
073: /**
074: * Parses the root element of an RSS document into a Channel bean.
075: * <p/>
076: * It first invokes super.parseChannel and then parses and injects the following
077: * properties if present: language, pubDate, rating and copyright.
078: * <p/>
079: *
080: * @param rssRoot the root element of the RSS document to parse.
081: * @return the parsed Channel bean.
082: */
083: protected WireFeed parseChannel(Element rssRoot) {
084: Channel channel = (Channel) super .parseChannel(rssRoot);
085:
086: Element eChannel = rssRoot.getChild("channel",
087: getRSSNamespace());
088:
089: Element e = eChannel.getChild("language", getRSSNamespace());
090: if (e != null) {
091: channel.setLanguage(e.getText());
092: }
093: e = eChannel.getChild("rating", getRSSNamespace());
094: if (e != null) {
095: channel.setRating(e.getText());
096: }
097: e = eChannel.getChild("copyright", getRSSNamespace());
098: if (e != null) {
099: channel.setCopyright(e.getText());
100: }
101: e = eChannel.getChild("pubDate", getRSSNamespace());
102: if (e != null) {
103: channel.setPubDate(DateParser.parseDate(e.getText()));
104: }
105: e = eChannel.getChild("lastBuildDate", getRSSNamespace());
106: if (e != null) {
107: channel.setLastBuildDate(DateParser.parseDate(e.getText()));
108: }
109: e = eChannel.getChild("docs", getRSSNamespace());
110: if (e != null) {
111: channel.setDocs(e.getText());
112: }
113: e = eChannel.getChild("docs", getRSSNamespace());
114: if (e != null) {
115: channel.setDocs(e.getText());
116: }
117: e = eChannel.getChild("managingEditor", getRSSNamespace());
118: if (e != null) {
119: channel.setManagingEditor(e.getText());
120: }
121: e = eChannel.getChild("webMaster", getRSSNamespace());
122: if (e != null) {
123: channel.setWebMaster(e.getText());
124: }
125: e = eChannel.getChild("skipHours");
126: if (e != null) {
127: List skipHours = new ArrayList();
128: List eHours = e.getChildren("hour", getRSSNamespace());
129: for (int i = 0; i < eHours.size(); i++) {
130: Element eHour = (Element) eHours.get(i);
131: skipHours.add(new Integer(eHour.getText().trim()));
132: }
133: channel.setSkipHours(skipHours);
134: }
135:
136: e = eChannel.getChild("skipDays");
137: if (e != null) {
138: List skipDays = new ArrayList();
139: List eDays = e.getChildren("day", getRSSNamespace());
140: for (int i = 0; i < eDays.size(); i++) {
141: Element eDay = (Element) eDays.get(i);
142: skipDays.add(eDay.getText().trim());
143: }
144: channel.setSkipDays(skipDays);
145: }
146: return channel;
147: }
148:
149: /**
150: * Parses the root element of an RSS document looking for image information.
151: * <p/>
152: * It first invokes super.parseImage and then parses and injects the following
153: * properties if present: url, link, width, height and description.
154: * <p/>
155: *
156: * @param rssRoot the root element of the RSS document to parse for image information.
157: * @return the parsed RSSImage bean.
158: */
159: protected Image parseImage(Element rssRoot) {
160: Image image = super .parseImage(rssRoot);
161: if (image != null) {
162: Element eImage = getImage(rssRoot);
163: Element e = eImage.getChild("width", getRSSNamespace());
164: if (e != null) {
165: image.setWidth(Integer.parseInt(e.getText().trim()));
166: }
167: e = eImage.getChild("height", getRSSNamespace());
168: if (e != null) {
169: image.setHeight(Integer.parseInt(e.getText().trim()));
170: }
171: e = eImage.getChild("description", getRSSNamespace());
172: if (e != null) {
173: image.setDescription(e.getText());
174: }
175: }
176: return image;
177: }
178:
179: /**
180: * It looks for the 'item' elements under the 'channel' elemment.
181: */
182: protected List getItems(Element rssRoot) {
183: Element eChannel = rssRoot.getChild("channel",
184: getRSSNamespace());
185: return (eChannel != null) ? eChannel.getChildren("item",
186: getRSSNamespace()) : Collections.EMPTY_LIST;
187: }
188:
189: /**
190: * It looks for the 'image' elements under the 'channel' elemment.
191: */
192: protected Element getImage(Element rssRoot) {
193: Element eChannel = rssRoot.getChild("channel",
194: getRSSNamespace());
195: return (eChannel != null) ? eChannel.getChild("image",
196: getRSSNamespace()) : null;
197: }
198:
199: /**
200: * To be overriden by RSS 0.91 Netscape parser
201: */
202: protected String getTextInputLabel() {
203: return "textInput";
204: }
205:
206: /**
207: * It looks for the 'textinput' elements under the 'channel' elemment.
208: */
209: protected Element getTextInput(Element rssRoot) {
210: String elementName = getTextInputLabel();
211: Element eChannel = rssRoot.getChild("channel",
212: getRSSNamespace());
213: return (eChannel != null) ? eChannel.getChild(elementName,
214: getRSSNamespace()) : null;
215: }
216:
217: /**
218: * Parses an item element of an RSS document looking for item information.
219: * <p/>
220: * It first invokes super.parseItem and then parses and injects the description property if present.
221: * <p/>
222: *
223: * @param rssRoot the root element of the RSS document in case it's needed for context.
224: * @param eItem the item element to parse.
225: * @return the parsed RSSItem bean.
226: */
227: protected Item parseItem(Element rssRoot, Element eItem) {
228: Item item = super .parseItem(rssRoot, eItem);
229: Element e = eItem.getChild("description", getRSSNamespace());
230: if (e != null) {
231: item.setDescription(parseItemDescription(rssRoot, e));
232: }
233: Element ce = eItem.getChild("encoded", getContentNamespace());
234: if (ce != null) {
235: Content content = new Content();
236: content.setType(Content.HTML);
237: content.setValue(ce.getText());
238: item.setContent(content);
239: }
240: return item;
241: }
242:
243: protected Description parseItemDescription(Element rssRoot,
244: Element eDesc) {
245: Description desc = new Description();
246: desc.setType("text/plain");
247: desc.setValue(eDesc.getText());
248: return desc;
249: }
250:
251: }
|