Save This Page
Home » apache-tomcat-6.0.16-src » javax » mail » internet » [javadoc | source]
    1   /**
    2    *
    3    * Copyright 2003-2004 The Apache Software Foundation
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package javax.mail.internet;
   19   
   20   import java.util.ArrayList;
   21   import java.util.List;
   22   import java.util.StringTokenizer;
   23   import javax.mail.Address;
   24   
   25   /**
   26    * A representation of an RFC1036 Internet newsgroup address.
   27    *
   28    * @version $Rev: 125583 $ $Date: 2005-01-18 19:44:27 -0800 (Tue, 18 Jan 2005) $
   29    */
   30   public class NewsAddress extends Address {
   31       /**
   32        * The host for this newsgroup
   33        */
   34       protected String host;
   35   
   36       /**
   37        * The name of this newsgroup
   38        */
   39       protected String newsgroup;
   40   
   41       public NewsAddress() {
   42       }
   43   
   44       public NewsAddress(String newsgroup) {
   45           this.newsgroup = newsgroup;
   46       }
   47   
   48       public NewsAddress(String newsgroup, String host) {
   49           this.newsgroup = newsgroup;
   50           this.host = host;
   51       }
   52   
   53       /**
   54        * The type of this address; always "news".
   55        * @return "news"
   56        */
   57       public String getType() {
   58           return "news";
   59       }
   60   
   61       public void setNewsgroup(String newsgroup) {
   62           this.newsgroup = newsgroup;
   63       }
   64   
   65       public String getNewsgroup() {
   66           return newsgroup;
   67       }
   68   
   69       public void setHost(String host) {
   70           this.host = host;
   71       }
   72   
   73       public String getHost() {
   74           return host;
   75       }
   76   
   77       public String toString() {
   78           return host == null ? newsgroup : newsgroup + "@" + host;
   79       }
   80   
   81       public boolean equals(Object o) {
   82           if (this == o) return true;
   83           if (!(o instanceof NewsAddress)) return false;
   84   
   85           final NewsAddress newsAddress = (NewsAddress) o;
   86   
   87           if (host != null ? !host.equals(newsAddress.host) : newsAddress.host != null) return false;
   88           if (newsgroup != null ? !newsgroup.equals(newsAddress.newsgroup) : newsAddress.newsgroup != null) return false;
   89   
   90           return true;
   91       }
   92   
   93       public int hashCode() {
   94           int result;
   95           result = (host != null ? host.hashCode() : 0);
   96           result = 29 * result + (newsgroup != null ? newsgroup.hashCode() : 0);
   97           return result;
   98       }
   99   
  100       /**
  101        * Parse a comma-spearated list of addresses.
  102        *
  103        * @param addresses the list to parse
  104        * @return the array of extracted addresses
  105        * @throws AddressException if one of the addresses is invalid
  106        */
  107       public static NewsAddress[] parse(String addresses) throws AddressException {
  108           List result = new ArrayList();
  109           StringTokenizer tokenizer = new StringTokenizer(addresses, ",");
  110           while (tokenizer.hasMoreTokens()) {
  111               String address = tokenizer.nextToken().trim();
  112               int index = address.indexOf('@');
  113               if (index == -1) {
  114                   result.add(new NewsAddress(address));
  115               } else {
  116                   String newsgroup = address.substring(0, index).trim();
  117                   String host = address.substring(index+1).trim();
  118                   result.add(new NewsAddress(newsgroup, host));
  119               }
  120           }
  121           return (NewsAddress[]) result.toArray(new NewsAddress[result.size()]);
  122       }
  123   
  124       /**
  125        * Convert the supplied addresses to a comma-separated String.
  126        * If addresses is null, returns null; if empty, returns an empty string.
  127        *
  128        * @param addresses the addresses to convert
  129        * @return a comma-separated list of addresses
  130        */
  131       public static String toString(Address[] addresses) {
  132           if (addresses == null) {
  133               return null;
  134           }
  135           if (addresses.length == 0) {
  136               return "";
  137           }
  138           
  139           StringBuffer result = new StringBuffer(addresses.length * 32);
  140           result.append(addresses[0]);
  141           for (int i = 1; i < addresses.length; i++) {
  142               result.append(',').append(addresses[i].toString());
  143           }
  144           return result.toString();
  145       }
  146   }

Save This Page
Home » apache-tomcat-6.0.16-src » javax » mail » internet » [javadoc | source]