1 21 22 package org.armedbear.j.mail; 23 24 import java.net.MalformedURLException ; 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 public PopURL(String s) throws MalformedURLException 33 { 34 if (!s.startsWith("pop://")) 35 throw new MalformedURLException (); 36 s = s.substring(6); 37 port = DEFAULT_PORT; 38 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 (); 46 if (s.length() == 0) { 48 host = "127.0.0.1"; 50 return; 51 } 52 if (s.charAt(0) != '@') 53 throw new MalformedURLException (); 54 s = s.substring(1); index = s.indexOf(':'); 56 if (index >= 0) { 57 try { 58 port = Integer.parseInt(s.substring(index + 1)); 59 } 60 catch (Exception e) { 61 throw new MalformedURLException (); 62 } 63 s = s.substring(0, index); 64 } 65 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 e) { 74 throw new MalformedURLException (); 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 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 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 getCanonicalName() 132 { 133 FastStringBuffer sb = new FastStringBuffer("pop://"); 134 String 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 |