KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > xml > RssFeedGenerator


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.xml;
21
22 import java.io.IOException JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import org.apache.log4j.Logger;
28 import org.openi.menu.Menu;
29
30 import de.nava.informa.core.ChannelBuilderIF;
31 import de.nava.informa.core.ChannelExporterIF;
32 import de.nava.informa.core.ChannelIF;
33 import de.nava.informa.core.ParseException;
34 import de.nava.informa.exporters.RSS_2_0_Exporter;
35 import de.nava.informa.impl.basic.ChannelBuilder;
36 import de.nava.informa.parsers.FeedParser;
37
38
39 /**
40  * Uses the informa rss feed parser library. Responsible for creating new rss channels,
41  * exporting to xml, retrieving rss feeds - both internal and external.
42  *
43  */

44 public class RssFeedGenerator {
45     private static Logger logger = Logger.getLogger(RssFeedGenerator.class);
46
47     /**
48      * This method is not so cool b/c removes boundary between informa api and openi
49      * but when I exposed only the xml via servlet - user not authenticated problem
50      * so had to expose direct call to the actual channel object
51      *
52      * Malformed url exceptions are trapped.
53      */

54     public static ChannelIF createStaticChannel() {
55         //
56
ChannelBuilderIF builder = new ChannelBuilder();
57         ChannelIF channel = builder.createChannel("OpenI");
58         channel.setDescription("OpenI News");
59         channel.setLanguage("en-us");
60
61         try {
62             channel.setSite(new URL JavaDoc("http://openi.org"));
63         } catch (MalformedURLException JavaDoc e) {
64             logger.error(e);
65         }
66
67         // take list and add it to channel
68
try {
69             builder.createItem(channel, "Subscriber base reaches 1M",
70                 "Subscriber base reaches 1M",
71                 new URL JavaDoc("http://demo.openi.org/openi"));
72             builder.createItem(channel, "Fresh data loaded as of Monday",
73                 "Fresh data loaded as of Monday",
74                 new URL JavaDoc("http://demo.openi.org/openi"));
75             builder.createItem(channel, "New OpenI Features",
76                 "New OpenI Features", new URL JavaDoc("http://demo.openi.org/openi"));
77         } catch (MalformedURLException JavaDoc e) {
78             logger.error(e);
79         }
80
81         return channel;
82     }
83
84     /**
85      * traps an IOException
86      * @param feedUrl
87      * @return null if parser throws an IOException, ParseException
88      */

89     public static ChannelIF createChannel(String JavaDoc feedUrl) {
90         ChannelIF channel = null;
91
92         try {
93             channel = FeedParser.parse(new ChannelBuilder(), feedUrl);
94         } catch (IOException JavaDoc e) {
95             logger.info("trouble parsing rss feed", e);
96         } catch (ParseException e) {
97             logger.info("trouble parsing rss feed", e);
98         }
99
100         return channel;
101     }
102     
103     /**
104      * Create an rss ChannelIF object, based on a users' OpenI menu.
105      * @param menu
106      * @return
107      */

108     public static ChannelIF createChannel(Menu menu){
109         ChannelIF channel = null;
110         
111         return channel;
112     }
113     
114     
115
116     /**
117      * Generates rss feed xml (v2.0) from the channel object, piping the results to the writer
118      * stream. Traps IOExceptions.
119      *
120      * Uses default character encoding UTF-8
121      */

122     public static void exportFeed(Writer JavaDoc writer, ChannelIF channel) {
123         exportFeed(writer, channel, "UTF-8");
124     }
125
126     public static void exportFeed(Writer JavaDoc writer, ChannelIF channel,
127         String JavaDoc encoding) {
128         // write channel to stream writer
129
ChannelExporterIF exp = new RSS_2_0_Exporter(writer, encoding);
130
131         try {
132             exp.write(channel);
133         } catch (IOException JavaDoc e) {
134             logger.error(e);
135         }
136     }
137 }
138
Popular Tags