001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package javax.mail.internet;
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    import java.util.StringTokenizer;
023    
024    import javax.mail.Address;
025    
026    import sun.security.provider.Sun;
027    
028    /**
029     * A representation of an RFC1036 Internet newsgroup address.
030     *
031     * @version $Rev: 381393 $ $Date: 2006-02-27 09:38:03 -0800 (Mon, 27 Feb 2006) $
032     */
033    public class NewsAddress extends Address {
034        /**
035         * The host for this newsgroup
036         */
037        protected String host;
038    
039        /**
040         * The name of this newsgroup
041         */
042        protected String newsgroup;
043    
044        public NewsAddress() {
045        }
046    
047        public NewsAddress(String newsgroup) {
048            this.newsgroup = newsgroup;
049        }
050    
051        public NewsAddress(String newsgroup, String host) {
052            this.newsgroup = newsgroup;
053            this.host = host;
054        }
055    
056        /**
057         * The type of this address; always "news".
058         * @return "news"
059         */
060        public String getType() {
061            return "news";
062        }
063    
064        public void setNewsgroup(String newsgroup) {
065            this.newsgroup = newsgroup;
066        }
067    
068        public String getNewsgroup() {
069            return newsgroup;
070        }
071    
072        public void setHost(String host) {
073            this.host = host;
074        }
075    
076        public String getHost() {
077            return host;
078        }
079    
080        public String toString() {
081            // Sun impl only appears to return the newsgroup name, no host.
082            return newsgroup;
083        }
084    
085        public boolean equals(Object o) {
086            if (this == o) return true;
087            if (!(o instanceof NewsAddress)) return false;
088    
089            final NewsAddress newsAddress = (NewsAddress) o;
090    
091            if (host != null ? !host.equals(newsAddress.host) : newsAddress.host != null) return false;
092            if (newsgroup != null ? !newsgroup.equals(newsAddress.newsgroup) : newsAddress.newsgroup != null) return false;
093    
094            return true;
095        }
096    
097        public int hashCode() {
098            int result;
099            result = (host != null ? host.toLowerCase().hashCode() : 0);
100            result = 29 * result + (newsgroup != null ? newsgroup.hashCode() : 0);
101            return result;
102        }
103    
104        /**
105         * Parse a comma-spearated list of addresses.
106         *
107         * @param addresses the list to parse
108         * @return the array of extracted addresses
109         * @throws AddressException if one of the addresses is invalid
110         */
111        public static NewsAddress[] parse(String addresses) throws AddressException {
112            List result = new ArrayList();
113            StringTokenizer tokenizer = new StringTokenizer(addresses, ",");
114            while (tokenizer.hasMoreTokens()) {
115                String address = tokenizer.nextToken().trim();
116                int index = address.indexOf('@');
117                if (index == -1) {
118                    result.add(new NewsAddress(address));
119                } else {
120                    String newsgroup = address.substring(0, index).trim();
121                    String host = address.substring(index+1).trim();
122                    result.add(new NewsAddress(newsgroup, host));
123                }
124            }
125            return (NewsAddress[]) result.toArray(new NewsAddress[result.size()]);
126        }
127    
128        /**
129         * Convert the supplied addresses to a comma-separated String.
130         * If addresses is null, returns null; if empty, returns an empty string.
131         *
132         * @param addresses the addresses to convert
133         * @return a comma-separated list of addresses
134         */
135        public static String toString(Address[] addresses) {
136            if (addresses == null) {
137                return null;
138            }
139            if (addresses.length == 0) {
140                return "";
141            }
142    
143            StringBuffer result = new StringBuffer(addresses.length * 32);
144            result.append(addresses[0]);
145            for (int i = 1; i < addresses.length; i++) {
146                result.append(',').append(addresses[i].toString());
147            }
148            return result.toString();
149        }
150    }