1 18 package org.apache.tools.ant.taskdefs.email; 19 20 25 public class EmailAddress { 26 private String name; 27 private String address; 28 29 30 31 public EmailAddress() { 32 } 33 34 35 47 public EmailAddress(String email) { 50 final int minLen = 9; 51 int len = email.length(); 52 53 if (len > minLen) { 55 if ((email.charAt(0) == '<' || email.charAt(1) == '<') 56 && (email.charAt(len - 1) == '>' || email.charAt(len - 2) == '>')) { 57 this.address = trim(email, true); 58 return; 59 } 60 } 61 62 int paramDepth = 0; 63 int start = 0; 64 int end = 0; 65 int nStart = 0; 66 int nEnd = 0; 67 68 for (int i = 0; i < len; i++) { 69 char c = email.charAt(i); 70 if (c == '(') { 71 paramDepth++; 72 if (start == 0) { 73 end = i; nStart = i + 1; 75 } 76 } else if (c == ')') { 77 paramDepth--; 78 if (end == 0) { 79 start = i + 1; nEnd = i; 81 } 82 } else if (paramDepth == 0 && c == '<') { 83 if (start == 0) { 84 nEnd = i; 85 } 86 start = i + 1; 87 } else if (paramDepth == 0 && c == '>') { 88 end = i; 89 if (end != len - 1) { 90 nStart = i + 1; 91 } 92 } 93 } 94 95 if (end == 0) { 97 end = len; 98 } 99 if (nEnd == 0) { 101 nEnd = len; 102 } 103 105 this.address = trim(email.substring(start, end), true); 106 this.name = trim(email.substring(nStart, nEnd), false); 107 108 if (this.name.length() + this.address.length() > len) { 111 this.name = null; 112 } 113 } 114 115 119 private String trim(String t, boolean trimAngleBrackets) { 120 int start = 0; 121 int end = t.length(); 122 boolean trim = false; 123 do { 124 trim = false; 125 if (t.charAt(end - 1) == ')' 126 || (t.charAt(end - 1) == '>' && trimAngleBrackets) 127 || (t.charAt(end - 1) == '"' && t.charAt(end - 2) != '\\') 128 || t.charAt(end - 1) <= '\u0020') { 129 trim = true; 130 end--; 131 } 132 if (t.charAt(start) == '(' 133 || (t.charAt(start) == '<' && trimAngleBrackets) 134 || t.charAt(start) == '"' 135 || t.charAt(start) <= '\u0020') { 136 trim = true; 137 start++; 138 } 139 } while (trim); 140 return t.substring(start, end); 141 } 142 143 144 149 public void setName(String name) { 150 this.name = name; 151 } 152 153 154 159 public void setAddress(String address) { 160 this.address = address; 161 } 162 163 164 169 public String toString() { 170 if (name == null) { 171 return address; 172 } else { 173 return name + " <" + address + ">"; 174 } 175 } 176 177 178 183 public String getAddress() { 184 return address; 185 } 186 187 188 193 public String getName() { 194 return name; 195 } 196 } 197 198 | Popular Tags |