1 package net.sourceforge.phpeclipse.news;
3 import java.io.InputStream;
4 import java.io.PushbackInputStream;
6 import java.net.URLConnection;
7 import java.util.ArrayList;
8 import java.util.HashSet;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
13 import org.eclipse.core.runtime.Preferences;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16 import org.w3c.dom.NodeList;
17 import org.xml.sax.InputSource;
20 * @author jnegre - http://www.jnegre.org/
22 * (c)Copyright 2002 J�r�me N�gre
25 public class Channel {
27 private final String url;
28 private final String title;
30 private boolean refreshing = false;
31 private String errorMessage = null;
32 private boolean unread = false;
34 private ArrayList items = new ArrayList();
35 private HashSet readUids = null;
38 * Constructor for Channel.
40 public Channel(String title, String url) {
41 this(title, url, null);
44 public Channel(String title, String url, HashSet readUids) {
47 this.readUids = readUids;
51 public void update() {
52 update(Plugin.getDefault().getPluginPreferences());
56 public void update(Preferences prefs) {
57 ArrayList newItems = new ArrayList();
58 String newErrorMessage = null;
61 URLConnection conn = new URL(url).openConnection();
62 conn.setRequestProperty("User-Agent", Plugin.userAgent);
63 if(prefs.getBoolean(Plugin.FORCE_CACHE_PREFERENCE)) {
64 conn.setRequestProperty("Pragma", "no-cache");
65 conn.setRequestProperty("Cache-Control", "no-cache");
67 InputStream stream = conn.getInputStream();
69 /* workaround a bug of crimson (it seems to ignore the encoding
70 * if it does not get it the first time it reads bytes from
71 * the stream. We use a PushbackInputStream to be sure that the
72 * encoding declaration is in the buffer)
74 PushbackInputStream pbStream = new PushbackInputStream(stream,64);
75 byte[] buffer = new byte[64];
78 pos += pbStream.read(buffer, pos, 64-pos);
80 pbStream.unread(buffer);
83 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
84 InputSource inputSource = new InputSource(pbStream);
85 Document doc = parser.parse(inputSource);
87 NodeList itemNodes = doc.getElementsByTagName("item");
88 for (int i = 0; i < itemNodes.getLength(); i++) {
89 Item aNewItem = new Item(this, (Element) itemNodes.item(i));
90 if(aNewItem.isBanned()) continue;
91 if(readUids!=null && readUids.remove(aNewItem.getUID())) {
92 aNewItem.setReadFlag(true);
94 int indexOld = items.indexOf(aNewItem);
96 newItems.add(items.get(indexOld));
98 newItems.add(aNewItem);
102 this.readUids = null;
103 } catch(Exception e) {
104 newErrorMessage = e.toString();
105 Plugin.logInfo("Error in channel update",e);
109 this.errorMessage = newErrorMessage;
110 if(newErrorMessage == null) {
111 this.items = newItems;
121 public String getUrl() {
126 * Returns the errorMessage.
129 public synchronized String getErrorMessage() {
137 public synchronized ArrayList getItems() {
138 return new ArrayList(items);
142 * @see java.lang.Object#toString()
144 public String toString() {
145 return "Channel at "+url;
152 public String getTitle() {
157 * Returns the refreshing.
160 public boolean isRefreshing() {
165 * Sets the refreshing.
166 * @param refreshing The refreshing to set
168 public void setRefreshing(boolean refreshing) {
169 this.refreshing = refreshing;
173 * Returns the unread.
176 public boolean isUnread() {
180 public synchronized void computeUnRead() {
182 for (int i = 0; i < items.size(); i++) {
183 this.unread = this.unread || !((Item)items.get(i)).isReadFlag();
187 public String getUID() {