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