1 21 22 package org.armedbear.j.mail; 23 24 import java.net.MalformedURLException ; 25 import java.util.ArrayList ; 26 import java.util.List ; 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 folderName; 34 35 public ImapURL(String s) throws MalformedURLException 36 { 37 if (!s.startsWith("{")) 38 throw new MalformedURLException (); 39 int index = s.indexOf('}'); 40 if (index < 0) 41 throw new MalformedURLException (); 42 folderName = s.substring(index + 1); 43 s = s.substring(1, index); 44 port = DEFAULT_PORT; 45 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 (); 53 if (s.length() == 0) { 55 host = "127.0.0.1"; 57 return; 58 } 59 if (s.charAt(0) != '@') 60 throw new MalformedURLException (); 61 s = s.substring(1); index = s.indexOf(':'); 63 if (index >= 0) { 64 try { 65 port = Integer.parseInt(s.substring(index + 1)); 66 } 67 catch (Exception e) { 68 throw new MalformedURLException (); 69 } 70 s = s.substring(0, index); 71 } 72 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 e) { 81 throw new MalformedURLException (); 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 getFolderName() 97 { 98 return folderName; 99 } 100 101 public final List getFolderPathComponents() 102 { 103 ArrayList list = new ArrayList (); 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 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 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 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 |