Staxで今度こそRSSフィードを纏め上げる(後は文字コード問題)

このエントリをはてなブックマークに追加このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをPOOKMARK. Airlinesに追加このエントリをBuzzurl(バザール)に追加このエントリをBuzzurl(バザール)に追加このエントリをChoixに追加このエントリをnewsingに追加このエントリをkwoutに追加
2009年5月6日 水曜日1:14:32

FeedManager

package org.tarotaro.java.wicket.feed;

import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.fetcher.FeedFetcher;
import com.sun.syndication.fetcher.FetcherException;
import com.sun.syndication.fetcher.impl.HttpClientFeedFetcher;
import com.sun.syndication.io.FeedException;

/**
 * フィードを管理するクラス
 * @author tarotarorg
 *
 */

public class FeedManager implements Serializable{
    private static final long serialVersionUID = 1735837604690452296L;

    private Set<URL> urlSet;
    private Set<URL> erroredUrlSet;
    private SortedSet<SyndEntry> entrySet;
    private FeedFetcher fetcher;
   
    public FeedManager() {
        this.urlSet = new LinkedHashSet<URL>();
        this.erroredUrlSet = new LinkedHashSet<URL>();
        this.entrySet = Collections.synchronizedSortedSet(
                new TreeSet<SyndEntry>(new Comparator<SyndEntry>(){
                    @Override
                    public int compare(SyndEntry o1, SyndEntry o2) {
                        return o2.getPublishedDate().compareTo(o1.getPublishedDate());
                    }
                }
            )
        );
    }
   
    /**
     * フィードのURLを追加する
     * @param url
     */

    public synchronized FeedManager addFeed(URL url) {
        this.urlSet.add(url);
        return this;
    }
   
    /**
     * フィードを取得して返す
     * @return
     */

    public synchronized SyndFeed getFeed() {
        FeedFetcher fetcher = this.getFetcher();
        this.entrySet.clear();

        for(URL url:this.urlSet) {
            try {
                SyndFeed aFeed = fetcher.retrieveFeed(url);
                this.entrySet.addAll(aFeed.getEntries());
            } catch (IllegalArgumentException e) {
                this.erroredUrlSet.add(url);
                this.urlSet.remove(url);
            } catch (IOException e) {
                this.erroredUrlSet.add(url);
                this.urlSet.remove(url);
            } catch (FeedException e) {
                this.erroredUrlSet.add(url);
                this.urlSet.remove(url);
            } catch (FetcherException e) {
                this.erroredUrlSet.add(url);
                this.urlSet.remove(url);
            }
        }

        SyndFeed feeds = new SyndFeedImpl();
        feeds.setEntries(new LinkedList<SyndFeed>(
            (Collection<? extends SyndFeed>) this.entrySet)
        );
        return feeds;
    }
   
    synchronized FeedFetcher getFetcher() {
        if (this.fetcher == null) {
            this.fetcher = new HttpClientFeedFetcher();
        }
        return this.fetcher;
    }
}

  • ページ:
  • 1
  • 2
  • 3
  • 4

TrackBack URL :

コメントする

HTML convert time: 0.879 sec.