source: contrib/MailArchiver/sources/vendor/mime4j/custom/dom/src/main/java/org/apache/james/mime4j/field/address/AddressBuilder.java @ 6785

Revision 6785, 4.9 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

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