1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17 package org.bejug.javacareers.feeder.parser;
18
19 import java.io.Writer;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.xml.bind.JAXBContext;
24 import javax.xml.bind.JAXBException;
25 import javax.xml.bind.Marshaller;
26 import javax.xml.bind.PropertyException;
27 import javax.xml.bind.Validator;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.bejug.javacareers.feeder.parser.jaxb.AnyType;
32 import org.bejug.javacareers.feeder.parser.jaxb.Channel;
33 import org.bejug.javacareers.feeder.parser.jaxb.Item;
34 import org.bejug.javacareers.feeder.parser.jaxb.ObjectFactory;
35 import org.bejug.javacareers.feeder.parser.jaxb.RssType;
36 import org.gnu.stealthp.rsslib.RSSChannel;
37 import org.gnu.stealthp.rsslib.RSSItem;
38
39 /***
40 * Generates a validated RSS 2.0 file
41 *
42 * @author Bavo Bruylandt (Last modified by $Author: shally $)
43 * @version $Revision: 1.6 $ - $Date: 2005/12/20 15:36:45 $
44 */
45 public class RssFeedGenerator {
46
47 /***
48 * The class related logger.
49 */
50 private static final Log LOG = LogFactory.getLog(RssFeedGenerator.class);
51
52 /***
53 * The RSS version.
54 */
55 public static final String RSS_VERSION = "2.0";
56
57 /***
58 *
59 */
60 private List items;
61
62 /***
63 *
64 */
65 private RSSChannel channel;
66
67 /***
68 *
69 */
70 private ObjectFactory objFactory;
71
72 /***
73 *
74 */
75 private JAXBContext jaxbContext;
76
77 /***
78 * Constructs an RSS generator
79 */
80 public RssFeedGenerator() {
81 try {
82 jaxbContext = JAXBContext.newInstance("org.bejug.javacareers.feeder.parser.jaxb");
83 } catch (JAXBException e) {
84 LOG.error(e);
85 }
86 }
87
88 /***
89 * Adds an RSS item for the channel element
90 *
91 * @param item RSS item with title, description and link
92 */
93 public void addItem(RSSItem item) {
94 if (items == null) {
95 items = new ArrayList();
96 }
97 items.add(item);
98 }
99
100 /***
101 * Sets the RSS channel information
102 *
103 * @param channel RSS channel information
104 */
105 public void setChannel(RSSChannel channel) {
106 this.channel = channel;
107 }
108
109 /***
110 * Generates the RSS content for current channel and elements
111 *
112 * @param out Writer object to output to
113 */
114 public void parseToRSS(Writer out) {
115 try {
116 Marshaller marshaller = jaxbContext.createMarshaller();
117 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
118 Boolean.TRUE);
119 marshaller.marshal(createRSSObject(), out);
120 LOG.info("Debug: See output in " + out);
121 } catch (PropertyException e) {
122 LOG.error(e);
123 } catch (JAXBException e) {
124 LOG.error(e);
125 }
126 }
127
128 /***
129 * Constructs the internal structure for the marshaller
130 *
131 * @return internal object structure that the marschaller understands
132 */
133 private RssType createRSSObject() {
134 RssType rss = null;
135 try {
136 objFactory = new ObjectFactory();
137
138 Validator validator = jaxbContext.createValidator();
139
140 rss = objFactory.createRss();
141 rss.setVersion(RSS_VERSION);
142
143 Channel rsschannel = objFactory.createChannel();
144
145 String imageUrl = channel.getRSSImage().getUrl();
146 String imageTitle = channel.getRSSImage().getTitle();
147 String imageLink = channel.getRSSImage().getLink();
148
149 LOG.info("Debug: Creating ImageType");
150 Channel.ImageType image = objFactory.createChannelImageType();
151 LOG.info("Setting ImageType properties");
152
153 image.setLink(getAnyType(imageLink));
154 LOG.info("Link set: "+image.getLink().getContent().size());
155 image.setUrl(getAnyType(imageUrl));
156 LOG.info("URL set: "+image.getUrl().getContent().size());
157 image.setTitle(getAnyType(imageTitle));
158 LOG.info("Setting ImageType");
159
160
161
162 rsschannel.setImage(image);
163 image.setDescription(getAnyType("Logo"));
164
165 LOG.info("Image set:"+rsschannel.getImage().getDescription());
166
167 String desc = channel.getDescription();
168 String link = channel.getLink();
169 String title = channel.getTitle();
170 String copyright = channel.getCopyright();
171 String editor = channel.getWebMaster();
172
173 rsschannel.setDescription(getAnyType(desc));
174 rsschannel.setLink(getAnyType(link));
175 rsschannel.setTitle(getAnyType(title));
176
177 rsschannel.setCopyright(getAnyType(copyright));
178 rsschannel.setWebMaster(getAnyType(editor));
179 AnyType channelType = objFactory.createAnyType();
180 channelType.getContent().add(rsschannel);
181
182 rss.setChannel(channelType);
183 LOG.info("Adding items");
184 if (items != null) {
185 for (int i = 0; i < items.size(); i++) {
186 RSSItem rssItem = (RSSItem) items.get(i);
187 String itemtitle = rssItem.getTitle();
188 String itemlink = rssItem.getLink();
189 String itemDescription = rssItem.getDescription();
190 Item item = objFactory.createItem();
191 item.setTitle(getAnyType(itemtitle));
192 item.setLink(getAnyType(itemlink));
193 item.setDescription(getAnyType(itemDescription));
194 rsschannel.getItem().add(item);
195 }
196 }
197 LOG.info("Starting validation...");
198 validator.validate(rss);
199 } catch (PropertyException e) {
200 LOG.error(e);
201 } catch (JAXBException e) {
202 LOG.error(e);
203 }
204
205 return rss;
206 }
207
208 /***
209 * Convenience method to create an AnyType containing a string value
210 *
211 * @param s The string value
212 * @return AnyType containing a string value
213 * @throws JAXBException if an error
214 */
215 private AnyType getAnyType(String s) throws JAXBException {
216 AnyType type = objFactory.createAnyType();
217 type.getContent().add(s);
218 return type;
219 }
220
221 /***
222 * a string representation of the object.
223 *
224 * @return a string representation of the object.
225 */
226 public String toString() {
227 final StringBuffer buf = new StringBuffer();
228 buf.append("RssFeedGenerator");
229 buf.append("{items=").append(items);
230 buf.append(",channel=").append(channel);
231 buf.append(",objFactory=").append(objFactory);
232 buf.append(",jaxbContext=").append(jaxbContext);
233 buf.append('}');
234 return buf.toString();
235 }
236 }
237
238 /***
239 * $Log: RssFeedGenerator.java,v $
240 * Revision 1.6 2005/12/20 15:36:45 shally
241 * CheckStyle and PMD changes.
242 *
243 * Revision 1.5 2005/12/09 10:46:55 shally
244 * Opkuis voor checkstyle en PMD
245 *
246 * Revision 1.4 2005/09/30 14:38:08 bavo_jcs
247 * Fixed URL
248 *
249 * Revision 1.3 2005/09/13 08:11:17 schauwvliege
250 * organize imports
251 *
252 * Revision 1.2 2005/09/01 09:38:55 bavo_jcs
253 * Added logo
254 *
255 * Revision 1.1 2005/08/26 07:58:29 ge0ffrey
256 * split up the sources in service, serviceimpl and webclient
257 *
258 * Revision 1.6 2005/08/10 09:04:48 bavo_jcs
259 * Optimized imports according to checkstyle
260 *
261 * Revision 1.5 2005/08/09 12:59:54 bavo_jcs
262 * Optimized imports
263 *
264 * Revision 1.4 2005/06/17 11:42:46 schauwvliege
265 * CheckStyle/ PMD
266 *
267 * Revision 1.3 2005/06/14 12:05:52 schauwvliege
268 * CheckStyle and fixing tests
269 *
270 * Revision 1.2 2005/06/09 08:18:43 bejug_cc
271 * Fix initial import
272 *
273 * Revision 1.3 2005/06/03 12:32:50 bbr
274 * fixed
275 *
276 * Revision 1.2 2005/05/24 11:52:39 bbr
277 * Using spring sheduling
278 *
279 * Revision 1.1 2005/05/23 17:04:57 sja
280 * Moved to org.bejug.javacareers.feeder package.
281 *
282 * Revision 1.1 2005/05/23 08:46:33 PSONG09
283 * added feeder source files to project
284 *
285 * Revision 1.4 2005/05/23 07:10:54 stephan_janssen
286 * Code cleanup.
287 *
288 * Revision 1.3 2005/05/19 15:46:03 stephan_janssen
289 * Added logger to exceptions.
290 *
291 * Revision 1.2 2005/05/11 13:16:02 bavo_jcs
292 * - debugged thread
293 *
294 * Revision 1.1 2005/05/11 11:53:25 bavo_jcs
295 * refactored
296 * - conform to conventions
297 * - some javadoc
298 * - Added FeederTask design
299 *
300 * Revision 1.1 2005/05/11 08:08:05 bavo_jcs
301 * adapted to changes
302 * added some javadoc abd RssFeedGenerator
303 *
304 * Revision 1.1 2005/05/09 15:48:44 bavo_jcs
305 * added RSS generator
306 * cleanup/javadoc TODO
307 *
308 */