/*******************************************************************************
blogger-cli Консольные инструменты по работе с blogger.com
(с) Камнев Георгий Павлович 2011 GPLv2
Данная программа является свободным программным обеспечением. Вы вправе
распространять ее и/или модифицировать в соответствии с условиями версии 2
либо по вашему выбору с условиями более поздней версии
Стандартной Общественной Лицензии GNU, опубликованной Free Software Foundation.
Мы распространяем данную программу в надежде на то, что она будет вам полезной,
однако НЕ ПРЕДОСТАВЛЯЕМ НА НЕЕ НИКАКИХ ГАРАНТИЙ,
в том числе ГАРАНТИИ ТОВАРНОГО СОСТОЯНИЯ ПРИ ПРОДАЖЕ
и ПРИГОДНОСТИ ДЛЯ ИСПОЛЬЗОВАНИЯ В КОНКРЕТНЫХ ЦЕЛЯХ.
Для получения более подробной информации ознакомьтесь
со Стандартной Общественной Лицензией GNU.
Вместе с данной программой вы должны были получить экземпляр
Стандартной Общественной Лицензии GNU.
Если вы его не получили, сообщите об этом в Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*******************************************************************************/
package tv.cofe.blogger;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.Link;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Итератор по записям
* @author gocha
*/
public class EntryIterable implements Iterable<Entry>
{
public static class FeedItr implements Iterator<Entry>
{
protected Feed feed = null;
protected Iterator<Entry> itr = null;
protected Entry e = null;
public FeedItr(Feed feed){
this.feed = feed;
this.itr = feed.getEntries().iterator();
this.e = _next();
}
@Override
public boolean hasNext() {
return this.e!=null;
}
@Override
public Entry next(){
Entry r = e;
e = _next();
return r;
}
// @Override
protected Entry _next() {
if( itr==null )return null;
if( itr.hasNext() ){
Entry e = itr.next();
return e;
}
while( true ){
if( feed!=null ){
try {
Link nextLink = feed.getNextLink();
if( nextLink==null ){
itr = null;
feed = null;
return null;
}
URL nextUrl = new URL(nextLink.getHref());
Feed nf = feed.getService().getFeed(nextUrl, Feed.class);
if( nf==null ){
itr = null;
feed = null;
return null;
}
feed = nf;
itr = feed.getEntries().iterator();
} catch (IOException ex) {
Logger.getLogger(EntryIterable.class.getName()).log(Level.SEVERE, null, ex);
throw new Error(ex.getMessage(),ex);
} catch (ServiceException ex) {
Logger.getLogger(EntryIterable.class.getName()).log(Level.SEVERE, null, ex);
throw new Error(ex.getMessage(),ex);
}
}
if( itr.hasNext() ){
Entry e = itr.next();
return e;
}
}
}
@Override
public void remove() {
}
}
private Iterator<Entry> itr = null;
public EntryIterable(Feed feed){
if (feed == null) {
throw new IllegalArgumentException("feed==null");
}
itr = new FeedItr(feed);
}
@Override
public Iterator<Entry> iterator() {
return itr;
}
}