KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > rss > RssHelper


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.util.rss;
24
25 import java.io.IOException JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33
34 import com.sun.syndication.feed.synd.SyndContent;
35 import com.sun.syndication.feed.synd.SyndContentImpl;
36 import com.sun.syndication.feed.synd.SyndEntry;
37 import com.sun.syndication.feed.synd.SyndEntryImpl;
38 import com.sun.syndication.feed.synd.SyndFeed;
39 import com.sun.syndication.feed.synd.SyndFeedImpl;
40 import com.sun.syndication.io.FeedException;
41 import com.sun.syndication.io.SyndFeedOutput;
42
43 /**
44  * @author mattias
45  *
46  * This is a helper class to interact with ROME - the sun sponsored RSS parser/generator.
47  */

48
49 public class RssHelper
50 {
51     private final static Logger logger = Logger.getLogger(RssHelper.class.getName());
52
53     //The default error message
54
private static final String JavaDoc COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
55
56     //Lets use the iso date format
57
private static final String JavaDoc DATE_FORMAT = "yyyy-MM-dd";
58
59     private String JavaDoc defaultFeedType = "atom_0.3";
60
61     /**
62      * The method that prints the xml and returns it as a string.
63      *
64      * @return
65      */

66     
67     public String JavaDoc render(SyndFeed feed)
68     {
69         String JavaDoc output = null;
70         
71         try
72         {
73             //res.setContentType(MIME_TYPE);
74
SyndFeedOutput out = new SyndFeedOutput();
75             output = out.outputString(feed);
76         }
77         catch (FeedException fe)
78         {
79             String JavaDoc msg = COULD_NOT_GENERATE_FEED_ERROR;
80             logger.error(msg, fe);
81             //res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,msg);
82
}
83         
84         return output;
85     }
86     /**
87      * This method returns a new SyndFeed instansiated with the parameters send in and with an empty entry-list.
88      *
89      * @param title
90      * @param link
91      * @param description
92      * @return
93      * @throws IOException
94      * @throws FeedException
95      */

96     
97     public SyndFeed getFeed(String JavaDoc feedType, String JavaDoc title, String JavaDoc link, String JavaDoc description, String JavaDoc encoding) throws IOException JavaDoc,FeedException
98     {
99         
100         SyndFeed feed = new SyndFeedImpl();
101
102         feedType = (feedType!=null) ? feedType : defaultFeedType;
103         feed.setFeedType(feedType);
104
105         feed.setTitle(title);
106         feed.setLink(link);
107         feed.setDescription(description);
108         feed.setEncoding(encoding);
109
110         List JavaDoc entries = new ArrayList JavaDoc();
111         
112         feed.setEntries(entries);
113
114         return feed;
115     }
116
117     /**
118      * This method adds an entry to a feed. No magic.
119      *
120      * @param feed
121      * @param title
122      * @param link
123      * @param publishedDate
124      * @param description
125      * @param descriptionContentType
126      * @throws IOException
127      * @throws FeedException
128      */

129     
130     public void addEntry(SyndFeed feed, String JavaDoc title, String JavaDoc link, Date JavaDoc publishedDate, String JavaDoc description, String JavaDoc descriptionContentType) throws IOException JavaDoc,FeedException
131     {
132         DateFormat JavaDoc dateParser = new SimpleDateFormat JavaDoc(DATE_FORMAT);
133
134         SyndEntry entry = new SyndEntryImpl();
135         entry.setTitle(title);
136         entry.setLink(link);
137         entry.setPublishedDate(publishedDate);
138         
139         SyndContent syndContent = new SyndContentImpl();
140         syndContent.setType(descriptionContentType);
141         syndContent.setValue(description);
142         
143         entry.setDescription(syndContent);
144         
145         feed.getEntries().add(entry);
146     }
147
148 }
149
Popular Tags