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.*;
020: import com.sun.syndication.io.FeedException;
021: import org.jdom.Attribute;
022: import org.jdom.Element;
023:
024: import java.util.List;
025:
026: /**
027: * Feed Generator for RSS 0.92
028: * <p/>
029: *
030: * @author Elaine Chien
031: *
032: */
033:
034: public class RSS092Generator extends RSS091UserlandGenerator {
035:
036: public RSS092Generator() {
037: this ("rss_0.92", "0.92");
038: }
039:
040: protected RSS092Generator(String type, String version) {
041: super (type, version);
042: }
043:
044: protected void populateChannel(Channel channel, Element eChannel) {
045: super .populateChannel(channel, eChannel);
046:
047: Cloud cloud = channel.getCloud();
048: if (cloud != null) {
049: eChannel.addContent(generateCloud(cloud));
050: }
051: }
052:
053: protected Element generateCloud(Cloud cloud) {
054: Element eCloud = new Element("cloud", getFeedNamespace());
055:
056: if (cloud.getDomain() != null) {
057: eCloud.setAttribute(new Attribute("domain", cloud
058: .getDomain()));
059: }
060:
061: if (cloud.getPort() != 0) {
062: eCloud.setAttribute(new Attribute("port", String
063: .valueOf(cloud.getPort())));
064: }
065:
066: if (cloud.getPath() != null) {
067: eCloud.setAttribute(new Attribute("path", cloud.getPath()));
068: }
069:
070: if (cloud.getRegisterProcedure() != null) {
071: eCloud.setAttribute(new Attribute("registerProcedure",
072: cloud.getRegisterProcedure()));
073: }
074:
075: if (cloud.getProtocol() != null) {
076: eCloud.setAttribute(new Attribute("protocol", cloud
077: .getProtocol()));
078: }
079: return eCloud;
080: }
081:
082: // Another one to thanks DW for
083: protected int getNumberOfEnclosures(List enclosures) {
084: return (enclosures.size() > 0) ? 1 : 0;
085: }
086:
087: protected void populateItem(Item item, Element eItem, int index) {
088: super .populateItem(item, eItem, index);
089:
090: Source source = item.getSource();
091: if (source != null) {
092: eItem.addContent(generateSourceElement(source));
093: }
094:
095: List enclosures = item.getEnclosures();
096: for (int i = 0; i < getNumberOfEnclosures(enclosures); i++) {
097: eItem.addContent(generateEnclosure((Enclosure) enclosures
098: .get(i)));
099: }
100:
101: List categories = item.getCategories();
102: for (int i = 0; i < categories.size(); i++) {
103: eItem
104: .addContent(generateCategoryElement((Category) categories
105: .get(i)));
106: }
107: }
108:
109: protected Element generateSourceElement(Source source) {
110: Element sourceElement = new Element("source",
111: getFeedNamespace());
112: if (source.getUrl() != null) {
113: sourceElement.setAttribute(new Attribute("url", source
114: .getUrl()));
115: }
116: sourceElement.addContent(source.getValue());
117: return sourceElement;
118: }
119:
120: protected Element generateEnclosure(Enclosure enclosure) {
121: Element enclosureElement = new Element("enclosure",
122: getFeedNamespace());
123: if (enclosure.getUrl() != null) {
124: enclosureElement.setAttribute("url", enclosure.getUrl());
125: }
126: if (enclosure.getLength() != 0) {
127: enclosureElement.setAttribute("length", String
128: .valueOf(enclosure.getLength()));
129: }
130: if (enclosure.getType() != null) {
131: enclosureElement.setAttribute("type", enclosure.getType());
132: }
133: return enclosureElement;
134: }
135:
136: protected Element generateCategoryElement(Category category) {
137: Element categoryElement = new Element("category",
138: getFeedNamespace());
139: if (category.getDomain() != null) {
140: categoryElement
141: .setAttribute("domain", category.getDomain());
142: }
143: categoryElement.addContent(category.getValue());
144: return categoryElement;
145: }
146:
147: protected void checkChannelConstraints(Element eChannel)
148: throws FeedException {
149: checkNotNullAndLength(eChannel, "title", 0, -1);
150: checkNotNullAndLength(eChannel, "description", 0, -1);
151: checkNotNullAndLength(eChannel, "link", 0, -1);
152: }
153:
154: protected void checkImageConstraints(Element eImage)
155: throws FeedException {
156: checkNotNullAndLength(eImage, "title", 0, -1);
157: checkNotNullAndLength(eImage, "url", 0, -1);
158: }
159:
160: protected void checkTextInputConstraints(Element eTextInput)
161: throws FeedException {
162: checkNotNullAndLength(eTextInput, "title", 0, -1);
163: checkNotNullAndLength(eTextInput, "description", 0, -1);
164: checkNotNullAndLength(eTextInput, "name", 0, -1);
165: checkNotNullAndLength(eTextInput, "link", 0, -1);
166: }
167:
168: protected void checkItemsConstraints(Element parent)
169: throws FeedException {
170: }
171:
172: protected void checkItemConstraints(Element eItem)
173: throws FeedException {
174: }
175:
176: }
|