KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > ImapURL


1 /*
2  * ImapURL.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: ImapURL.java,v 1.2 2002/12/30 16:28:17 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import org.armedbear.j.FastStringBuffer;
28
29 public final class ImapURL extends MailboxURL
30 {
31     private static final int DEFAULT_PORT = 143;
32
33     private String JavaDoc folderName;
34
35     public ImapURL(String JavaDoc s) throws MalformedURLException JavaDoc
36     {
37         if (!s.startsWith("{"))
38             throw new MalformedURLException JavaDoc();
39         int index = s.indexOf('}');
40         if (index < 0)
41             throw new MalformedURLException JavaDoc();
42         folderName = s.substring(index + 1);
43         s = s.substring(1, index);
44         port = DEFAULT_PORT;
45         // The user name may be enclosed in quotes.
46
if (s.length() > 0 && s.charAt(0) == '"') {
47             index = s.indexOf('"', 1);
48             if (index >= 0) {
49                 user = s.substring(1, index);
50                 s = s.substring(index + 1);
51             } else
52                 throw new MalformedURLException JavaDoc();
53             // We've got the user name.
54
if (s.length() == 0) {
55                 // No host specified.
56
host = "127.0.0.1";
57                 return;
58             }
59             if (s.charAt(0) != '@')
60                 throw new MalformedURLException JavaDoc();
61             s = s.substring(1); // Skip '@'.
62
index = s.indexOf(':');
63             if (index >= 0) {
64                 try {
65                     port = Integer.parseInt(s.substring(index + 1));
66                 }
67                 catch (Exception JavaDoc e) {
68                     throw new MalformedURLException JavaDoc();
69                 }
70                 s = s.substring(0, index);
71             }
72             // What's left is the host name.
73
host = s;
74         } else {
75             index = s.indexOf(':');
76             if (index >= 0) {
77                 try {
78                     port = Integer.parseInt(s.substring(index + 1));
79                 }
80                 catch (Exception JavaDoc e) {
81                     throw new MalformedURLException JavaDoc();
82                 }
83                 s = s.substring(0, index);
84             }
85             index = s.indexOf('@');
86             if (index >= 0) {
87                 user = s.substring(0, index);
88                 host = s.substring(index + 1);
89             } else
90                 host = s;
91         }
92         if (folderName.equalsIgnoreCase("inbox"))
93             folderName = "inbox";
94     }
95
96     public final String JavaDoc getFolderName()
97     {
98         return folderName;
99     }
100
101     public final List JavaDoc getFolderPathComponents()
102     {
103         ArrayList JavaDoc list = new ArrayList JavaDoc();
104         int begin = 0;
105         while (true) {
106             int index = folderName.indexOf('/', begin);
107             if (index < 0) {
108                 list.add(folderName.substring(begin));
109                 break;
110             } else {
111                 list.add(folderName.substring(begin, index));
112                 begin = index + 1;
113                 if (begin >= folderName.length())
114                     break;
115             }
116         }
117         return list;
118     }
119
120     public boolean equals(Object JavaDoc object)
121     {
122         if (!(object instanceof ImapURL))
123             return false;
124         ImapURL url = (ImapURL) object;
125         if (host != url.host) {
126             if (host == null)
127                 return false;
128             if (!host.equals(url.host))
129                 return false;
130         }
131         if (folderName != url.folderName) {
132             if (folderName == null)
133                 return false;
134             if (!folderName.equals(url.folderName))
135                 return false;
136         }
137         if (user != url.user) {
138             if (user == null)
139                 return false;
140             if (!user.equals(url.user))
141                 return false;
142         }
143         if (port != url.port)
144             return false;
145         return true;
146     }
147
148     public String JavaDoc toString()
149     {
150         FastStringBuffer sb = new FastStringBuffer('{');
151         if (user != null) {
152             sb.append(user);
153             sb.append('@');
154         }
155         sb.append(host);
156         if (port != DEFAULT_PORT) {
157             sb.append(':');
158             sb.append(port);
159         }
160         sb.append('}');
161         sb.append(folderName);
162         return sb.toString();
163     }
164
165     public String JavaDoc getCanonicalName()
166     {
167         FastStringBuffer sb = new FastStringBuffer('{');
168         if (user != null)
169             sb.append(user);
170         else
171             sb.append(System.getProperty("user.name"));
172         sb.append('@');
173         sb.append(host);
174         sb.append(':');
175         sb.append(port);
176         sb.append('}');
177         sb.append(folderName);
178         return sb.toString();
179     }
180 }
181
Popular Tags