Home | History | Annotate | Download | only in address
      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.address;
     21 
     22 import org.apache.james.mime4j.field.address.parser.AddressListParser;
     23 import org.apache.james.mime4j.field.address.parser.ParseException;
     24 
     25 import java.io.StringReader;
     26 import java.util.ArrayList;
     27 
     28 /**
     29  * An immutable, random-access list of Address objects.
     30  *
     31  *
     32  */
     33 public class AddressList {
     34 
     35 	private ArrayList<Address> addresses;
     36 
     37 	/**
     38 	 * @param addresses An ArrayList that contains only Address objects.
     39 	 * @param dontCopy true iff it is not possible for the addresses ArrayList to be modified by someone else.
     40 	 */
     41 	public AddressList(ArrayList<Address> addresses, boolean dontCopy) {
     42 		if (addresses != null)
     43 			this.addresses = (dontCopy ? addresses : new ArrayList<Address>(addresses));
     44 		else
     45 			this.addresses = new ArrayList<Address>(0);
     46 	}
     47 
     48 	/**
     49 	 * The number of elements in this list.
     50 	 */
     51 	public int size() {
     52 		return addresses.size();
     53 	}
     54 
     55 	/**
     56 	 * Gets an address.
     57 	 */
     58 	public Address get(int index) {
     59 		if (0 > index || size() <= index)
     60 			throw new IndexOutOfBoundsException();
     61 		return addresses.get(index);
     62 	}
     63 
     64 	/**
     65 	 * Returns a flat list of all mailboxes represented
     66 	 * in this address list. Use this if you don't care
     67 	 * about grouping.
     68 	 */
     69 	public MailboxList flatten() {
     70 		// in the common case, all addresses are mailboxes
     71 		boolean groupDetected = false;
     72 		for (int i = 0; i < size(); i++) {
     73 			if (!(get(i) instanceof Mailbox)) {
     74 				groupDetected = true;
     75 				break;
     76 			}
     77 		}
     78 
     79 		if (!groupDetected)
     80 			return new MailboxList(addresses, true);
     81 
     82 		ArrayList<Address> results = new ArrayList<Address>();
     83 		for (int i = 0; i < size(); i++) {
     84 			Address addr = get(i);
     85 			addr.addMailboxesTo(results);
     86 		}
     87 
     88 		// copy-on-construct this time, because subclasses
     89 		// could have held onto a reference to the results
     90 		return new MailboxList(results, false);
     91 	}
     92 
     93 	/**
     94 	 * Dumps a representation of this address list to
     95 	 * stdout, for debugging purposes.
     96 	 */
     97 	public void print() {
     98 		for (int i = 0; i < size(); i++) {
     99 			Address addr = get(i);
    100 			System.out.println(addr.toString());
    101 		}
    102 	}
    103 
    104 	/**
    105 	 * Parse the address list string, such as the value
    106 	 * of a From, To, Cc, Bcc, Sender, or Reply-To
    107 	 * header.
    108 	 *
    109 	 * The string MUST be unfolded already.
    110 	 */
    111 	public static AddressList parse(String rawAddressList) throws ParseException {
    112 		AddressListParser parser = new AddressListParser(new StringReader(rawAddressList));
    113 		return Builder.getInstance().buildAddressList(parser.parse());
    114 	}
    115 
    116 	/**
    117 	 * Test console.
    118 	 */
    119 	public static void main(String[] args) throws Exception {
    120 		java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
    121 		while (true) {
    122 			try {
    123 				System.out.print("> ");
    124 				String line = reader.readLine();
    125 				if (line.length() == 0 || line.toLowerCase().equals("exit") || line.toLowerCase().equals("quit")) {
    126 					System.out.println("Goodbye.");
    127 					return;
    128 				}
    129 				AddressList list = parse(line);
    130 				list.print();
    131 			}
    132 			catch(Exception e) {
    133 				e.printStackTrace();
    134 				Thread.sleep(300);
    135 			}
    136 		}
    137 	}
    138 }
    139