Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2011 Aimluck,Inc.
 * http://www.aipo.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashSet;

import java.util.StringTokenizer;

import javax.mail.Address;

import javax.mail.internet.InternetAddress;

public class Main {

    public static String getAddressString(Address[] addresses) {
        if (addresses == null || addresses.length <= 0) {
            return "";
        }
        HashSet<String> foundAddress = new HashSet<String>();

        StringBuffer sb = new StringBuffer();
        InternetAddress addr = null;
        int length = addresses.length;
        for (int i = 0; i < length; i++) {
            addr = (InternetAddress) addresses[i];
            if (foundAddress.contains(addr.getAddress())) {
                continue;
            }
            foundAddress.add(addr.getAddress());

            if (addr.getPersonal() != null) {
                String personaladdr = getOneString(getTokens(addr.getPersonal(), "\r\n"), "");
                sb.append(personaladdr).append(" <").append(addr.getAddress()).append(">, ");
            } else {
                sb.append(addr.getAddress()).append(", ");
            }
        }
        String addressStr = sb.toString();
        return addressStr.substring(0, addressStr.length() - 2);
    }

    public static String getOneString(String[] strs, String delim) {
        if (strs == null) {
            return "";
        }
        String delimiter = delim + " ";
        StringBuffer sb = new StringBuffer();
        int length = strs.length - 1;
        for (int i = 0; i < length; i++) {
            sb.append(strs[i]).append(delimiter);
        }
        sb.append(strs[length]);
        return sb.toString();
    }

    public static String[] getTokens(String line, String delim) {
        if (line == null || line.equals("")) {
            return null;
        }
        if (line.indexOf(delim) < 0) {

            if (delim.length() > 1 && !"\r\n".equals(delim)) {
                String regex = "^.*[" + delim + "].*";
                if (!line.matches(regex)) {
                    return new String[] { line };
                }
            } else {
                return new String[] { line };
            }
        }

        StringTokenizer st = new StringTokenizer(line, delim);
        int length = st.countTokens();
        String[] tokens = new String[length];
        for (int i = 0; i < length; i++) {
            tokens[i] = st.nextToken();
        }
        return tokens;
    }
}