1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.email;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Holds an email address.
|
---|
21 | *
|
---|
22 | * @since Ant 1.5
|
---|
23 | */
|
---|
24 | public class EmailAddress {
|
---|
25 | private String name;
|
---|
26 | private String address;
|
---|
27 |
|
---|
28 |
|
---|
29 | /** Creates an empty email address */
|
---|
30 | public EmailAddress() {
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Creates a new email address based on the given string
|
---|
36 | *
|
---|
37 | * @param email the email address (with or without <>)
|
---|
38 | * Acceptable forms include:
|
---|
39 | * address
|
---|
40 | * <address>
|
---|
41 | * name <address>
|
---|
42 | * <address> name
|
---|
43 | * (name) address
|
---|
44 | * address (name)
|
---|
45 | */
|
---|
46 | // Make a limited attempt to extract a sanitized name and email address
|
---|
47 | // Algorithm based on the one found in Ant's MailMessage.java
|
---|
48 | public EmailAddress(String email) {
|
---|
49 | final int minLen = 9;
|
---|
50 | int len = email.length();
|
---|
51 |
|
---|
52 | // shortcut for "<address>"
|
---|
53 | if (len > minLen) {
|
---|
54 | if ((email.charAt(0) == '<' || email.charAt(1) == '<')
|
---|
55 | && (email.charAt(len - 1) == '>' || email.charAt(len - 2) == '>')) {
|
---|
56 | this.address = trim(email, true);
|
---|
57 | return;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | int paramDepth = 0;
|
---|
62 | int start = 0;
|
---|
63 | int end = 0;
|
---|
64 | int nStart = 0;
|
---|
65 | int nEnd = 0;
|
---|
66 |
|
---|
67 | for (int i = 0; i < len; i++) {
|
---|
68 | char c = email.charAt(i);
|
---|
69 | if (c == '(') {
|
---|
70 | paramDepth++;
|
---|
71 | if (start == 0) {
|
---|
72 | end = i; // support "address (name)"
|
---|
73 | nStart = i + 1;
|
---|
74 | }
|
---|
75 | } else if (c == ')') {
|
---|
76 | paramDepth--;
|
---|
77 | if (end == 0) {
|
---|
78 | start = i + 1; // support "(name) address"
|
---|
79 | nEnd = i;
|
---|
80 | }
|
---|
81 | } else if (paramDepth == 0 && c == '<') {
|
---|
82 | if (start == 0) {
|
---|
83 | nEnd = i;
|
---|
84 | }
|
---|
85 | start = i + 1;
|
---|
86 | } else if (paramDepth == 0 && c == '>') {
|
---|
87 | end = i;
|
---|
88 | if (end != len - 1) {
|
---|
89 | nStart = i + 1;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | // DEBUG: System.out.println( email );
|
---|
95 | if (end == 0) {
|
---|
96 | end = len;
|
---|
97 | }
|
---|
98 | // DEBUG: System.out.println( "address: " + start + " " + end );
|
---|
99 | if (nEnd == 0) {
|
---|
100 | nEnd = len;
|
---|
101 | }
|
---|
102 | // DEBUG: System.out.println( "name: " + nStart + " " + nEnd );
|
---|
103 |
|
---|
104 | this.address = trim(email.substring(start, end), true);
|
---|
105 | this.name = trim(email.substring(nStart, nEnd), false);
|
---|
106 |
|
---|
107 | // if the two substrings are longer than the original, then name
|
---|
108 | // contains address - so reset the name to null
|
---|
109 | if (this.name.length() + this.address.length() > len) {
|
---|
110 | this.name = null;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * A specialised trim() that trims whitespace,
|
---|
116 | * '(', ')', '"', '<', '>' from the start and end of strings
|
---|
117 | */
|
---|
118 | private String trim(String t, boolean trimAngleBrackets) {
|
---|
119 | int start = 0;
|
---|
120 | int end = t.length();
|
---|
121 | boolean trim = false;
|
---|
122 | do {
|
---|
123 | trim = false;
|
---|
124 | if (t.charAt(end - 1) == ')'
|
---|
125 | || (t.charAt(end - 1) == '>' && trimAngleBrackets)
|
---|
126 | || (t.charAt(end - 1) == '"' && t.charAt(end - 2) != '\\')
|
---|
127 | || t.charAt(end - 1) <= '\u0020') {
|
---|
128 | trim = true;
|
---|
129 | end--;
|
---|
130 | }
|
---|
131 | if (t.charAt(start) == '('
|
---|
132 | || (t.charAt(start) == '<' && trimAngleBrackets)
|
---|
133 | || t.charAt(start) == '"'
|
---|
134 | || t.charAt(start) <= '\u0020') {
|
---|
135 | trim = true;
|
---|
136 | start++;
|
---|
137 | }
|
---|
138 | } while (trim);
|
---|
139 | return t.substring(start, end);
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Sets the personal / display name of the address
|
---|
145 | *
|
---|
146 | * @param name the display name
|
---|
147 | */
|
---|
148 | public void setName(String name) {
|
---|
149 | this.name = name;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Sets the email address
|
---|
155 | *
|
---|
156 | * @param address the actual email address (without <>)
|
---|
157 | */
|
---|
158 | public void setAddress(String address) {
|
---|
159 | this.address = address;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Constructs a string "name <address>" or "address"
|
---|
165 | *
|
---|
166 | * @return a string representation of the address
|
---|
167 | */
|
---|
168 | public String toString() {
|
---|
169 | if (name == null) {
|
---|
170 | return address;
|
---|
171 | } else {
|
---|
172 | return name + " <" + address + ">";
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Returns the address
|
---|
179 | *
|
---|
180 | * @return the address part
|
---|
181 | */
|
---|
182 | public String getAddress() {
|
---|
183 | return address;
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Returns the display name
|
---|
189 | *
|
---|
190 | * @return the display name part
|
---|
191 | */
|
---|
192 | public String getName() {
|
---|
193 | return name;
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|