1 16 package org.apache.myfaces.custom.rssticker; 17 18 import java.io.IOException ; 19 import java.net.MalformedURLException ; 20 import java.net.UnknownHostException ; 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 ; 32 33 34 55 public class HtmlRssTicker extends HtmlOutputText{ 56 57 private static final Log log = LogFactory.getLog(HtmlRssTicker.class); 58 public static final String COMPONENT_TYPE = "org.apache.myfaces.RssTicker"; 59 public static final String COMPONENT_FAMILY = "javax.faces.Output"; 60 public static final String DEFAULT_RENDERER_TYPE = HtmlRssTickerRenderer.RENDERER_TYPE; 61 62 private String _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 getFamily() 75 { 76 return COMPONENT_FAMILY; 77 } 78 79 80 public Object saveState(FacesContext context) 81 { 82 Object values[] = new Object [5]; 83 values[0] = super.saveState(context); 84 values[1] = _rssUrl; 85 return ((Object ) (values)); 86 } 87 88 public void restoreState(FacesContext context, Object state) 89 { 90 Object values[] = (Object [])state; 91 super.restoreState(context, values[0]); 92 _rssUrl = (String )values[1]; 93 } 94 95 public String getRssUrl() { 96 if (_rssUrl != null) return _rssUrl; 97 ValueBinding vb = getValueBinding("rssUrl"); 98 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 99 100 } 101 102 public void setRssUrl(String string) { 103 _rssUrl = string; 104 loadNews(_rssUrl); 105 } 106 107 110 private void loadNews(String string) { 111 try { 112 113 this._channel = (Channel)_digester.parse(string); 114 } catch(MalformedURLException mue){ 115 _channel = null; 116 log.warn("NO CONNECTION TO THE INTERNET. CAN NOT READ RSS-FEED"); 117 }catch (UnknownHostException uhe){ 118 _channel = null; 119 log.warn("NO CONNECTION TO THE INTERNET. CAN NOT READ RSS-FEED"); 120 } catch (IOException e1) { 121 e1.printStackTrace(); 122 } catch (SAXException 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 |