001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mime4j.field.address;
021
022import java.io.StringReader;
023
024import org.apache.james.mime4j.codec.DecodeMonitor;
025import org.apache.james.mime4j.dom.address.Address;
026import org.apache.james.mime4j.dom.address.AddressList;
027import org.apache.james.mime4j.dom.address.Group;
028import org.apache.james.mime4j.dom.address.Mailbox;
029
030/**
031 * Default (strict) builder for {@link Address} and its subclasses.
032 */
033public class AddressBuilder {
034
035    public static final AddressBuilder DEFAULT = new AddressBuilder();
036
037    protected AddressBuilder() {
038        super();
039    }
040
041    /**
042     * Parses the specified raw string into an address.
043     *
044     * @param rawAddressString
045     *            string to parse.
046     * @param monitor the DecodeMonitor to be used while parsing/decoding
047     * @return an <code>Address</code> object for the specified string.
048     * @throws ParseException if the raw string does not represent a single address.
049     */
050    public Address parseAddress(String rawAddressString, DecodeMonitor monitor) throws ParseException {
051        AddressListParser parser = new AddressListParser(new StringReader(
052                rawAddressString));
053        return Builder.getInstance().buildAddress(parser.parseAddress(), monitor);
054    }
055
056    public Address parseAddress(String rawAddressString) throws ParseException {
057        return parseAddress(rawAddressString, DecodeMonitor.STRICT);
058    }
059
060    /**
061     * Parse the address list string, such as the value of a From, To, Cc, Bcc,
062     * Sender, or Reply-To header.
063     *
064     * The string MUST be unfolded already.
065     * @param monitor the DecodeMonitor to be used while parsing/decoding
066     */
067    public AddressList parseAddressList(String rawAddressList, DecodeMonitor monitor)
068            throws ParseException {
069        AddressListParser parser = new AddressListParser(new StringReader(
070                rawAddressList));
071        return Builder.getInstance().buildAddressList(parser.parseAddressList(), monitor);
072    }
073
074    public AddressList parseAddressList(String rawAddressList) throws ParseException {
075        return parseAddressList(rawAddressList, DecodeMonitor.STRICT);
076    }
077
078    /**
079     * Parses the specified raw string into a mailbox address.
080     *
081     * @param rawMailboxString
082     *            string to parse.
083     * @param monitor the DecodeMonitor to be used while parsing/decoding.
084     * @return a <code>Mailbox</code> object for the specified string.
085     * @throws ParseException
086     *             if the raw string does not represent a single mailbox
087     *             address.
088     */
089    public Mailbox parseMailbox(String rawMailboxString, DecodeMonitor monitor) throws ParseException {
090        AddressListParser parser = new AddressListParser(new StringReader(
091                rawMailboxString));
092        return Builder.getInstance().buildMailbox(parser.parseMailbox(), monitor);
093    }
094
095    public Mailbox parseMailbox(String rawMailboxString) throws ParseException {
096        return parseMailbox(rawMailboxString, DecodeMonitor.STRICT);
097    }
098
099    /**
100     * Parses the specified raw string into a group address.
101     *
102     * @param rawGroupString
103     *            string to parse.
104     * @return a <code>Group</code> object for the specified string.
105     * @throws ParseException
106     *             if the raw string does not represent a single group address.
107     */
108    public Group parseGroup(String rawGroupString, DecodeMonitor monitor) throws ParseException {
109        Address address = parseAddress(rawGroupString, monitor);
110        if (!(address instanceof Group))
111            throw new ParseException("Not a group address");
112
113        return (Group) address;
114    }
115
116    public Group parseGroup(String rawGroupString) throws ParseException {
117        return parseGroup(rawGroupString, DecodeMonitor.STRICT);
118    }
119
120}