1 package net.sourceforge.phpeclipse.news;
3 import java.io.InputStream;
4 import java.io.PushbackInputStream;
6 import java.net.URLConnection;
7 import java.net.UnknownHostException;
8 import java.util.ArrayList;
9 import java.util.HashSet;
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
14 import org.eclipse.core.runtime.Preferences;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17 import org.w3c.dom.NodeList;
18 import org.xml.sax.InputSource;
21 * @author jnegre - http://www.jnegre.org/
23 * (c)Copyright 2002 Jérôme Nègre
26 public class Channel {
28 private final String url;
29 private final String title;
31 private boolean refreshing = false;
32 private String errorMessage = null;
33 private boolean unread = false;
35 private ArrayList items = new ArrayList();
36 private HashSet readUids = null;
39 * Constructor for Channel.
41 public Channel(String title, String url) {
42 this(title, url, null);
45 public Channel(String title, String url, HashSet readUids) {
48 this.readUids = readUids;
52 public void update() {
53 update(Plugin.getDefault().getPluginPreferences());
57 public void update(Preferences prefs) {
58 ArrayList newItems = new ArrayList();
59 String newErrorMessage = null;
62 URLConnection conn = new URL(url).openConnection();
63 conn.setRequestProperty("User-Agent", Plugin.userAgent);
64 if(prefs.getBoolean(Plugin.FORCE_CACHE_PREFERENCE)) {
65 conn.setRequestProperty("Pragma", "no-cache");
66 conn.setRequestProperty("Cache-Control", "no-cache");
68 InputStream stream = conn.getInputStream();
70 /* workaround a bug of crimson (it seems to ignore the encoding
71 * if it does not get it the first time it reads bytes from
72 * the stream. We use a PushbackInputStream to be sure that the
73 * encoding declaration is in the buffer)
75 PushbackInputStream pbStream = new PushbackInputStream(stream,64);
76 byte[] buffer = new byte[64];
79 pos += pbStream.read(buffer, pos, 64-pos);
81 pbStream.unread(buffer);
84 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
85 InputSource inputSource = new InputSource(pbStream);
86 Document doc = parser.parse(inputSource);
88 NodeList itemNodes = doc.getElementsByTagName("item");
89 for (int i = 0; i < itemNodes.getLength(); i++) {
90 Item aNewItem = new Item(this, (Element) itemNodes.item(i));
91 if(aNewItem.isBanned()) continue;
92 if(readUids!=null && readUids.remove(aNewItem.getUID())) {
93 aNewItem.setReadFlag(true);
95 int indexOld = items.indexOf(aNewItem);
97 newItems.add(items.get(indexOld));
99 newItems.add(aNewItem);
103 this.readUids = null;
104 } catch(UnknownHostException e) {
105 // no connection to internet
106 } catch(Exception e) {
107 newErrorMessage = e.toString();
108 Plugin.logInfo("Error in channel update",e);
112 this.errorMessage = newErrorMessage;
113 if(newErrorMessage == null) {
114 this.items = newItems;
124 public String getUrl() {
129 * Returns the errorMessage.
132 public synchronized String getErrorMessage() {
140 public synchronized ArrayList getItems() {
141 return new ArrayList(items);
145 * @see java.lang.Object#toString()
147 public String toString() {
148 return "Channel at "+url;
155 public String getTitle() {
160 * Returns the refreshing.
163 public boolean isRefreshing() {
168 * Sets the refreshing.
169 * @param refreshing The refreshing to set
171 public void setRefreshing(boolean refreshing) {
172 this.refreshing = refreshing;
176 * Returns the unread.
179 public boolean isUnread() {
183 public synchronized void computeUnRead() {
185 for (int i = 0; i < items.size(); i++) {
186 this.unread = this.unread || !((Item)items.get(i)).isReadFlag();
190 public String getUID() {