KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > email > EmailAddress


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.email;
19
20 /**
21  * Holds an email address.
22  *
23  * @since Ant 1.5
24  */

25 public class EmailAddress {
26     private String JavaDoc name;
27     private String JavaDoc address;
28
29
30     /** Creates an empty email address */
31     public EmailAddress() {
32     }
33
34
35     /**
36      * Creates a new email address based on the given string
37      *
38      * @param email the email address (with or without <>)
39      * Acceptable forms include:
40      * address
41      * <address>
42      * name <address>
43      * <address> name
44      * (name) address
45      * address (name)
46      */

47     // Make a limited attempt to extract a sanitized name and email address
48
// Algorithm based on the one found in Ant's MailMessage.java
49
public EmailAddress(String JavaDoc email) {
50         final int minLen = 9;
51         int len = email.length();
52
53         // shortcut for "<address>"
54
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; // support "address (name)"
74
nStart = i + 1;
75                 }
76             } else if (c == ')') {
77                 paramDepth--;
78                 if (end == 0) {
79                     start = i + 1; // support "(name) address"
80
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         // DEBUG: System.out.println( email );
96
if (end == 0) {
97             end = len;
98         }
99         // DEBUG: System.out.println( "address: " + start + " " + end );
100
if (nEnd == 0) {
101             nEnd = len;
102         }
103         // DEBUG: System.out.println( "name: " + nStart + " " + nEnd );
104

105         this.address = trim(email.substring(start, end), true);
106         this.name = trim(email.substring(nStart, nEnd), false);
107
108         // if the two substrings are longer than the original, then name
109
// contains address - so reset the name to null
110
if (this.name.length() + this.address.length() > len) {
111             this.name = null;
112         }
113     }
114
115     /**
116      * A specialised trim() that trims whitespace,
117      * '(', ')', '"', '<', '>' from the start and end of strings
118      */

119     private String JavaDoc trim(String JavaDoc 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     /**
145      * Sets the personal / display name of the address
146      *
147      * @param name the display name
148      */

149     public void setName(String JavaDoc name) {
150         this.name = name;
151     }
152
153
154     /**
155      * Sets the email address
156      *
157      * @param address the actual email address (without <>)
158      */

159     public void setAddress(String JavaDoc address) {
160         this.address = address;
161     }
162
163
164     /**
165      * Constructs a string "name &lt;address&gt;" or "address"
166      *
167      * @return a string representation of the address
168      */

169     public String JavaDoc toString() {
170         if (name == null) {
171             return address;
172         } else {
173             return name + " <" + address + ">";
174         }
175     }
176
177
178     /**
179      * Returns the address
180      *
181      * @return the address part
182      */

183     public String JavaDoc getAddress() {
184         return address;
185     }
186
187
188     /**
189      * Returns the display name
190      *
191      * @return the display name part
192      */

193     public String JavaDoc getName() {
194         return name;
195     }
196 }
197
198
Popular Tags