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.rss.Channel;
020: import com.sun.syndication.feed.rss.Description;
021: import com.sun.syndication.feed.rss.Image;
022: import com.sun.syndication.feed.rss.Item;
023: import com.sun.syndication.io.FeedException;
024: import org.jdom.Attribute;
025: import org.jdom.Document;
026: import org.jdom.Element;
027: import org.jdom.Namespace;
028:
029: import java.util.List;
030: import java.util.Date;
031:
032: /**
033: * Feed Generator for RSS 0.91
034: * <p/>
035: *
036: * @author Elaine Chien
037: *
038: */
039: public class RSS091UserlandGenerator extends RSS090Generator {
040: private String _version;
041:
042: public RSS091UserlandGenerator() {
043: this ("rss_0.91U", "0.91");
044: }
045:
046: protected RSS091UserlandGenerator(String type, String version) {
047: super (type);
048: _version = version;
049: }
050:
051: protected String getVersion() {
052: return _version;
053: }
054:
055: protected Namespace getFeedNamespace() {
056: return Namespace.NO_NAMESPACE;
057: }
058:
059: protected Document createDocument(Element root) {
060: return new Document(root);
061: }
062:
063: protected Element createRootElement(Channel channel) {
064: Element root = new Element("rss", getFeedNamespace());
065: Attribute version = new Attribute("version", getVersion());
066: root.setAttribute(version);
067: root.addNamespaceDeclaration(getContentNamespace());
068: generateModuleNamespaceDefs(root);
069: return root;
070: }
071:
072: protected void populateFeed(Channel channel, Element parent)
073: throws FeedException {
074: addChannel(channel, parent);
075: }
076:
077: protected void addChannel(Channel channel, Element parent)
078: throws FeedException {
079: super .addChannel(channel, parent);
080: Element eChannel = parent.getChild("channel",
081: getFeedNamespace());
082:
083: addImage(channel, eChannel);
084: addTextInput(channel, eChannel);
085: addItems(channel, eChannel);
086: }
087:
088: protected void populateChannel(Channel channel, Element eChannel) {
089: super .populateChannel(channel, eChannel);
090: String language = channel.getLanguage();
091: if (language != null) {
092: eChannel.addContent(generateSimpleElement("language",
093: language));
094: }
095:
096: String rating = channel.getRating();
097: if (rating != null) {
098: eChannel
099: .addContent(generateSimpleElement("rating", rating));
100: }
101:
102: String copyright = channel.getCopyright();
103: if (copyright != null) {
104: eChannel.addContent(generateSimpleElement("copyright",
105: copyright));
106: }
107:
108: Date pubDate = channel.getPubDate();
109: if (pubDate != null) {
110: eChannel.addContent(generateSimpleElement("pubDate",
111: DateParser.formatRFC822(pubDate)));
112: }
113:
114: Date lastBuildDate = channel.getLastBuildDate();
115: if (lastBuildDate != null) {
116: eChannel.addContent(generateSimpleElement("lastBuildDate",
117: DateParser.formatRFC822(lastBuildDate)));
118: }
119:
120: String docs = channel.getDocs();
121: if (docs != null) {
122: eChannel.addContent(generateSimpleElement("docs", docs));
123: }
124:
125: String managingEditor = channel.getManagingEditor();
126: if (managingEditor != null) {
127: eChannel.addContent(generateSimpleElement("managingEditor",
128: managingEditor));
129: }
130:
131: String webMaster = channel.getWebMaster();
132: if (webMaster != null) {
133: eChannel.addContent(generateSimpleElement("webMaster",
134: webMaster));
135: }
136:
137: List skipHours = channel.getSkipHours();
138: if (skipHours != null && skipHours.size() > 0) {
139: eChannel.addContent(generateSkipHoursElement(skipHours));
140: }
141:
142: List skipDays = channel.getSkipDays();
143: if (skipDays != null && skipDays.size() > 0) {
144: eChannel.addContent(generateSkipDaysElement(skipDays));
145: }
146: }
147:
148: protected Element generateSkipHoursElement(List hours) {
149: Element skipHoursElement = new Element("skipHours",
150: getFeedNamespace());
151: for (int i = 0; i < hours.size(); i++) {
152: skipHoursElement.addContent(generateSimpleElement("hour",
153: hours.get(i).toString()));
154: }
155: return skipHoursElement;
156: }
157:
158: protected Element generateSkipDaysElement(List days) {
159: Element skipDaysElement = new Element("skipDays");
160: for (int i = 0; i < days.size(); i++) {
161: skipDaysElement.addContent(generateSimpleElement("day",
162: days.get(i).toString()));
163: }
164: return skipDaysElement;
165: }
166:
167: protected void populateImage(Image image, Element eImage) {
168: super .populateImage(image, eImage);
169:
170: int width = image.getWidth();
171: if (width > -1) {
172: eImage.addContent(generateSimpleElement("width", String
173: .valueOf(width)));
174: }
175: int height = image.getHeight();
176: if (height > -1) {
177: eImage.addContent(generateSimpleElement("height", String
178: .valueOf(height)));
179: }
180:
181: String description = image.getDescription();
182: if (description != null) {
183: eImage.addContent(generateSimpleElement("description",
184: description));
185: }
186: }
187:
188: protected void populateItem(Item item, Element eItem, int index) {
189: super .populateItem(item, eItem, index);
190: Description description = item.getDescription();
191: if (description != null) {
192: eItem.addContent(generateSimpleElement("description",
193: description.getValue()));
194: }
195: if (item.getModule(getContentNamespace().getURI()) == null
196: && item.getContent() != null) {
197: Element elem = new Element("encoded", getContentNamespace());
198: elem.addContent(item.getContent().getValue());
199: eItem.addContent(elem);
200: }
201: }
202:
203: /**
204: * To be overriden by RSS 0.91 Netscape and RSS 0.94
205: */
206: protected boolean isHourFormat24() {
207: return true;
208: }
209:
210: protected void checkChannelConstraints(Element eChannel)
211: throws FeedException {
212: checkNotNullAndLength(eChannel, "title", 1, 100);
213: checkNotNullAndLength(eChannel, "description", 1, 500);
214: checkNotNullAndLength(eChannel, "link", 1, 500);
215: checkNotNullAndLength(eChannel, "language", 2, 5);
216:
217: checkLength(eChannel, "rating", 20, 500);
218: checkLength(eChannel, "copyright", 1, 100);
219: checkLength(eChannel, "pubDate", 1, 100);
220: checkLength(eChannel, "lastBuildDate", 1, 100);
221: checkLength(eChannel, "docs", 1, 500);
222: checkLength(eChannel, "managingEditor", 1, 100);
223: checkLength(eChannel, "webMaster", 1, 100);
224:
225: Element skipHours = eChannel.getChild("skipHours");
226: if (skipHours != null) {
227: List hours = skipHours.getChildren();
228: for (int i = 0; i < hours.size(); i++) {
229: Element hour = (Element) hours.get(i);
230: int value = Integer.parseInt(hour.getText().trim());
231: if (isHourFormat24()) {
232: if (value < 1 || value > 24) {
233: throw new FeedException("Invalid hour value "
234: + value
235: + ", it must be between 1 and 24");
236: }
237: } else {
238: if (value < 0 || value > 23) {
239: throw new FeedException("Invalid hour value "
240: + value
241: + ", it must be between 0 and 23");
242: }
243: }
244: }
245: }
246: }
247:
248: protected void checkImageConstraints(Element eImage)
249: throws FeedException {
250: checkNotNullAndLength(eImage, "title", 1, 100);
251: checkNotNullAndLength(eImage, "url", 1, 500);
252:
253: checkLength(eImage, "link", 1, 500);
254: checkLength(eImage, "width", 1, 3);
255: checkLength(eImage, "width", 1, 3);
256: checkLength(eImage, "description", 1, 100);
257: }
258:
259: protected void checkTextInputConstraints(Element eTextInput)
260: throws FeedException {
261: checkNotNullAndLength(eTextInput, "title", 1, 100);
262: checkNotNullAndLength(eTextInput, "description", 1, 500);
263: checkNotNullAndLength(eTextInput, "name", 1, 20);
264: checkNotNullAndLength(eTextInput, "link", 1, 500);
265: }
266:
267: protected void checkItemConstraints(Element eItem)
268: throws FeedException {
269: checkNotNullAndLength(eItem, "title", 1, 100);
270: checkNotNullAndLength(eItem, "link", 1, 500);
271:
272: checkLength(eItem, "description", 1, 500);
273: }
274:
275: }
|