001 /*
002 * Copyright 1999,2004 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.apache.commons.feedparser.example;
018
019 import java.io.InputStream;
020 import java.util.Date;
021
022 import org.apache.commons.feedparser.DefaultFeedParserListener;
023 import org.apache.commons.feedparser.FeedParser;
024 import org.apache.commons.feedparser.FeedParserException;
025 import org.apache.commons.feedparser.FeedParserFactory;
026 import org.apache.commons.feedparser.FeedParserListener;
027 import org.apache.commons.feedparser.FeedParserState;
028 import org.apache.commons.feedparser.network.ResourceRequest;
029 import org.apache.commons.feedparser.network.ResourceRequestFactory;
030
031 /**
032 * Example use of the FeedParser
033 *
034 * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
035 * @version $Id: HelloFeedParser.java 373622 2006-01-30 22:53:00Z mvdb $
036 */
037 public class HelloFeedParser {
038
039 public static void main( String[] args ) throws Exception {
040
041 //create a new FeedParser...
042 FeedParser parser = FeedParserFactory.newFeedParser();
043
044 //create a listener for handling our callbacks
045 FeedParserListener listener = new DefaultFeedParserListener() {
046
047 public void onChannel( FeedParserState state,
048 String title,
049 String link,
050 String description ) throws FeedParserException {
051
052 System.out.println( "Found a new channel: " + title );
053
054 }
055
056 public void onItem( FeedParserState state,
057 String title,
058 String link,
059 String description,
060 String permalink ) throws FeedParserException {
061
062 System.out.println( "Found a new published article: " + permalink );
063
064 }
065
066 public void onCreated( FeedParserState state, Date date ) throws FeedParserException {
067 System.out.println( "Which was created on: " + date );
068 }
069
070 };
071
072 //specify the feed we want to fetch
073
074 String resource = "http://peerfear.org/rss/index.rss";
075
076 if ( args.length == 1 )
077 resource = args[0];
078
079 System.out.println( "Fetching resource:" + resource );
080
081 //use the FeedParser network IO package to fetch our resource URL
082 ResourceRequest request = ResourceRequestFactory.getResourceRequest( resource );
083
084 //grab our input stream
085 InputStream is = request.getInputStream();
086
087 //start parsing our feed and have the above onItem methods called
088 parser.parse( listener, is, resource );
089
090 }
091
092 }
093