View Javadoc

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;
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 type,String 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.getPath() != null) {
66              eCloud.setAttribute(new Attribute("path", cloud.getPath()));
67          }
68  
69          if (cloud.getRegisterProcedure() != null) {
70              eCloud.setAttribute(new Attribute("registerProcedure", cloud.getRegisterProcedure()));
71          }
72  
73          if (cloud.getProtocol() != null) {
74              eCloud.setAttribute(new Attribute("protocol", cloud.getProtocol()));
75          }
76          return eCloud;
77      }
78  
79      // Another one to thanks DW for
80      protected int getNumberOfEnclosures(List enclosures) {
81          return (enclosures.size()>0) ? 1 : 0;
82      }
83  
84      protected void populateItem(Item item, Element eItem, int index) {
85          super.populateItem(item,eItem, index);
86  
87          Source source =item.getSource();
88          if (source != null) {
89              eItem.addContent(generateSourceElement(source));
90          }
91  
92          List enclosures = item.getEnclosures();
93          for(int i = 0; i < getNumberOfEnclosures(enclosures); i++) {
94              eItem.addContent(generateEnclosure((Enclosure)enclosures.get(i)));
95          }
96  
97          List categories = item.getCategories();
98          for(int i = 0; i < categories.size(); i++) {
99              eItem.addContent(generateCategoryElement((Category)categories.get(i)));
100         }
101     }
102 
103     protected Element generateSourceElement(Source source) {
104         Element sourceElement = new Element("source",getFeedNamespace());
105         if (source.getUrl() != null) {
106             sourceElement.setAttribute(new Attribute("url", source.getUrl()));
107         }
108         sourceElement.addContent(source.getValue());
109         return sourceElement;
110     }
111 
112     protected Element generateEnclosure(Enclosure enclosure) {
113         Element enclosureElement = new Element("enclosure",getFeedNamespace());
114         if (enclosure.getUrl() != null) {
115             enclosureElement.setAttribute("url", enclosure.getUrl());
116         }
117         if (enclosure.getLength() != 0) {
118             enclosureElement.setAttribute("length", String.valueOf(enclosure.getLength()));
119         }
120         if (enclosure.getType() != null) {
121             enclosureElement.setAttribute("type", enclosure.getType());
122         }
123         return enclosureElement;
124     }
125 
126     protected Element generateCategoryElement(Category category) {
127         Element categoryElement = new Element("category",getFeedNamespace());
128         if (category.getDomain() != null) {
129             categoryElement.setAttribute("domain", category.getDomain());
130         }
131         categoryElement.addContent(category.getValue());
132         return categoryElement;
133     }
134 
135 
136     protected void checkChannelConstraints(Element eChannel) throws FeedException {
137         checkNotNullAndLength(eChannel,"title", 0, -1);
138         checkNotNullAndLength(eChannel,"description", 0, -1);
139         checkNotNullAndLength(eChannel,"link", 0, -1);
140     }
141 
142     protected void checkImageConstraints(Element eImage) throws FeedException {
143         checkNotNullAndLength(eImage,"title", 0, -1);
144         checkNotNullAndLength(eImage,"url", 0, -1);
145     }
146 
147     protected void checkTextInputConstraints(Element eTextInput) throws FeedException {
148         checkNotNullAndLength(eTextInput,"title", 0, -1);
149         checkNotNullAndLength(eTextInput,"description", 0, -1);
150         checkNotNullAndLength(eTextInput,"name", 0, -1);
151         checkNotNullAndLength(eTextInput,"link", 0, -1);
152     }
153 
154     protected void checkItemsConstraints(Element parent) throws FeedException {
155     }
156 
157     protected void checkItemConstraints(Element eItem) throws FeedException {
158     }
159 
160 }