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 java.io.StringReader; |
---|
23 | |
---|
24 | import org.apache.james.mime4j.codec.DecodeMonitor; |
---|
25 | import org.apache.james.mime4j.dom.address.Address; |
---|
26 | import org.apache.james.mime4j.dom.address.AddressList; |
---|
27 | import org.apache.james.mime4j.dom.address.Group; |
---|
28 | import org.apache.james.mime4j.dom.address.Mailbox; |
---|
29 | import org.apache.james.mime4j.field.address.AddressListParser; |
---|
30 | import org.apache.james.mime4j.field.address.ParseException; |
---|
31 | |
---|
32 | public class AddressBuilder { |
---|
33 | |
---|
34 | public static final AddressBuilder DEFAULT = new AddressBuilder(); |
---|
35 | |
---|
36 | protected AddressBuilder() { |
---|
37 | super(); |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Parses the specified raw string into an address. |
---|
42 | * |
---|
43 | * @param rawAddressString |
---|
44 | * string to parse. |
---|
45 | * @param monitor the DecodeMonitor to be used while parsing/decoding |
---|
46 | * @return an <code>Address</code> object for the specified string. |
---|
47 | * @throws ParseException if the raw string does not represent a single address. |
---|
48 | */ |
---|
49 | public Address parseAddress(String rawAddressString, DecodeMonitor monitor) throws ParseException { |
---|
50 | AddressListParser parser = new AddressListParser(new StringReader( |
---|
51 | rawAddressString)); |
---|
52 | return Builder.getInstance().buildAddress(parser.parseAddress(), monitor); |
---|
53 | } |
---|
54 | |
---|
55 | public Address parseAddress(String rawAddressString) throws ParseException { |
---|
56 | return parseAddress(rawAddressString, DecodeMonitor.STRICT); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Parse the address list string, such as the value of a From, To, Cc, Bcc, |
---|
61 | * Sender, or Reply-To header. |
---|
62 | * |
---|
63 | * The string MUST be unfolded already. |
---|
64 | * @param monitor the DecodeMonitor to be used while parsing/decoding |
---|
65 | */ |
---|
66 | public AddressList parseAddressList(String rawAddressList, DecodeMonitor monitor) |
---|
67 | throws ParseException { |
---|
68 | AddressListParser parser = new AddressListParser(new StringReader( |
---|
69 | rawAddressList)); |
---|
70 | return Builder.getInstance().buildAddressList(parser.parseAddressList(), monitor); |
---|
71 | } |
---|
72 | |
---|
73 | public AddressList parseAddressList(String rawAddressList) throws ParseException { |
---|
74 | return parseAddressList(rawAddressList, DecodeMonitor.STRICT); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Parses the specified raw string into a mailbox address. |
---|
79 | * |
---|
80 | * @param rawMailboxString |
---|
81 | * string to parse. |
---|
82 | * @param monitor the DecodeMonitor to be used while parsing/decoding. |
---|
83 | * @return a <code>Mailbox</code> object for the specified string. |
---|
84 | * @throws ParseException |
---|
85 | * if the raw string does not represent a single mailbox |
---|
86 | * address. |
---|
87 | */ |
---|
88 | public Mailbox parseMailbox(String rawMailboxString, DecodeMonitor monitor) throws ParseException { |
---|
89 | AddressListParser parser = new AddressListParser(new StringReader( |
---|
90 | rawMailboxString)); |
---|
91 | return Builder.getInstance().buildMailbox(parser.parseMailbox(), monitor); |
---|
92 | } |
---|
93 | |
---|
94 | public Mailbox parseMailbox(String rawMailboxString) throws ParseException { |
---|
95 | return parseMailbox(rawMailboxString, DecodeMonitor.STRICT); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Parses the specified raw string into a group address. |
---|
100 | * |
---|
101 | * @param rawGroupString |
---|
102 | * string to parse. |
---|
103 | * @return a <code>Group</code> object for the specified string. |
---|
104 | * @throws ParseException |
---|
105 | * if the raw string does not represent a single group address. |
---|
106 | */ |
---|
107 | public Group parseGroup(String rawGroupString, DecodeMonitor monitor) throws ParseException { |
---|
108 | Address address = parseAddress(rawGroupString, monitor); |
---|
109 | if (!(address instanceof Group)) |
---|
110 | throw new ParseException("Not a group address"); |
---|
111 | |
---|
112 | return (Group) address; |
---|
113 | } |
---|
114 | |
---|
115 | public Group parseGroup(String rawGroupString) throws ParseException { |
---|
116 | return parseGroup(rawGroupString, DecodeMonitor.STRICT); |
---|
117 | } |
---|
118 | |
---|
119 | } |
---|