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;
021
022import org.apache.james.mime4j.codec.DecodeMonitor;
023import org.apache.james.mime4j.dom.FieldParser;
024import org.apache.james.mime4j.dom.address.AddressList;
025import org.apache.james.mime4j.dom.field.AddressListField;
026import org.apache.james.mime4j.field.address.AddressBuilder;
027import org.apache.james.mime4j.field.address.ParseException;
028import org.apache.james.mime4j.stream.Field;
029
030/**
031 * Address list field such as <code>To</code> or <code>Reply-To</code>.
032 */
033public class AddressListFieldImpl extends AbstractField implements AddressListField {
034
035    private boolean parsed = false;
036
037    private AddressList addressList;
038    private ParseException parseException;
039
040    AddressListFieldImpl(Field rawField, DecodeMonitor monitor) {
041        super(rawField, monitor);
042    }
043
044    /**
045     * @see org.apache.james.mime4j.dom.field.AddressListField#getAddressList()
046     */
047    public AddressList getAddressList() {
048        if (!parsed)
049            parse();
050
051        return addressList;
052    }
053
054    /**
055     * @see org.apache.james.mime4j.dom.field.AddressListField#getParseException()
056     */
057    @Override
058    public ParseException getParseException() {
059        if (!parsed)
060            parse();
061
062        return parseException;
063    }
064
065    private void parse() {
066        String body = getBody();
067
068        try {
069            addressList = AddressBuilder.DEFAULT.parseAddressList(body, monitor);
070        } catch (ParseException e) {
071            parseException = e;
072        }
073
074        parsed = true;
075    }
076
077    public static final FieldParser<AddressListField> PARSER = new FieldParser<AddressListField>() {
078
079        public AddressListField parse(final Field rawField, final DecodeMonitor monitor) {
080            return new AddressListFieldImpl(rawField, monitor);
081        }
082
083    };
084}