1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail.internet;
  6. import java.util.Vector;
  7. import java.util.StringTokenizer;
  8. import javax.mail.*;
  9. /**
  10. * This class models an RFC1036 newsgroup address.
  11. *
  12. * @author Bill Shannon
  13. * @author John Mani
  14. */
  15. public class NewsAddress extends Address {
  16. protected String newsgroup;
  17. protected String host; // may be null
  18. /**
  19. * Default constructor.
  20. */
  21. public NewsAddress() { }
  22. /**
  23. * Construct a NewsAddress with the given newsgroup.
  24. *
  25. * @param newsgroup the newsgroup
  26. */
  27. public NewsAddress(String newsgroup) {
  28. this(newsgroup, null);
  29. }
  30. /**
  31. * Construct a NewsAddress with the given newsgroup and host.
  32. *
  33. * @param newsgroup the newsgroup
  34. * @param host the host
  35. */
  36. public NewsAddress(String newsgroup, String host) {
  37. this.newsgroup = newsgroup;
  38. this.host = host;
  39. }
  40. /**
  41. * Return the type of this address. The type of a NewsAddress
  42. * is "news".
  43. */
  44. public String getType() {
  45. return "news";
  46. }
  47. /**
  48. * Set the newsgroup.
  49. *
  50. * @param newsgroup the newsgroup
  51. */
  52. public void setNewsgroup(String newsgroup) {
  53. this.newsgroup = newsgroup;
  54. }
  55. /**
  56. * Get the newsgroup.
  57. *
  58. * @return newsgroup
  59. */
  60. public String getNewsgroup() {
  61. return newsgroup;
  62. }
  63. /**
  64. * Set the host.
  65. *
  66. * @param host the host
  67. */
  68. public void setHost(String host) {
  69. this.host = host;
  70. }
  71. /**
  72. * Get the host.
  73. *
  74. * @return host
  75. */
  76. public String getHost() {
  77. return host;
  78. }
  79. /**
  80. * Convert this address into a RFC 1036 address.
  81. *
  82. * @return newsgroup
  83. */
  84. public String toString() {
  85. return newsgroup;
  86. }
  87. /**
  88. * The equality operator.
  89. */
  90. public boolean equals(Object a) {
  91. if (!(a instanceof NewsAddress))
  92. return false;
  93. NewsAddress s = (NewsAddress)a;
  94. return newsgroup.equals(s.newsgroup) &&
  95. ((host == null && s.host == null) ||
  96. (host != null && s.host != null && host.equalsIgnoreCase(s.host)));
  97. }
  98. /**
  99. * Compute a hash code for the address.
  100. */
  101. public int hashCode() {
  102. int hash = 0;
  103. if (newsgroup != null)
  104. hash += newsgroup.hashCode();
  105. if (host != null)
  106. hash += host.toLowerCase().hashCode();
  107. return hash;
  108. }
  109. /**
  110. * Convert the given array of NewsAddress objects into
  111. * a comma separated sequence of address strings. The
  112. * resulting string contains only US-ASCII characters, and
  113. * hence is mail-safe.
  114. *
  115. * @param addresses array of NewsAddress objects
  116. * @exception ClassCastException, if any address object in the
  117. * given array is not a NewsAddress objects. Note
  118. * that this is a RuntimeException.
  119. * @return comma separated address strings
  120. */
  121. public static String toString(Address[] addresses) {
  122. if (addresses == null || addresses.length == 0)
  123. return null;
  124. StringBuffer s =
  125. new StringBuffer(((NewsAddress)addresses[0]).toString());
  126. for (int i = 1; i < addresses.length; i++)
  127. s.append(",").append(((NewsAddress)addresses[i]).toString());
  128. return s.toString();
  129. }
  130. /**
  131. * Parse the given comma separated sequence of newsgroup into
  132. * NewsAddress objects.
  133. *
  134. * @param newsgroups comma separated newsgroup string
  135. * @return array of NewsAddress objects
  136. * @exception AddressException if the parse failed
  137. */
  138. public static NewsAddress[] parse(String newsgroups)
  139. throws AddressException {
  140. // XXX - verify format of newsgroup name?
  141. StringTokenizer st = new StringTokenizer(newsgroups, ",");
  142. Vector nglist = new Vector();
  143. while (st.hasMoreTokens()) {
  144. String ng = st.nextToken();
  145. nglist.addElement(new NewsAddress(ng));
  146. }
  147. int size = nglist.size();
  148. NewsAddress[] na = new NewsAddress[size];
  149. if (size > 0)
  150. nglist.copyInto(na);
  151. return na;
  152. }
  153. }