1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.nntp;
  17. import java.util.Calendar;
  18. /***
  19. * The NewGroupsOrNewsQuery class. This is used to issue NNTP NEWGROUPS and
  20. * NEWNEWS queries, implemented by
  21. * <a href="org.apache.commons.net.nntp.NNTPClient.html#listNewNewsgroups">
  22. * listNewNewsGroups </a> and
  23. * <a href="org.apache.commons.net.nntp.NNTPClient.html#listNewNews">
  24. * listNewNews </a> respectively. It prevents you from having to format
  25. * date, time, distribution, and newgroup arguments.
  26. * <p>
  27. * You might use the class as follows:
  28. * <pre>
  29. * query = new NewsGroupsOrNewsQuery(new GregorianCalendar(97, 11, 15), false);
  30. * query.addDistribution("comp");
  31. * NewsgroupInfo[] newsgroups = client.listNewgroups(query);
  32. * </pre>
  33. * This will retrieve the list of newsgroups starting with the comp.
  34. * distribution prefix created since midnight 11/15/97.
  35. * <p>
  36. * <p>
  37. * @author Daniel F. Savarese
  38. * @see NNTPClient
  39. ***/
  40. public final class NewGroupsOrNewsQuery
  41. {
  42. private String __date, __time;
  43. private StringBuffer __distributions;
  44. private StringBuffer __newsgroups;
  45. private boolean __isGMT;
  46. /***
  47. * Creates a new query using the given time as a reference point.
  48. * <p>
  49. * @param date The date since which new groups or news have arrived.
  50. * @param gmt True if the date should be considered as GMT, false if not.
  51. ***/
  52. public NewGroupsOrNewsQuery(Calendar date, boolean gmt)
  53. {
  54. int num;
  55. String str;
  56. StringBuffer buffer;
  57. __distributions = null;
  58. __newsgroups = null;
  59. __isGMT = gmt;
  60. buffer = new StringBuffer();
  61. // Get year
  62. num = date.get(Calendar.YEAR);
  63. str = Integer.toString(num);
  64. num = str.length();
  65. if (num >= 2)
  66. buffer.append(str.substring(num - 2));
  67. else
  68. buffer.append("00");
  69. // Get month
  70. num = date.get(Calendar.MONTH) + 1;
  71. str = Integer.toString(num);
  72. num = str.length();
  73. if (num == 1)
  74. {
  75. buffer.append('0');
  76. buffer.append(str);
  77. }
  78. else if (num == 2)
  79. buffer.append(str);
  80. else
  81. buffer.append("01");
  82. // Get day
  83. num = date.get(Calendar.DAY_OF_MONTH);
  84. str = Integer.toString(num);
  85. num = str.length();
  86. if (num == 1)
  87. {
  88. buffer.append('0');
  89. buffer.append(str);
  90. }
  91. else if (num == 2)
  92. buffer.append(str);
  93. else
  94. buffer.append("01");
  95. __date = buffer.toString();
  96. buffer.setLength(0);
  97. // Get hour
  98. num = date.get(Calendar.HOUR_OF_DAY);
  99. str = Integer.toString(num);
  100. num = str.length();
  101. if (num == 1)
  102. {
  103. buffer.append('0');
  104. buffer.append(str);
  105. }
  106. else if (num == 2)
  107. buffer.append(str);
  108. else
  109. buffer.append("00");
  110. // Get minutes
  111. num = date.get(Calendar.MINUTE);
  112. str = Integer.toString(num);
  113. num = str.length();
  114. if (num == 1)
  115. {
  116. buffer.append('0');
  117. buffer.append(str);
  118. }
  119. else if (num == 2)
  120. buffer.append(str);
  121. else
  122. buffer.append("00");
  123. // Get seconds
  124. num = date.get(Calendar.SECOND);
  125. str = Integer.toString(num);
  126. num = str.length();
  127. if (num == 1)
  128. {
  129. buffer.append('0');
  130. buffer.append(str);
  131. }
  132. else if (num == 2)
  133. buffer.append(str);
  134. else
  135. buffer.append("00");
  136. __time = buffer.toString();
  137. }
  138. /***
  139. * Add a newsgroup to the list of newsgroups being queried. Newsgroups
  140. * added this way are only meaningful to the NEWNEWS command. Newsgroup
  141. * names may include the <code> * </code> wildcard, as in
  142. * <code>comp.lang.* </code> or <code> comp.lang.java.* </code>. Adding
  143. * at least one newsgroup is mandatory for the NEWNEWS command.
  144. * <p>
  145. * @param newsgroup The newsgroup to add to the list of groups to be
  146. * checked for new news.
  147. ***/
  148. public void addNewsgroup(String newsgroup)
  149. {
  150. if (__newsgroups != null)
  151. __newsgroups.append(',');
  152. else
  153. __newsgroups = new StringBuffer();
  154. __newsgroups.append(newsgroup);
  155. }
  156. /***
  157. * Add a newsgroup to the list of newsgroups being queried, but indicate
  158. * that group should not be checked for new news. Newsgroups
  159. * added this way are only meaningful to the NEWNEWS command.
  160. * Newsgroup names may include the <code> * </code> wildcard, as in
  161. * <code>comp.lang.* </code> or <code> comp.lang.java.* </code>.
  162. * <p>
  163. * The following would create a query that searched for new news in
  164. * all comp.lang.java newsgroups except for comp.lang.java.advocacy.
  165. * <pre>
  166. * query.addNewsgroup("comp.lang.java.*");
  167. * query.omitNewsgroup("comp.lang.java.advocacy");
  168. * </pre>
  169. * <p>
  170. * @param newsgroup The newsgroup to add to the list of groups to be
  171. * checked for new news, but which should be omitted from
  172. * the search for new news..
  173. ***/
  174. public void omitNewsgroup(String newsgroup)
  175. {
  176. addNewsgroup("!" + newsgroup);
  177. }
  178. /***
  179. * Add a distribution group to the query. The distribution part of a
  180. * newsgroup is the segment of the name preceding the first dot (e.g.,
  181. * comp, alt, rec). Only those newsgroups matching one of the
  182. * distributions or, in the case of NEWNEWS, an article in a newsgroup
  183. * matching one of the distributions, will be reported as a query result.
  184. * Adding distributions is purely optional.
  185. * <p>
  186. * @param distribution A distribution to add to the query.
  187. ***/
  188. public void addDistribution(String distribution)
  189. {
  190. if (__distributions != null)
  191. __distributions.append(',');
  192. else
  193. __distributions = new StringBuffer();
  194. __distributions.append(distribution);
  195. }
  196. /***
  197. * Return the NNTP query formatted date (year, month, day in the form
  198. * YYMMDD.
  199. * <p>
  200. * @return The NNTP query formatted date.
  201. ***/
  202. public String getDate()
  203. {
  204. return __date;
  205. }
  206. /***
  207. * Return the NNTP query formatted time (hour, minutes, seconds in the form
  208. * HHMMSS.
  209. * <p>
  210. * @return The NNTP query formatted time.
  211. ***/
  212. public String getTime()
  213. {
  214. return __time;
  215. }
  216. /***
  217. * Return whether or not the query date should be treated as GMT.
  218. * <p>
  219. * @return True if the query date is to be treated as GMT, false if not.
  220. ***/
  221. public boolean isGMT()
  222. {
  223. return __isGMT;
  224. }
  225. /***
  226. * Return the comma separated list of distributions. This may be null
  227. * if there are no distributions.
  228. * <p>
  229. * @return The list of distributions, which may be null if no distributions
  230. * have been specified.
  231. ***/
  232. public String getDistributions()
  233. {
  234. return (__distributions == null ? null : __distributions.toString());
  235. }
  236. /***
  237. * Return the comma separated list of newsgroups. This may be null
  238. * if there are no newsgroups
  239. * <p>
  240. * @return The list of newsgroups, which may be null if no newsgroups
  241. * have been specified.
  242. ***/
  243. public String getNewsgroups()
  244. {
  245. return (__newsgroups == null ? null : __newsgroups.toString());
  246. }
  247. }