View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package com.sun.syndication.fetcher.samples;
18  
19  import java.io.PrintWriter;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import com.sun.syndication.feed.synd.SyndFeedImpl;
25  import com.sun.syndication.feed.synd.SyndFeed;
26  import com.sun.syndication.fetcher.FeedFetcher;
27  import com.sun.syndication.fetcher.impl.FeedFetcherCache;
28  import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache;
29  import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher;
30  import com.sun.syndication.io.SyndFeedOutput;
31  
32  /***
33   * <p>It aggregates a list of RSS/Atom feeds (they can be of different types)
34   * into a single feed of the specified type.</p>
35   *
36   * <p>Converted from the original FeedAggregator sample</p>
37   *
38   * @author Alejandro Abdelnur
39   * @author Nick Lothian
40   *
41   */
42  public class FeedAggregator {
43  
44      public static void main(String[] args) {
45          boolean ok = false;
46          if (args.length>=2) {
47              try {
48                  String outputType = args[0];
49  
50                  SyndFeed feed = new SyndFeedImpl();
51                  feed.setFeedType(outputType);
52  
53                  feed.setTitle("Aggregated Feed");
54                  feed.setDescription("Anonymous Aggregated Feed");
55                  feed.setAuthor("anonymous");
56                  feed.setLink("http://www.anonymous.com");
57  
58                  List entries = new ArrayList();
59                  feed.setEntries(entries);
60  
61  				FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();
62  				FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
63  
64                  for (int i=1;i<args.length;i++) {
65                      URL inputUrl = new URL(args[i]);
66  					SyndFeed inFeed = feedFetcher.retrieveFeed(inputUrl);
67                      entries.addAll(inFeed.getEntries());
68                  }
69  
70                  SyndFeedOutput output = new SyndFeedOutput();
71  				output.output(feed, new PrintWriter(System.out));
72  
73                  ok = true;
74              }
75              catch (Exception ex) {
76                  System.out.println("ERROR: "+ex.getMessage());
77                  ex.printStackTrace();
78              }
79          }
80  
81          if (!ok) {
82              System.out.println();
83              System.out.println("FeedAggregator aggregates different feeds into a single one.");
84              System.out.println("The first parameter must be the feed type for the aggregated feed.");
85              System.out.println(" [valid values are: rss_0.9, rss_0.91, rss_0.92, rss_0.93, ]");
86              System.out.println(" [                  rss_0.94, rss_1.0, rss_2.0 & atom_0.3  ]");
87              System.out.println("The second to last parameters are the URLs of feeds to aggregate.");
88              System.out.println();
89          }
90      }
91  
92  }