KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > rssticker > HtmlRssTicker


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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 package org.apache.myfaces.custom.rssticker;
17
18 import java.io.IOException JavaDoc;
19 import java.net.MalformedURLException JavaDoc;
20 import java.net.UnknownHostException JavaDoc;
21
22 import javax.faces.component.html.HtmlOutputText;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25
26 import org.apache.commons.digester.rss.Channel;
27 import org.apache.commons.digester.rss.Item;
28 import org.apache.commons.digester.rss.RSSDigester;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.xml.sax.SAXException JavaDoc;
32
33
34 /**
35  * @author mwessendorf (latest modification by $Author: matzew $)
36  * @version $Revision: 1.7 $ $Date: 2005/02/22 13:41:10 $
37  * $Log: HtmlRssTicker.java,v $
38  * Revision 1.7 2005/02/22 13:41:10 matzew
39  * added RENDER_TYPE constant
40  *
41  * Revision 1.6 2004/10/13 11:50:57 matze
42  * renamed packages to org.apache
43  *
44  * Revision 1.5 2004/09/15 07:27:01 mwessendorf
45  * RssTicker works now behind a firewall
46  *
47  * Revision 1.4 2004/07/01 21:53:10 mwessendorf
48  * ASF switch
49  *
50  * Revision 1.3 2004/06/27 22:06:26 mwessendorf
51  * Log
52  *
53  *
54  */

55 public class HtmlRssTicker extends HtmlOutputText{
56     
57     private static final Log log = LogFactory.getLog(HtmlRssTicker.class);
58     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.RssTicker";
59     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Output";
60     public static final String JavaDoc DEFAULT_RENDERER_TYPE = HtmlRssTickerRenderer.RENDERER_TYPE;
61
62     //private fields
63
private String JavaDoc _rssUrl = null;
64     private RSSDigester _digester = null;
65     private Channel _channel;
66
67
68     public HtmlRssTicker()
69     {
70         _digester = new RSSDigester();
71         setRendererType(DEFAULT_RENDERER_TYPE);
72     }
73
74     public String JavaDoc getFamily()
75     {
76         return COMPONENT_FAMILY;
77     }
78     
79     
80     public Object JavaDoc saveState(FacesContext context)
81     {
82         Object JavaDoc values[] = new Object JavaDoc[5];
83         values[0] = super.saveState(context);
84         values[1] = _rssUrl;
85         return ((Object JavaDoc) (values));
86     }
87  
88     public void restoreState(FacesContext context, Object JavaDoc state)
89     {
90         Object JavaDoc values[] = (Object JavaDoc[])state;
91         super.restoreState(context, values[0]);
92         _rssUrl = (String JavaDoc)values[1];
93     }
94
95     public String JavaDoc getRssUrl() {
96         if (_rssUrl != null) return _rssUrl;
97         ValueBinding vb = getValueBinding("rssUrl");
98         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
99
100     }
101
102     public void setRssUrl(String JavaDoc string) {
103         _rssUrl = string;
104         loadNews(_rssUrl);
105     }
106
107     /**
108      * @param _rssUrl
109      */

110     private void loadNews(String JavaDoc string) {
111         try {
112             
113             this._channel = (Channel)_digester.parse(string);
114           } catch(MalformedURLException JavaDoc mue){
115             _channel = null;
116             log.warn("NO CONNECTION TO THE INTERNET. CAN NOT READ RSS-FEED");
117           }catch (UnknownHostException JavaDoc uhe){
118                 _channel = null;
119                 log.warn("NO CONNECTION TO THE INTERNET. CAN NOT READ RSS-FEED");
120           } catch (IOException JavaDoc e1) {
121             e1.printStackTrace();
122           } catch (SAXException JavaDoc e) {
123             e.printStackTrace();
124           }
125     }
126
127     public Channel getChannel() {
128       return _channel;
129     }
130     public int itemCount(){
131       return _channel.getItems().length;
132     }
133     public Item[] items(){
134       return _channel.getItems();
135     }
136 }
137
Popular Tags