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