1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.field.address; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.codec.EncoderUtil; |
---|
23 | import org.apache.james.mime4j.dom.address.Address; |
---|
24 | import org.apache.james.mime4j.dom.address.Group; |
---|
25 | import org.apache.james.mime4j.dom.address.Mailbox; |
---|
26 | |
---|
27 | public class AddressFormatter { |
---|
28 | |
---|
29 | public static final AddressFormatter DEFAULT = new AddressFormatter(); |
---|
30 | |
---|
31 | protected AddressFormatter() { |
---|
32 | super(); |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * Formats the address as a human readable string, not including the route. |
---|
37 | * The resulting string is intended for display purposes only and cannot be |
---|
38 | * used for transport purposes. |
---|
39 | * |
---|
40 | * For example, if the unparsed address was |
---|
41 | * |
---|
42 | * <"Joe Cheng"@joecheng.com> |
---|
43 | * |
---|
44 | * this method would return |
---|
45 | * |
---|
46 | * <Joe Cheng@joecheng.com> |
---|
47 | * |
---|
48 | * which is not valid for transport; the local part would need to be |
---|
49 | * re-quoted. |
---|
50 | * |
---|
51 | * @param includeRoute |
---|
52 | * <code>true</code> if the route should be included if it |
---|
53 | * exists, <code>false</code> otherwise. |
---|
54 | * @return a string representation of this address intended to be displayed. |
---|
55 | */ |
---|
56 | public void format(final StringBuilder sb, final Address address, boolean includeRoute) { |
---|
57 | if (address == null) { |
---|
58 | return; |
---|
59 | } |
---|
60 | if (address instanceof Mailbox) { |
---|
61 | format(sb, (Mailbox) address, includeRoute); |
---|
62 | } else if (address instanceof Group) { |
---|
63 | format(sb, (Group) address, includeRoute); |
---|
64 | } else { |
---|
65 | throw new IllegalArgumentException("Unsuppported Address class: " + address.getClass()); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Returns a string representation of this address that can be used for |
---|
71 | * transport purposes. The route is never included in this representation |
---|
72 | * because routes are obsolete and RFC 5322 states that obsolete syntactic |
---|
73 | * forms MUST NOT be generated. |
---|
74 | * |
---|
75 | * @return a string representation of this address intended for transport |
---|
76 | * purposes. |
---|
77 | */ |
---|
78 | public void encode(final StringBuilder sb, final Address address) { |
---|
79 | if (address == null) { |
---|
80 | return; |
---|
81 | } |
---|
82 | if (address instanceof Mailbox) { |
---|
83 | encode(sb, (Mailbox) address); |
---|
84 | } else if (address instanceof Group) { |
---|
85 | encode(sb, (Group) address); |
---|
86 | } else { |
---|
87 | throw new IllegalArgumentException("Unsuppported Address class: " + address.getClass()); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | public void format(final StringBuilder sb, final Mailbox mailbox, boolean includeRoute) { |
---|
92 | if (sb == null) { |
---|
93 | throw new IllegalArgumentException("StringBuilder may not be null"); |
---|
94 | } |
---|
95 | if (mailbox == null) { |
---|
96 | throw new IllegalArgumentException("Mailbox may not be null"); |
---|
97 | } |
---|
98 | includeRoute &= mailbox.getRoute() != null; |
---|
99 | boolean includeAngleBrackets = mailbox.getName() != null || includeRoute; |
---|
100 | if (mailbox.getName() != null) { |
---|
101 | sb.append(mailbox.getName()); |
---|
102 | sb.append(' '); |
---|
103 | } |
---|
104 | if (includeAngleBrackets) { |
---|
105 | sb.append('<'); |
---|
106 | } |
---|
107 | if (includeRoute) { |
---|
108 | sb.append(mailbox.getRoute().toRouteString()); |
---|
109 | sb.append(':'); |
---|
110 | } |
---|
111 | sb.append(mailbox.getLocalPart()); |
---|
112 | if (mailbox.getDomain() != null) { |
---|
113 | sb.append('@'); |
---|
114 | sb.append(mailbox.getDomain()); |
---|
115 | } |
---|
116 | if (includeAngleBrackets) { |
---|
117 | sb.append('>'); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | public String format(final Mailbox mailbox, boolean includeRoute) { |
---|
122 | StringBuilder sb = new StringBuilder(); |
---|
123 | format(sb, mailbox, includeRoute); |
---|
124 | return sb.toString(); |
---|
125 | } |
---|
126 | |
---|
127 | public void encode(final StringBuilder sb, final Mailbox mailbox) { |
---|
128 | if (sb == null) { |
---|
129 | throw new IllegalArgumentException("StringBuilder may not be null"); |
---|
130 | } |
---|
131 | if (mailbox == null) { |
---|
132 | throw new IllegalArgumentException("Mailbox may not be null"); |
---|
133 | } |
---|
134 | if (mailbox.getName() != null) { |
---|
135 | sb.append(EncoderUtil.encodeAddressDisplayName(mailbox.getName())); |
---|
136 | sb.append(" <"); |
---|
137 | } |
---|
138 | sb.append(EncoderUtil.encodeAddressLocalPart(mailbox.getLocalPart())); |
---|
139 | // domain = dot-atom / domain-literal |
---|
140 | // domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] |
---|
141 | // dtext = %d33-90 / %d94-126 |
---|
142 | if (mailbox.getDomain() != null) { |
---|
143 | sb.append('@'); |
---|
144 | sb.append(mailbox.getDomain()); |
---|
145 | } |
---|
146 | if (mailbox.getName() != null) { |
---|
147 | sb.append('>'); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | public String encode(final Mailbox mailbox) { |
---|
152 | StringBuilder sb = new StringBuilder(); |
---|
153 | encode(sb, mailbox); |
---|
154 | return sb.toString(); |
---|
155 | } |
---|
156 | |
---|
157 | public void format(final StringBuilder sb, final Group group, boolean includeRoute) { |
---|
158 | if (sb == null) { |
---|
159 | throw new IllegalArgumentException("StringBuilder may not be null"); |
---|
160 | } |
---|
161 | if (group == null) { |
---|
162 | throw new IllegalArgumentException("Group may not be null"); |
---|
163 | } |
---|
164 | sb.append(group.getName()); |
---|
165 | sb.append(':'); |
---|
166 | |
---|
167 | boolean first = true; |
---|
168 | for (Mailbox mailbox : group.getMailboxes()) { |
---|
169 | if (first) { |
---|
170 | first = false; |
---|
171 | } else { |
---|
172 | sb.append(','); |
---|
173 | } |
---|
174 | sb.append(' '); |
---|
175 | format(sb, mailbox, includeRoute); |
---|
176 | } |
---|
177 | sb.append(";"); |
---|
178 | } |
---|
179 | |
---|
180 | public String format(final Group group, boolean includeRoute) { |
---|
181 | StringBuilder sb = new StringBuilder(); |
---|
182 | format(sb, group, includeRoute); |
---|
183 | return sb.toString(); |
---|
184 | } |
---|
185 | |
---|
186 | public void encode(final StringBuilder sb, final Group group) { |
---|
187 | if (sb == null) { |
---|
188 | throw new IllegalArgumentException("StringBuilder may not be null"); |
---|
189 | } |
---|
190 | if (group == null) { |
---|
191 | throw new IllegalArgumentException("Group may not be null"); |
---|
192 | } |
---|
193 | sb.append(EncoderUtil.encodeAddressDisplayName(group.getName())); |
---|
194 | sb.append(':'); |
---|
195 | boolean first = true; |
---|
196 | for (Mailbox mailbox : group.getMailboxes()) { |
---|
197 | if (first) { |
---|
198 | first = false; |
---|
199 | } else { |
---|
200 | sb.append(','); |
---|
201 | } |
---|
202 | |
---|
203 | sb.append(' '); |
---|
204 | encode(sb, mailbox); |
---|
205 | } |
---|
206 | sb.append(';'); |
---|
207 | } |
---|
208 | |
---|
209 | public String encode(final Group group) { |
---|
210 | StringBuilder sb = new StringBuilder(); |
---|
211 | encode(sb, group); |
---|
212 | return sb.toString(); |
---|
213 | } |
---|
214 | |
---|
215 | } |
---|