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    
020    package org.apache.james.mime4j.field;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.james.mime4j.field.address.AddressList;
025    import org.apache.james.mime4j.field.address.parser.ParseException;
026    import org.apache.james.mime4j.util.ByteSequence;
027    
028    /**
029     * Address list field such as <code>To</code> or <code>Reply-To</code>.
030     */
031    public class AddressListField extends AbstractField {
032        private static Log log = LogFactory.getLog(AddressListField.class);
033    
034        private boolean parsed = false;
035    
036        private AddressList addressList;
037        private ParseException parseException;
038    
039        AddressListField(String name, String body, ByteSequence raw) {
040            super(name, body, raw);
041        }
042    
043        public AddressList getAddressList() {
044            if (!parsed)
045                parse();
046    
047            return addressList;
048        }
049    
050        @Override
051        public ParseException getParseException() {
052            if (!parsed)
053                parse();
054    
055            return parseException;
056        }
057    
058        private void parse() {
059            String body = getBody();
060    
061            try {
062                addressList = AddressList.parse(body);
063            } catch (ParseException e) {
064                if (log.isDebugEnabled()) {
065                    log.debug("Parsing value '" + body + "': " + e.getMessage());
066                }
067                parseException = e;
068            }
069    
070            parsed = true;
071        }
072    
073        static final FieldParser PARSER = new FieldParser() {
074            public ParsedField parse(final String name, final String body,
075                    final ByteSequence raw) {
076                return new AddressListField(name, body, raw);
077            }
078        };
079    }