Author: sebb
Date: Sat Mar 12 00:51:24 2011
New Revision: 1080827
URL: http://svn.apache.org/viewvc?rev=1080827&view=rev
Log:
Add iterateArticleInfo
Added:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ArticleIterator.java
(with props)
Modified:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java
Added:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ArticleIterator.java
URL:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ArticleIterator.java?rev=1080827&view=auto
==============================================================================
---
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ArticleIterator.java
(added)
+++
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ArticleIterator.java
Sat Mar 12 00:51:24 2011
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.commons.net.nntp;
+
+import java.util.Iterator;
+/**
+ * Class which wraps an {@code Iterable<String>} of raw article information
+ * to generate an {@code Iterable<Article>} of the parsed information.
+ * @since 3.0
+ */
+class ArticleIterator implements Iterator<Article>, Iterable<Article> {
+
+ private final Iterator<String> stringIterator;
+
+ public ArticleIterator(Iterable<String> iterableString) {
+ stringIterator = iterableString.iterator();
+ }
+
+ public boolean hasNext() {
+ return stringIterator.hasNext();
+ }
+
+ public Article next() {
+ String line = stringIterator.next();
+ return NNTPClient.__parseArticleEntry(line);
+ }
+
+ public void remove() {
+ stringIterator.remove();
+ }
+ public Iterator<Article> iterator() {
+ return this;
+ }
+}
Propchange:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ArticleIterator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/ArticleIterator.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified:
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java
URL:
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java?rev=1080827&r1=1080826&r2=1080827&view=diff
==============================================================================
---
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java
(original)
+++
commons/proper/net/trunk/src/main/java/org/apache/commons/net/nntp/NNTPClient.java
Sat Mar 12 00:51:24 2011
@@ -208,6 +208,33 @@ public class NNTPClient extends NNTP
return result;
}
+ /**
+ * Parse a response line from {@link #retrieveArticleInfo(long, long)}
+ * @param line a response line
+ * @return the parsed {@link Article} or {@code null} if it cannot be
parsed
+ * @since 3.0
+ */
+ static Article __parseArticleEntry(String line) {
+ // Extract the article information
+ // Mandatory format (from NNTP RFC 2980) is :
+ // articleNumber\tSubject\tAuthor\tDate\tID\tReference(s)\tByte
Count\tLine Count
+
+ String parts[] = line.split("\t");
+ if (parts.length > 6) {
+ int i = 0;
+ Article article = new Article();
+ article.setArticleNumber(Integer.parseInt(parts[i++]));
+ article.setSubject(parts[i++]);
+ article.setFrom(parts[i++]);
+ article.setDate(parts[i++]);
+ article.setArticleId(parts[i++]);
+ article.addReference(parts[i++]);
+ return article;
+ } else {
+ return null;
+ }
+ }
+
private NewsgroupInfo[] __readNewsgroupListing() throws IOException
{
int size;
@@ -1381,6 +1408,27 @@ public class NNTPClient extends NNTP
highArticleNumber);
}
+ /**
+ * Return article headers for all articles between lowArticleNumber
+ * and highArticleNumber, inclusively.
+ * <p>
+ * @param lowArticleNumber
+ * @param highArticleNumber
+ * @return an Iterable of Articles, or {@code null} if the command failed
+ * @throws IOException
+ * @since 3.0
+ */
+ public Iterable<Article> iterateArticleInfo(long lowArticleNumber, long
highArticleNumber)
+ throws IOException
+ {
+ Reader info = retrieveArticleInfo(lowArticleNumber,highArticleNumber);
+ if (info == null) {
+ return null;
+ }
+ // N.B. info is already DotTerminated, so don't rewrap
+ return new ArticleIterator(new ReplyIterator(info, false));
+ }
+
/***
* Private implementation of XHDR functionality.
*