001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.nntp;
017:
018: import java.util.Calendar;
019:
020: /***
021: * The NewGroupsOrNewsQuery class. This is used to issue NNTP NEWGROUPS and
022: * NEWNEWS queries, implemented by
023: * {@link org.apache.commons.net.nntp.NNTPClient#listNewNewsgroups listNewNewsGroups }
024: * and
025: * {@link org.apache.commons.net.nntp.NNTPClient#listNewNews listNewNews }
026: * respectively. It prevents you from having to format
027: * date, time, distribution, and newgroup arguments.
028: * <p>
029: * You might use the class as follows:
030: * <pre>
031: * query = new NewsGroupsOrNewsQuery(new GregorianCalendar(97, 11, 15), false);
032: * query.addDistribution("comp");
033: * NewsgroupInfo[] newsgroups = client.listNewgroups(query);
034: * </pre>
035: * This will retrieve the list of newsgroups starting with the comp.
036: * distribution prefix created since midnight 11/15/97.
037: * <p>
038: * <p>
039: * @author Daniel F. Savarese
040: * @see NNTPClient
041: ***/
042:
043: public final class NewGroupsOrNewsQuery {
044: private String __date, __time;
045: private StringBuffer __distributions;
046: private StringBuffer __newsgroups;
047: private boolean __isGMT;
048:
049: /***
050: * Creates a new query using the given time as a reference point.
051: * <p>
052: * @param date The date since which new groups or news have arrived.
053: * @param gmt True if the date should be considered as GMT, false if not.
054: ***/
055: public NewGroupsOrNewsQuery(Calendar date, boolean gmt) {
056: int num;
057: String str;
058: StringBuffer buffer;
059:
060: __distributions = null;
061: __newsgroups = null;
062: __isGMT = gmt;
063:
064: buffer = new StringBuffer();
065:
066: // Get year
067: num = date.get(Calendar.YEAR);
068: str = Integer.toString(num);
069: num = str.length();
070:
071: if (num >= 2)
072: buffer.append(str.substring(num - 2));
073: else
074: buffer.append("00");
075:
076: // Get month
077: num = date.get(Calendar.MONTH) + 1;
078: str = Integer.toString(num);
079: num = str.length();
080:
081: if (num == 1) {
082: buffer.append('0');
083: buffer.append(str);
084: } else if (num == 2)
085: buffer.append(str);
086: else
087: buffer.append("01");
088:
089: // Get day
090: num = date.get(Calendar.DAY_OF_MONTH);
091: str = Integer.toString(num);
092: num = str.length();
093:
094: if (num == 1) {
095: buffer.append('0');
096: buffer.append(str);
097: } else if (num == 2)
098: buffer.append(str);
099: else
100: buffer.append("01");
101:
102: __date = buffer.toString();
103:
104: buffer.setLength(0);
105:
106: // Get hour
107: num = date.get(Calendar.HOUR_OF_DAY);
108: str = Integer.toString(num);
109: num = str.length();
110:
111: if (num == 1) {
112: buffer.append('0');
113: buffer.append(str);
114: } else if (num == 2)
115: buffer.append(str);
116: else
117: buffer.append("00");
118:
119: // Get minutes
120: num = date.get(Calendar.MINUTE);
121: str = Integer.toString(num);
122: num = str.length();
123:
124: if (num == 1) {
125: buffer.append('0');
126: buffer.append(str);
127: } else if (num == 2)
128: buffer.append(str);
129: else
130: buffer.append("00");
131:
132: // Get seconds
133: num = date.get(Calendar.SECOND);
134: str = Integer.toString(num);
135: num = str.length();
136:
137: if (num == 1) {
138: buffer.append('0');
139: buffer.append(str);
140: } else if (num == 2)
141: buffer.append(str);
142: else
143: buffer.append("00");
144:
145: __time = buffer.toString();
146: }
147:
148: /***
149: * Add a newsgroup to the list of newsgroups being queried. Newsgroups
150: * added this way are only meaningful to the NEWNEWS command. Newsgroup
151: * names may include the <code> * </code> wildcard, as in
152: * <code>comp.lang.* </code> or <code> comp.lang.java.* </code>. Adding
153: * at least one newsgroup is mandatory for the NEWNEWS command.
154: * <p>
155: * @param newsgroup The newsgroup to add to the list of groups to be
156: * checked for new news.
157: ***/
158: public void addNewsgroup(String newsgroup) {
159: if (__newsgroups != null)
160: __newsgroups.append(',');
161: else
162: __newsgroups = new StringBuffer();
163: __newsgroups.append(newsgroup);
164: }
165:
166: /***
167: * Add a newsgroup to the list of newsgroups being queried, but indicate
168: * that group should not be checked for new news. Newsgroups
169: * added this way are only meaningful to the NEWNEWS command.
170: * Newsgroup names may include the <code> * </code> wildcard, as in
171: * <code>comp.lang.* </code> or <code> comp.lang.java.* </code>.
172: * <p>
173: * The following would create a query that searched for new news in
174: * all comp.lang.java newsgroups except for comp.lang.java.advocacy.
175: * <pre>
176: * query.addNewsgroup("comp.lang.java.*");
177: * query.omitNewsgroup("comp.lang.java.advocacy");
178: * </pre>
179: * <p>
180: * @param newsgroup The newsgroup to add to the list of groups to be
181: * checked for new news, but which should be omitted from
182: * the search for new news..
183: ***/
184: public void omitNewsgroup(String newsgroup) {
185: addNewsgroup("!" + newsgroup);
186: }
187:
188: /***
189: * Add a distribution group to the query. The distribution part of a
190: * newsgroup is the segment of the name preceding the first dot (e.g.,
191: * comp, alt, rec). Only those newsgroups matching one of the
192: * distributions or, in the case of NEWNEWS, an article in a newsgroup
193: * matching one of the distributions, will be reported as a query result.
194: * Adding distributions is purely optional.
195: * <p>
196: * @param distribution A distribution to add to the query.
197: ***/
198: public void addDistribution(String distribution) {
199: if (__distributions != null)
200: __distributions.append(',');
201: else
202: __distributions = new StringBuffer();
203: __distributions.append(distribution);
204: }
205:
206: /***
207: * Return the NNTP query formatted date (year, month, day in the form
208: * YYMMDD.
209: * <p>
210: * @return The NNTP query formatted date.
211: ***/
212: public String getDate() {
213: return __date;
214: }
215:
216: /***
217: * Return the NNTP query formatted time (hour, minutes, seconds in the form
218: * HHMMSS.
219: * <p>
220: * @return The NNTP query formatted time.
221: ***/
222: public String getTime() {
223: return __time;
224: }
225:
226: /***
227: * Return whether or not the query date should be treated as GMT.
228: * <p>
229: * @return True if the query date is to be treated as GMT, false if not.
230: ***/
231: public boolean isGMT() {
232: return __isGMT;
233: }
234:
235: /***
236: * Return the comma separated list of distributions. This may be null
237: * if there are no distributions.
238: * <p>
239: * @return The list of distributions, which may be null if no distributions
240: * have been specified.
241: ***/
242: public String getDistributions() {
243: return (__distributions == null ? null : __distributions
244: .toString());
245: }
246:
247: /***
248: * Return the comma separated list of newsgroups. This may be null
249: * if there are no newsgroups
250: * <p>
251: * @return The list of newsgroups, which may be null if no newsgroups
252: * have been specified.
253: ***/
254: public String getNewsgroups() {
255: return (__newsgroups == null ? null : __newsgroups.toString());
256: }
257: }
|