1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.field;
21
22//BEGIN android-changed: Stubbing out logging
23import org.apache.james.mime4j.Log;
24import org.apache.james.mime4j.LogFactory;
25//END android-changed
26import org.apache.james.mime4j.field.address.AddressList;
27import org.apache.james.mime4j.field.address.Mailbox;
28import org.apache.james.mime4j.field.address.MailboxList;
29import org.apache.james.mime4j.field.address.parser.ParseException;
30
31public class MailboxField extends Field {
32    private final Mailbox mailbox;
33    private final ParseException parseException;
34
35    protected MailboxField(final String name, final String body, final String raw, final Mailbox mailbox, final ParseException parseException) {
36        super(name, body, raw);
37        this.mailbox = mailbox;
38        this.parseException = parseException;
39    }
40
41    public Mailbox getMailbox() {
42        return mailbox;
43    }
44
45    public ParseException getParseException() {
46        return parseException;
47    }
48
49    public static class Parser implements FieldParser {
50        private static Log log = LogFactory.getLog(Parser.class);
51
52        public Field parse(final String name, final String body, final String raw) {
53            Mailbox mailbox = null;
54            ParseException parseException = null;
55            try {
56                MailboxList mailboxList = AddressList.parse(body).flatten();
57                if (mailboxList.size() > 0) {
58                    mailbox = mailboxList.get(0);
59                }
60            }
61            catch (ParseException e) {
62                if (log.isDebugEnabled()) {
63                    log.debug("Parsing value '" + body + "': "+ e.getMessage());
64                }
65                parseException = e;
66            }
67            return new MailboxField(name, body, raw, mailbox, parseException);
68        }
69    }
70}
71