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