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.Description;
20  import com.sun.syndication.feed.rss.Item;
21  import com.sun.syndication.feed.rss.Channel;
22  import com.sun.syndication.io.FeedException;
23  import org.jdom.Element;
24  import org.jdom.Namespace;
25  
26  import java.util.List;
27  
28  /***
29   * Feed Generator for RSS 1.0
30   * <p/>
31   *
32   * @author Elaine Chien
33   *
34   */
35  
36  public class RSS10Generator extends RSS090Generator {
37      
38      private static final String RSS_URI = "http://purl.org/rss/1.0/";
39      private static final Namespace RSS_NS = Namespace.getNamespace(RSS_URI);
40      
41      public RSS10Generator() {
42          super("rss_1.0");
43      }
44  
45      protected RSS10Generator(String feedType) {
46          super(feedType);
47      }
48  
49      protected Namespace getFeedNamespace() {
50          return RSS_NS;
51      }
52  
53      protected void populateChannel(Channel channel,Element eChannel) {
54          super.populateChannel(channel,eChannel);
55          if (channel.getUri() != null) {
56              eChannel.setAttribute("about", channel.getUri(), getRDFNamespace());
57          }
58          List items = channel.getItems();
59          if (items.size()>0) {
60              Element eItems = new Element("items",getFeedNamespace());
61              Element eSeq = new Element("Seq",getRDFNamespace());
62              for (int i=0;i<items.size();i++) {
63                  Item item = (Item) items.get(i);
64                  Element eLi = new Element("li",getRDFNamespace());
65                  String link = item.getLink();
66                  if (link!=null) {
67                      eLi.setAttribute("resource",link);
68                  }
69                  eSeq.addContent(eLi);
70              }
71              eItems.addContent(eSeq);
72              eChannel.addContent(eItems);
73          }
74      }
75      
76      protected void populateItem(Item item, Element eItem, int index) {
77          super.populateItem(item,eItem, index);
78          String link = item.getLink();
79          String uri = item.getUri();
80          
81          if (uri != null) {
82              eItem.setAttribute("about", uri, getRDFNamespace());
83          } else if (link != null) {
84              eItem.setAttribute("about", link, getRDFNamespace());
85          }
86          
87          Description description = item.getDescription();
88          if (description!=null) {
89              eItem.addContent(generateSimpleElement("description", description.getValue()));
90          }
91          if (item.getModule(getContentNamespace().getURI()) == null && item.getContent() != null) {
92              Element elem = new Element("encoded", getContentNamespace());
93              elem.addContent(item.getContent().getValue());
94              eItem.addContent(elem);
95          }
96      }
97  
98      protected void checkChannelConstraints(Element eChannel) throws FeedException {
99          checkNotNullAndLength(eChannel,"title", 0, -1);
100         checkNotNullAndLength(eChannel,"description", 0, -1);
101         checkNotNullAndLength(eChannel,"link", 0, -1);
102     }
103 
104     protected void checkImageConstraints(Element eImage) throws FeedException {
105         checkNotNullAndLength(eImage,"title", 0, -1);
106         checkNotNullAndLength(eImage,"url", 0, -1);
107         checkNotNullAndLength(eImage,"link", 0, -1);
108     }
109 
110     protected void checkTextInputConstraints(Element eTextInput) throws FeedException {
111         checkNotNullAndLength(eTextInput,"title", 0, -1);
112         checkNotNullAndLength(eTextInput,"description", 0, -1);
113         checkNotNullAndLength(eTextInput,"name", 0, -1);
114         checkNotNullAndLength(eTextInput,"link", 0, -1);
115     }
116 
117     protected void checkItemsConstraints(Element parent) throws FeedException {
118     }
119 
120     protected void checkItemConstraints(Element eItem) throws FeedException {
121         checkNotNullAndLength(eItem,"title", 0, -1);
122         checkNotNullAndLength(eItem,"link", 0, -1);
123     }
124 
125 }
126