KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PopURL.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: PopURL.java,v 1.2 2002/12/27 03:53:18 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 org.armedbear.j.FastStringBuffer;
26
27 public final class PopURL extends MailboxURL
28 {
29     private static final int DEFAULT_PORT = 110;
30
31     // RFC 2384 pop://user@host:port
32
public PopURL(String JavaDoc s) throws MalformedURLException JavaDoc
33     {
34         if (!s.startsWith("pop://"))
35             throw new MalformedURLException JavaDoc();
36         s = s.substring(6);
37         port = DEFAULT_PORT;
38         // The user name may be enclosed in quotes.
39
if (s.length() > 0 && s.charAt(0) == '"') {
40             int index = s.indexOf('"', 1);
41             if (index >= 0) {
42                 user = s.substring(1, index);
43                 s = s.substring(index + 1);
44             } else
45                 throw new MalformedURLException JavaDoc();
46             // We've got the user name.
47
if (s.length() == 0) {
48                 // No host specified.
49
host = "127.0.0.1";
50                 return;
51             }
52             if (s.charAt(0) != '@')
53                 throw new MalformedURLException JavaDoc();
54             s = s.substring(1); // Skip '@'.
55
index = s.indexOf(':');
56             if (index >= 0) {
57                 try {
58                     port = Integer.parseInt(s.substring(index + 1));
59                 }
60                 catch (Exception JavaDoc e) {
61                     throw new MalformedURLException JavaDoc();
62                 }
63                 s = s.substring(0, index);
64             }
65             // What's left is the host name.
66
host = s;
67         } else {
68             int index = s.indexOf(':');
69             if (index >= 0) {
70                 try {
71                     port = Integer.parseInt(s.substring(index + 1));
72                 }
73                 catch (Exception JavaDoc e) {
74                     throw new MalformedURLException JavaDoc();
75                 }
76                 s = s.substring(0, index);
77             }
78             index = s.indexOf('@');
79             if (index >= 0) {
80                 user = s.substring(0, index);
81                 host = s.substring(index + 1);
82             } else {
83                 user = s;
84                 host = "127.0.0.1";
85             }
86         }
87     }
88
89     public boolean equals(Object JavaDoc object)
90     {
91         if (!(object instanceof PopURL))
92             return false;
93         PopURL url = (PopURL) object;
94         if (host != url.host) {
95             if (host == null)
96                 return false;
97             if (!host.equals(url.host))
98                 return false;
99         }
100         if (user != url.user) {
101             if (user == null)
102                 return false;
103             if (!user.equals(url.user))
104                 return false;
105         }
106         if (port != url.port)
107             return false;
108         return true;
109     }
110
111     public String JavaDoc toString()
112     {
113         FastStringBuffer sb = new FastStringBuffer("pop://");
114         if (user != null) {
115             if (user.indexOf('@') >= 0) {
116                 sb.append('"');
117                 sb.append(user);
118                 sb.append('"');
119             } else
120                 sb.append(user);
121             sb.append('@');
122         }
123         sb.append(host);
124         if (port != DEFAULT_PORT) {
125             sb.append(':');
126             sb.append(port);
127         }
128         return sb.toString();
129     }
130
131     public String JavaDoc getCanonicalName()
132     {
133         FastStringBuffer sb = new FastStringBuffer("pop://");
134         String JavaDoc s = user != null ? user : System.getProperty("user.name");
135         if (s.indexOf('@') >= 0) {
136             sb.append('"');
137             sb.append(s);
138             sb.append('"');
139         } else
140             sb.append(s);
141         sb.append('@');
142         sb.append(host);
143         sb.append(':');
144         sb.append(port);
145         return sb.toString();
146     }
147 }
148
Popular Tags