KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > nntp > NNTPRootFolder


1 /*
2  * NNTPRootFolder.java
3  * Copyright (C) 2002 dog <dog@gnu.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * You also have permission to link it with the Sun Microsystems, Inc.
11  * JavaMail(tm) extension and run that combination.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22
23 package gnu.mail.providers.nntp;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import javax.mail.Flags JavaDoc;
30 import javax.mail.Folder JavaDoc;
31 import javax.mail.IllegalWriteException JavaDoc;
32 import javax.mail.Message JavaDoc;
33 import javax.mail.MessagingException JavaDoc;
34
35 import gnu.inet.nntp.Group;
36 import gnu.inet.nntp.GroupIterator;
37 import gnu.inet.nntp.NNTPConstants;
38 import gnu.inet.nntp.StatusResponse;
39
40 /**
41  * The &quot;root&quot; folder of the NNTP newsgroup list.
42  * The NNTP folder namespace is taken to be a flat namespace.
43  * This object allows us to retrieve folders corresponding to each newsgroup
44  * in that space.
45  *
46  * @author <a HREF='mailto:dog@gnu.org'>Chris Burdess</a>
47  * @version 2.0
48  */

49 final class NNTPRootFolder
50 extends Folder JavaDoc
51 {
52
53   NNTPRootFolder(NNTPStore store)
54   {
55     super(store);
56   }
57
58   public String JavaDoc getName()
59   {
60     NNTPStore ns = (NNTPStore)store;
61     return ns.getURLName().getHost();
62   }
63
64   public String JavaDoc getFullName()
65   {
66     NNTPStore ns = (NNTPStore)store;
67     return ns.connection.getWelcome();
68   }
69
70   /**
71    * Returns the list of folders matching the specified pattern.
72    * @param pattern the JavaMail pattern
73    */

74   public Folder JavaDoc[] list(String JavaDoc pattern)
75     throws MessagingException JavaDoc
76     {
77       pattern = pattern.replace('%', '*'); // convert pattern to wildmat
78
try
79       {
80         NNTPStore ns = (NNTPStore)store;
81         // Indicates whether to *really* list all folders.
82
boolean listAll = ns.isListAll();
83
84         List JavaDoc acc = new LinkedList JavaDoc();
85         synchronized (ns.connection)
86         {
87           GroupIterator i = listAll ?
88             ns.connection.listActive(pattern) :
89             ns.connection.listSubscriptions();
90           while (i.hasNext())
91           {
92             Group group = i.nextGroup();
93             NNTPFolder folder = new NNTPFolder(ns, group.getName());
94             acc.add(folder);
95           }
96         }
97         int len = acc.size();
98         Folder JavaDoc[] folders = new Folder JavaDoc[len];
99         acc.toArray(folders);
100         return folders;
101       }
102       catch (StatusResponse e)
103       {
104         switch (e.getStatus())
105         {
106           case NNTPConstants.COMMAND_NOT_RECOGNIZED:
107           case NNTPConstants.SYNTAX_ERROR:
108           case NNTPConstants.INTERNAL_ERROR:
109             return listSubscribed(pattern);
110           default:
111             throw new MessagingException JavaDoc(e.getMessage(), e);
112         }
113       }
114       catch (IOException JavaDoc e)
115       {
116         throw new MessagingException JavaDoc(e.getMessage(), e);
117       }
118     }
119
120   /**
121    * Returns the list of subscribed folders matching the specified pattern.
122    * @param pattern the JavaMail pattern
123    */

124   public Folder JavaDoc[] listSubscribed(String JavaDoc pattern)
125     throws MessagingException JavaDoc
126     {
127       pattern = pattern.replace('%', '*'); // convert pattern to wildmat
128
// Does the pattern contain any wildcards?
129
boolean hasWildcard = pattern.indexOf('*')>-1;
130       // Does the pattern contain only a wildcard?
131
boolean onlyWildcard = hasWildcard && (pattern.length()==0);
132
133       NNTPStore ns = (NNTPStore)store;
134       List JavaDoc acc = new LinkedList JavaDoc();
135       Iterator JavaDoc i = ns.newsrc.list();
136       while (i.hasNext())
137       {
138         String JavaDoc name = (String JavaDoc)i.next();
139         // Check that name matches pattern
140
if (!onlyWildcard)
141         {
142           if (hasWildcard && matches(name, pattern))
143             acc.add(new NNTPFolder(ns, name));
144           else if (!hasWildcard && pattern.equals(name))
145             acc.add(new NNTPFolder(ns, name));
146         }
147       }
148       int len = acc.size();
149       Folder JavaDoc[] folders = new Folder JavaDoc[len];
150       acc.toArray(folders);
151       return folders;
152     }
153
154   /**
155    * Implements a subset of wildmat matching on the client side.
156    * This is necessary for newsgroup matching from newsrc lists.
157    */

158   boolean matches(String JavaDoc name, String JavaDoc pattern)
159   {
160     int i1 = pattern.indexOf('*');
161     int pn = 0, pp = 0;
162     while (i1>-1)
163     {
164       if (i1>0)
165       {
166         String JavaDoc ps = pattern.substring(pp, i1);
167         int len = ps.length();
168         String JavaDoc ns = name.substring(pn, len);
169         if (!ps.equals(ns))
170           return false;
171         pp = i1+1;
172         pn += len;
173         i1 = 0;
174       }
175       else
176       {
177         pp = i1+1;
178         i1 = pattern.indexOf('*', pp);
179         String JavaDoc ps = null;
180         if (i1==-1)
181           ps = pattern.substring(pp);
182         else
183           ps = pattern.substring(pp, i1);
184         int len = ps.length();
185         if (len>0)
186         {
187           String JavaDoc ns = name.substring(pn, len);
188           if (!ps.equals(ns))
189             return false;
190         }
191       }
192     }
193     return true;
194   }
195
196   /**
197    * Returns a new Folder object associated with the specified name.
198    */

199   public Folder JavaDoc getFolder(String JavaDoc name)
200     throws MessagingException JavaDoc
201     {
202       NNTPStore ns = (NNTPStore)store;
203       return new NNTPFolder(ns, name);
204     }
205
206   public Folder JavaDoc getParent()
207     throws MessagingException JavaDoc
208     {
209       return null;
210     }
211
212   public boolean exists()
213     throws MessagingException JavaDoc
214     {
215       return true;
216     }
217
218   /**
219    * As we're dealing with a flat namespace, the value of this is
220    * irrelevant.
221    */

222   public char getSeparator()
223     throws MessagingException JavaDoc
224     {
225       return '.';
226     }
227
228   /**
229    * This folder contains only folders.
230    */

231   public int getType()
232   {
233     return HOLDS_FOLDERS;
234   }
235
236   public void open(int mode)
237     throws MessagingException JavaDoc
238     {
239       // Although we will never actually _be_ open,
240
// it's always good to remind people....
241
if (mode!=READ_ONLY)
242         throw new IllegalWriteException JavaDoc("Folder is read-only");
243     }
244
245   public void close(boolean expunge)
246     throws MessagingException JavaDoc
247     {
248     }
249
250   public Message JavaDoc[] expunge()
251     throws MessagingException JavaDoc
252     {
253       throw new IllegalWriteException JavaDoc("Folder is read-only");
254     }
255
256   public boolean isOpen()
257   {
258     return false;
259   }
260
261   public Flags JavaDoc getPermanentFlags()
262   {
263     return new Flags JavaDoc();
264   }
265
266   public int getMessageCount()
267     throws MessagingException JavaDoc
268     {
269       return -1;
270     }
271
272   public Message JavaDoc getMessage(int msgnum)
273     throws MessagingException JavaDoc
274     {
275       throw new IllegalStateException JavaDoc("Folder not open");
276     }
277
278   /**
279    * This folder is always &quot;subscribed&quot;.
280    */

281   public void setSubscribed(boolean flag)
282     throws MessagingException JavaDoc
283     {
284       if (!flag)
285         throw new IllegalWriteException JavaDoc("Can't unsubscribe root folder");
286     }
287
288   public boolean hasNewMessages()
289     throws MessagingException JavaDoc
290     {
291       return false;
292     }
293
294   public void appendMessages(Message JavaDoc[] messages)
295     throws MessagingException JavaDoc
296     {
297       throw new IllegalWriteException JavaDoc("Folder is read-only");
298     }
299
300   public boolean create(int type)
301     throws MessagingException JavaDoc
302     {
303       throw new MessagingException JavaDoc("Folder already exists");
304     }
305
306   public boolean delete(boolean flag)
307     throws MessagingException JavaDoc
308     {
309       throw new IllegalWriteException JavaDoc("Folder is read-only");
310     }
311
312   public boolean renameTo(Folder JavaDoc folder)
313     throws MessagingException JavaDoc
314     {
315       throw new IllegalWriteException JavaDoc("Folder is read-only");
316     }
317
318 }
319
Popular Tags