View Javadoc

1   /*
2    * Copyright 1999,2004 The Apache Software Foundation.
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 org.apache.commons.feedparser.example;
18  
19  import org.apache.commons.feedparser.*;
20  import org.apache.commons.feedparser.impl.*;
21  import org.apache.commons.feedparser.network.*;
22  
23  import java.io.*;
24  import java.net.*;
25  import java.util.*;
26  
27  /***
28   * Example use of the FeedParser
29   *
30   * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
31   * @version $Id: HelloFeedParser.java 155416 2005-02-26 13:00:10Z dirkv $
32   */
33  public class HelloFeedParser {
34  
35      public static void main( String[] args ) throws Exception {
36  
37          //create a new FeedParser...
38          FeedParser parser = FeedParserFactory.newFeedParser();
39  
40          //create a listener for handling our callbacks
41          FeedParserListener listener = new DefaultFeedParserListener() {
42  
43                  public void onChannel( FeedParserState state,
44                                         String title,
45                                         String link,
46                                         String description ) throws FeedParserException {
47  
48                      System.out.println( "Found a new channel: " + title );
49  
50                  }
51  
52                  public void onItem( FeedParserState state,
53                                      String title,
54                                      String link,
55                                      String description,
56                                      String permalink ) throws FeedParserException {
57  
58                      System.out.println( "Found a new published article: " + permalink );
59                      
60                  }
61  
62                  public void onCreated( FeedParserState state, Date date ) throws FeedParserException {
63                      System.out.println( "Which was created on: " + date );
64                  }
65  
66              };
67  
68          //specify the feed we want to fetch
69  
70          String resource = "http://peerfear.org/rss/index.rss";
71  
72          if ( args.length == 1 )
73              resource = args[0];
74  
75          System.out.println( "Fetching resource:" + resource );
76          
77          //use the FeedParser network IO package to fetch our resource URL
78          ResourceRequest request = ResourceRequestFactory.getResourceRequest( resource );
79  
80          //grab our input stream
81          InputStream is = request.getInputStream();
82  
83          //start parsing our feed and have the above onItem methods called
84          parser.parse( listener, is, resource );
85  
86      }
87  
88  }
89