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.samples;
18  
19  import com.sun.syndication.feed.synd.SyndFeed;
20  import com.sun.syndication.feed.synd.SyndFeedImpl;
21  import com.sun.syndication.io.SyndFeedInput;
22  import com.sun.syndication.io.SyndFeedOutput;
23  import com.sun.syndication.io.XmlReader;
24  
25  import java.io.PrintWriter;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /***
31   * It aggregates a list of RSS/Atom feeds (they can be of different types)
32   * into a single feed of the specified type.
33   * <p>
34   * @author Alejandro Abdelnur
35   *
36   */
37  public class FeedAggregator {
38  
39      public static void main(String[] args) {
40          boolean ok = false;
41          if (args.length>=2) {
42              try {
43                  String outputType = args[0];
44  
45                  SyndFeed aggrFeed = new SyndFeedImpl();
46                  aggrFeed.setFeedType(outputType);
47  
48                  aggrFeed.setTitle("Aggregated Feed");
49                  aggrFeed.setDescription("Anonymous Aggregated Feed");
50                  aggrFeed.setAuthor("anonymous");
51                  aggrFeed.setLink("http://www.anonymous.com");
52  
53                  List entries = new ArrayList();
54                  aggrFeed.setEntries(entries);
55  
56                  for (int i=1;i<args.length;i++) {
57                      URL feedUrl = new URL(args[i]);
58                      SyndFeedInput input = new SyndFeedInput();
59  
60                      SyndFeed feed = input.build(new XmlReader(feedUrl));
61  
62                      entries.addAll(feed.getEntries());
63                  }
64  
65                  SyndFeedOutput output = new SyndFeedOutput();
66                  output.output(aggrFeed,new PrintWriter(System.out));
67  
68                  ok = true;
69              }
70              catch (Exception ex) {
71                  System.out.println("ERROR: "+ex.getMessage());
72              }
73          }
74  
75          if (!ok) {
76              System.out.println();
77              System.out.println("FeedAggregator aggregates different feeds into a single one.");
78              System.out.println("The first parameter must be the feed type for the aggregated feed.");
79              System.out.println(" [valid values are: rss_0.9, rss_0.91, rss_0.92, rss_0.93, ]");
80              System.out.println(" [                  rss_0.94, rss_1.0, rss_2.0 & atom_0.3  ]");
81              System.out.println("The second to last parameters are the URLs of feeds to aggregate.");
82              System.out.println();
83          }
84      }
85  
86  }