KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > feed > synd > impl > ConverterForRSS090


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.feed.synd.impl;
18
19 import com.sun.syndication.feed.WireFeed;
20 import com.sun.syndication.feed.module.impl.ModuleUtils;
21 import com.sun.syndication.feed.rss.Channel;
22 import com.sun.syndication.feed.rss.Image;
23 import com.sun.syndication.feed.rss.Item;
24 import com.sun.syndication.feed.synd.SyndFeed;
25 import com.sun.syndication.feed.synd.Converter;
26 import com.sun.syndication.feed.synd.SyndEntry;
27 import com.sun.syndication.feed.synd.SyndImage;
28 import com.sun.syndication.feed.synd.SyndImageImpl;
29 import com.sun.syndication.feed.synd.SyndEntryImpl;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  */

36 public class ConverterForRSS090 implements Converter {
37     private String JavaDoc _type;
38
39     public ConverterForRSS090() {
40         this("rss_0.9");
41     }
42
43     protected ConverterForRSS090(String JavaDoc type) {
44         _type = type;
45     }
46
47     public String JavaDoc getType() {
48         return _type;
49     }
50
51     public void copyInto(WireFeed feed,SyndFeed syndFeed) {
52         syndFeed.setModules(ModuleUtils.cloneModules(feed.getModules()));
53
54         syndFeed.setEncoding(feed.getEncoding());
55         Channel channel = (Channel) feed;
56         syndFeed.setTitle(channel.getTitle());
57         syndFeed.setLink(channel.getLink());
58         syndFeed.setDescription(channel.getDescription());
59
60         Image image = channel.getImage();
61         if (image!=null) {
62             syndFeed.setImage(createSyndImage(image));
63         }
64
65         List JavaDoc items = channel.getItems();
66         if (items!=null) {
67             syndFeed.setEntries(createSyndEntries(items));
68         }
69     }
70
71     protected SyndImage createSyndImage(Image rssImage) {
72         SyndImage syndImage = new SyndImageImpl();
73         syndImage.setTitle(rssImage.getTitle());
74         syndImage.setUrl(rssImage.getUrl());
75         syndImage.setLink(rssImage.getLink());
76         return syndImage;
77     }
78
79     protected List JavaDoc createSyndEntries(List JavaDoc rssItems) {
80         List JavaDoc syndEntries = new ArrayList JavaDoc();
81         for (int i=0;i<rssItems.size();i++) {
82             syndEntries.add(createSyndEntry((Item) rssItems.get(i)));
83         }
84         return syndEntries;
85     }
86
87     protected SyndEntry createSyndEntry(Item item) {
88         SyndEntry syndEntry = new SyndEntryImpl();
89
90         syndEntry.setModules(ModuleUtils.cloneModules(item.getModules()));
91
92         syndEntry.setUri(item.getLink());
93         syndEntry.setTitle(item.getTitle());
94         syndEntry.setLink(item.getLink());
95         return syndEntry;
96     }
97
98     public WireFeed createRealFeed(SyndFeed syndFeed) {
99         return createRealFeed(getType(),syndFeed);
100     }
101
102     protected WireFeed createRealFeed(String JavaDoc type,SyndFeed syndFeed) {
103         Channel channel = new Channel(type);
104         channel.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));
105
106         channel.setEncoding(syndFeed.getEncoding());
107
108         channel.setTitle(syndFeed.getTitle());
109         channel.setLink(syndFeed.getLink());
110         channel.setDescription(syndFeed.getDescription());
111         SyndImage sImage = syndFeed.getImage();
112         if (sImage!=null) {
113             channel.setImage(createRSSImage(sImage));
114         }
115
116         List JavaDoc sEntries = syndFeed.getEntries();
117         if (sEntries!=null) {
118             channel.setItems(createRSSItems(sEntries));
119         }
120         return channel;
121     }
122
123     protected Image createRSSImage(SyndImage sImage) {
124         Image image = new Image();
125         image.setTitle(sImage.getTitle());
126         image.setUrl(sImage.getUrl());
127         image.setLink(sImage.getLink());
128         return image;
129     }
130
131     protected List JavaDoc createRSSItems(List JavaDoc sEntries) {
132         List JavaDoc list = new ArrayList JavaDoc();
133         for (int i=0;i<sEntries.size();i++) {
134             list.add(createRSSItem((SyndEntry)sEntries.get(i)));
135         }
136         return list;
137     }
138
139     protected Item createRSSItem(SyndEntry sEntry) {
140         Item item = new Item();
141         item.setModules(ModuleUtils.cloneModules(sEntry.getModules()));
142         item.setTitle(sEntry.getTitle());
143         item.setLink(sEntry.getLink());
144         return item;
145     }
146
147 }
148
Popular Tags