KickJava   Java API By Example, From Geeks To Geeks.

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


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.rss.*;
20 import com.sun.syndication.io.FeedException;
21 import org.jdom.Attribute;
22 import org.jdom.Element;
23
24 import java.util.List JavaDoc;
25
26
27 /**
28  * Feed Generator for RSS 0.92
29  * <p/>
30  *
31  * @author Elaine Chien
32  *
33  */

34
35 public class RSS092Generator extends RSS091UserlandGenerator {
36
37     public RSS092Generator() {
38         this("rss_0.92","0.92");
39     }
40
41     protected RSS092Generator(String JavaDoc type,String JavaDoc version) {
42         super(type,version);
43     }
44
45     protected void populateChannel(Channel channel,Element eChannel) {
46         super.populateChannel(channel,eChannel);
47
48         Cloud cloud = channel.getCloud();
49         if (cloud!=null) {
50             eChannel.addContent(generateCloud(cloud));
51         }
52     }
53
54     protected Element generateCloud(Cloud cloud) {
55         Element eCloud = new Element("cloud",getFeedNamespace());
56
57         if (cloud.getDomain() != null) {
58             eCloud.setAttribute(new Attribute("domain", cloud.getDomain()));
59         }
60
61         if (cloud.getPort() != 0) {
62             eCloud.setAttribute(new Attribute("port", String.valueOf(cloud.getPort())));
63         }
64
65         if (cloud.getRegisterProcedure() != null) {
66             eCloud.setAttribute(new Attribute("registerProcedure", cloud.getRegisterProcedure()));
67         }
68
69         if (cloud.getProtocol() != null) {
70             eCloud.setAttribute(new Attribute("protocol", cloud.getProtocol()));
71         }
72         return eCloud;
73     }
74
75     // Another one to thanks DW for
76
protected int getNumberOfEnclosures(List JavaDoc enclosures) {
77         return (enclosures.size()>0) ? 1 : 0;
78     }
79
80     protected void populateItem(Item item, Element eItem, int index) {
81         super.populateItem(item,eItem, index);
82
83         Source source =item.getSource();
84         if (source != null) {
85             eItem.addContent(generateSourceElement(source));
86         }
87
88         List JavaDoc enclosures = item.getEnclosures();
89         for(int i = 0; i < getNumberOfEnclosures(enclosures); i++) {
90             eItem.addContent(generateEnclosure((Enclosure)enclosures.get(i)));
91         }
92
93         List JavaDoc categories = item.getCategories();
94         for(int i = 0; i < categories.size(); i++) {
95             eItem.addContent(generateCategoryElement((Category)categories.get(i)));
96         }
97     }
98
99     protected Element generateSourceElement(Source source) {
100         Element sourceElement = new Element("source",getFeedNamespace());
101         if (source.getUrl() != null) {
102             sourceElement.setAttribute(new Attribute("url", source.getUrl()));
103         }
104         sourceElement.addContent(source.getValue());
105         return sourceElement;
106     }
107
108     protected Element generateEnclosure(Enclosure enclosure) {
109         Element enclosureElement = new Element("enclosure",getFeedNamespace());
110         if (enclosure.getUrl() != null) {
111             enclosureElement.setAttribute("url", enclosure.getUrl());
112         }
113         if (enclosure.getLength() != 0) {
114             enclosureElement.setAttribute("length", String.valueOf(enclosure.getLength()));
115         }
116         if (enclosure.getType() != null) {
117             enclosureElement.setAttribute("type", enclosure.getType());
118         }
119         return enclosureElement;
120     }
121
122     protected Element generateCategoryElement(Category category) {
123         Element categoryElement = new Element("category",getFeedNamespace());
124         if (category.getDomain() != null) {
125             categoryElement.setAttribute("domain", category.getDomain());
126         }
127         categoryElement.addContent(category.getValue());
128         return categoryElement;
129     }
130
131
132     protected void checkChannelConstraints(Element eChannel) throws FeedException {
133         checkNotNullAndLength(eChannel,"title", 0, -1);
134         checkNotNullAndLength(eChannel,"description", 0, -1);
135         checkNotNullAndLength(eChannel,"link", 0, -1);
136     }
137
138     protected void checkImageConstraints(Element eImage) throws FeedException {
139         checkNotNullAndLength(eImage,"title", 0, -1);
140         checkNotNullAndLength(eImage,"url", 0, -1);
141     }
142
143     protected void checkTextInputConstraints(Element eTextInput) throws FeedException {
144         checkNotNullAndLength(eTextInput,"title", 0, -1);
145         checkNotNullAndLength(eTextInput,"description", 0, -1);
146         checkNotNullAndLength(eTextInput,"name", 0, -1);
147         checkNotNullAndLength(eTextInput,"link", 0, -1);
148     }
149
150     protected void checkItemsConstraints(Element parent) throws FeedException {
151     }
152
153     protected void checkItemConstraints(Element eItem) throws FeedException {
154     }
155
156 }
157
Popular Tags