View Javadoc

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  
20  package org.apache.james.mime4j.field;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.james.mime4j.field.address.AddressList;
25  import org.apache.james.mime4j.field.address.MailboxList;
26  import org.apache.james.mime4j.field.address.parser.ParseException;
27  import org.apache.james.mime4j.util.ByteSequence;
28  
29  /**
30   * Mailbox-list field such as <code>From</code> or <code>Resent-From</code>.
31   */
32  public class MailboxListField extends AbstractField {
33      private static Log log = LogFactory.getLog(MailboxListField.class);
34  
35      private boolean parsed = false;
36  
37      private MailboxList mailboxList;
38      private ParseException parseException;
39  
40      MailboxListField(final String name, final String body, final ByteSequence raw) {
41          super(name, body, raw);
42      }
43  
44      public MailboxList getMailboxList() {
45          if (!parsed)
46              parse();
47  
48          return mailboxList;
49      }
50  
51      @Override
52      public ParseException getParseException() {
53          if (!parsed)
54              parse();
55  
56          return parseException;
57      }
58  
59      private void parse() {
60          String body = getBody();
61  
62          try {
63              mailboxList = AddressList.parse(body).flatten();
64          } catch (ParseException e) {
65              if (log.isDebugEnabled()) {
66                  log.debug("Parsing value '" + body + "': " + e.getMessage());
67              }
68              parseException = e;
69          }
70  
71          parsed = true;
72      }
73  
74      static final FieldParser PARSER = new FieldParser() {
75          public ParsedField parse(final String name, final String body,
76                  final ByteSequence raw) {
77              return new MailboxListField(name, body, raw);
78          }
79      };
80  }