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