View Javadoc
1 package org.apache.commons.net.nntp; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Commons" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.util.Calendar; 58 59 /**** 60 * The NewGroupsOrNewsQuery class. This is used to issue NNTP NEWGROUPS and 61 * NEWNEWS queries, implemented by 62 * <a href="org.apache.commons.net.nntp.NNTPClient.html#listNewNewsgroups"> 63 * listNewNewsGroups </a> and 64 * <a href="org.apache.commons.net.nntp.NNTPClient.html#listNewNews"> 65 * listNewNews </a> respectively. It prevents you from having to format 66 * date, time, distribution, and newgroup arguments. 67 * <p> 68 * You might use the class as follows: 69 * <pre> 70 * query = new NewsGroupsOrNewsQuery(new GregorianCalendar(97, 11, 15), false); 71 * query.addDistribution("comp"); 72 * NewsgroupInfo[] newsgroups = client.listNewgroups(query); 73 * </pre> 74 * This will retrieve the list of newsgroups starting with the comp. 75 * distribution prefix created since midnight 11/15/97. 76 * <p> 77 * <p> 78 * @author Daniel F. Savarese 79 * @see NNTPClient 80 ***/ 81 82 public final class NewGroupsOrNewsQuery 83 { 84 private String __date, __time; 85 private StringBuffer __distributions; 86 private StringBuffer __newsgroups; 87 private boolean __isGMT; 88 89 90 /**** 91 * Creates a new query using the given time as a reference point. 92 * <p> 93 * @param date The date since which new groups or news have arrived. 94 * @param gmt True if the date should be considered as GMT, false if not. 95 ***/ 96 public NewGroupsOrNewsQuery(Calendar date, boolean gmt) 97 { 98 int num; 99 String str; 100 StringBuffer buffer; 101 102 __distributions = null; 103 __newsgroups = null; 104 __isGMT = gmt; 105 106 buffer = new StringBuffer(); 107 108 // Get year 109 num = date.get(Calendar.YEAR); 110 str = Integer.toString(num); 111 num = str.length(); 112 113 if (num >= 2) 114 buffer.append(str.substring(num - 2)); 115 else 116 buffer.append("00"); 117 118 // Get month 119 num = date.get(Calendar.MONTH) + 1; 120 str = Integer.toString(num); 121 num = str.length(); 122 123 if (num == 1) 124 { 125 buffer.append('0'); 126 buffer.append(str); 127 } 128 else if (num == 2) 129 buffer.append(str); 130 else 131 buffer.append("01"); 132 133 // Get day 134 num = date.get(Calendar.DAY_OF_MONTH); 135 str = Integer.toString(num); 136 num = str.length(); 137 138 if (num == 1) 139 { 140 buffer.append('0'); 141 buffer.append(str); 142 } 143 else if (num == 2) 144 buffer.append(str); 145 else 146 buffer.append("01"); 147 148 __date = buffer.toString(); 149 150 buffer.setLength(0); 151 152 // Get hour 153 num = date.get(Calendar.HOUR_OF_DAY); 154 str = Integer.toString(num); 155 num = str.length(); 156 157 if (num == 1) 158 { 159 buffer.append('0'); 160 buffer.append(str); 161 } 162 else if (num == 2) 163 buffer.append(str); 164 else 165 buffer.append("00"); 166 167 // Get minutes 168 num = date.get(Calendar.MINUTE); 169 str = Integer.toString(num); 170 num = str.length(); 171 172 if (num == 1) 173 { 174 buffer.append('0'); 175 buffer.append(str); 176 } 177 else if (num == 2) 178 buffer.append(str); 179 else 180 buffer.append("00"); 181 182 183 // Get seconds 184 num = date.get(Calendar.SECOND); 185 str = Integer.toString(num); 186 num = str.length(); 187 188 if (num == 1) 189 { 190 buffer.append('0'); 191 buffer.append(str); 192 } 193 else if (num == 2) 194 buffer.append(str); 195 else 196 buffer.append("00"); 197 198 __time = buffer.toString(); 199 } 200 201 202 /**** 203 * Add a newsgroup to the list of newsgroups being queried. Newsgroups 204 * added this way are only meaningful to the NEWNEWS command. Newsgroup 205 * names may include the <code> * </code> wildcard, as in 206 * <code>comp.lang.* </code> or <code> comp.lang.java.* </code>. Adding 207 * at least one newsgroup is mandatory for the NEWNEWS command. 208 * <p> 209 * @param newsgroup The newsgroup to add to the list of groups to be 210 * checked for new news. 211 ***/ 212 public void addNewsgroup(String newsgroup) 213 { 214 if (__newsgroups != null) 215 __newsgroups.append(','); 216 else 217 __newsgroups = new StringBuffer(); 218 __newsgroups.append(newsgroup); 219 } 220 221 222 /**** 223 * Add a newsgroup to the list of newsgroups being queried, but indicate 224 * that group should not be checked for new news. Newsgroups 225 * added this way are only meaningful to the NEWNEWS command. 226 * Newsgroup names may include the <code> * </code> wildcard, as in 227 * <code>comp.lang.* </code> or <code> comp.lang.java.* </code>. 228 * <p> 229 * The following would create a query that searched for new news in 230 * all comp.lang.java newsgroups except for comp.lang.java.advocacy. 231 * <pre> 232 * query.addNewsgroup("comp.lang.java.*"); 233 * query.omitNewsgroup("comp.lang.java.advocacy"); 234 * </pre> 235 * <p> 236 * @param newsgroup The newsgroup to add to the list of groups to be 237 * checked for new news, but which should be omitted from 238 * the search for new news.. 239 ***/ 240 public void omitNewsgroup(String newsgroup) 241 { 242 addNewsgroup("!" + newsgroup); 243 } 244 245 246 /**** 247 * Add a distribution group to the query. The distribution part of a 248 * newsgroup is the segment of the name preceding the first dot (e.g., 249 * comp, alt, rec). Only those newsgroups matching one of the 250 * distributions or, in the case of NEWNEWS, an article in a newsgroup 251 * matching one of the distributions, will be reported as a query result. 252 * Adding distributions is purely optional. 253 * <p> 254 * @param distribution A distribution to add to the query. 255 ***/ 256 public void addDistribution(String distribution) 257 { 258 if (__distributions != null) 259 __distributions.append(','); 260 else 261 __distributions = new StringBuffer(); 262 __distributions.append(distribution); 263 } 264 265 /**** 266 * Return the NNTP query formatted date (year, month, day in the form 267 * YYMMDD. 268 * <p> 269 * @return The NNTP query formatted date. 270 ***/ 271 public String getDate() 272 { 273 return __date; 274 } 275 276 /**** 277 * Return the NNTP query formatted time (hour, minutes, seconds in the form 278 * HHMMSS. 279 * <p> 280 * @return The NNTP query formatted time. 281 ***/ 282 public String getTime() 283 { 284 return __time; 285 } 286 287 /**** 288 * Return whether or not the query date should be treated as GMT. 289 * <p> 290 * @return True if the query date is to be treated as GMT, false if not. 291 ***/ 292 public boolean isGMT() 293 { 294 return __isGMT; 295 } 296 297 /**** 298 * Return the comma separated list of distributions. This may be null 299 * if there are no distributions. 300 * <p> 301 * @return The list of distributions, which may be null if no distributions 302 * have been specified. 303 ***/ 304 public String getDistributions() 305 { 306 return (__distributions == null ? null : __distributions.toString()); 307 } 308 309 /**** 310 * Return the comma separated list of newsgroups. This may be null 311 * if there are no newsgroups 312 * <p> 313 * @return The list of newsgroups, which may be null if no newsgroups 314 * have been specified. 315 ***/ 316 public String getNewsgroups() 317 { 318 return (__newsgroups == null ? null : __newsgroups.toString()); 319 } 320 }

This page was automatically generated by Maven