001: /*
002: * ImapURL.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: ImapURL.java,v 1.2 2002/12/30 16:28:17 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.net.MalformedURLException;
025: import java.util.ArrayList;
026: import java.util.List;
027: import org.armedbear.j.FastStringBuffer;
028:
029: public final class ImapURL extends MailboxURL {
030: private static final int DEFAULT_PORT = 143;
031:
032: private String folderName;
033:
034: public ImapURL(String s) throws MalformedURLException {
035: if (!s.startsWith("{"))
036: throw new MalformedURLException();
037: int index = s.indexOf('}');
038: if (index < 0)
039: throw new MalformedURLException();
040: folderName = s.substring(index + 1);
041: s = s.substring(1, index);
042: port = DEFAULT_PORT;
043: // The user name may be enclosed in quotes.
044: if (s.length() > 0 && s.charAt(0) == '"') {
045: index = s.indexOf('"', 1);
046: if (index >= 0) {
047: user = s.substring(1, index);
048: s = s.substring(index + 1);
049: } else
050: throw new MalformedURLException();
051: // We've got the user name.
052: if (s.length() == 0) {
053: // No host specified.
054: host = "127.0.0.1";
055: return;
056: }
057: if (s.charAt(0) != '@')
058: throw new MalformedURLException();
059: s = s.substring(1); // Skip '@'.
060: index = s.indexOf(':');
061: if (index >= 0) {
062: try {
063: port = Integer.parseInt(s.substring(index + 1));
064: } catch (Exception e) {
065: throw new MalformedURLException();
066: }
067: s = s.substring(0, index);
068: }
069: // What's left is the host name.
070: host = s;
071: } else {
072: index = s.indexOf(':');
073: if (index >= 0) {
074: try {
075: port = Integer.parseInt(s.substring(index + 1));
076: } catch (Exception e) {
077: throw new MalformedURLException();
078: }
079: s = s.substring(0, index);
080: }
081: index = s.indexOf('@');
082: if (index >= 0) {
083: user = s.substring(0, index);
084: host = s.substring(index + 1);
085: } else
086: host = s;
087: }
088: if (folderName.equalsIgnoreCase("inbox"))
089: folderName = "inbox";
090: }
091:
092: public final String getFolderName() {
093: return folderName;
094: }
095:
096: public final List getFolderPathComponents() {
097: ArrayList list = new ArrayList();
098: int begin = 0;
099: while (true) {
100: int index = folderName.indexOf('/', begin);
101: if (index < 0) {
102: list.add(folderName.substring(begin));
103: break;
104: } else {
105: list.add(folderName.substring(begin, index));
106: begin = index + 1;
107: if (begin >= folderName.length())
108: break;
109: }
110: }
111: return list;
112: }
113:
114: public boolean equals(Object object) {
115: if (!(object instanceof ImapURL))
116: return false;
117: ImapURL url = (ImapURL) object;
118: if (host != url.host) {
119: if (host == null)
120: return false;
121: if (!host.equals(url.host))
122: return false;
123: }
124: if (folderName != url.folderName) {
125: if (folderName == null)
126: return false;
127: if (!folderName.equals(url.folderName))
128: return false;
129: }
130: if (user != url.user) {
131: if (user == null)
132: return false;
133: if (!user.equals(url.user))
134: return false;
135: }
136: if (port != url.port)
137: return false;
138: return true;
139: }
140:
141: public String toString() {
142: FastStringBuffer sb = new FastStringBuffer('{');
143: if (user != null) {
144: sb.append(user);
145: sb.append('@');
146: }
147: sb.append(host);
148: if (port != DEFAULT_PORT) {
149: sb.append(':');
150: sb.append(port);
151: }
152: sb.append('}');
153: sb.append(folderName);
154: return sb.toString();
155: }
156:
157: public String getCanonicalName() {
158: FastStringBuffer sb = new FastStringBuffer('{');
159: if (user != null)
160: sb.append(user);
161: else
162: sb.append(System.getProperty("user.name"));
163: sb.append('@');
164: sb.append(host);
165: sb.append(':');
166: sb.append(port);
167: sb.append('}');
168: sb.append(folderName);
169: return sb.toString();
170: }
171: }
|