1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class HelloFeedParser { |
38 | |
|
39 | |
public static void main( String[] args ) throws Exception { |
40 | |
|
41 | |
|
42 | 0 | FeedParser parser = FeedParserFactory.newFeedParser(); |
43 | |
|
44 | |
|
45 | 0 | FeedParserListener listener = new DefaultFeedParserListener() { |
46 | |
|
47 | |
public void onChannel( FeedParserState state, |
48 | |
String title, |
49 | |
String link, |
50 | |
String description ) throws FeedParserException { |
51 | |
|
52 | 0 | System.out.println( "Found a new channel: " + title ); |
53 | |
|
54 | 0 | } |
55 | |
|
56 | |
public void onItem( FeedParserState state, |
57 | |
String title, |
58 | |
String link, |
59 | |
String description, |
60 | |
String permalink ) throws FeedParserException { |
61 | |
|
62 | 0 | System.out.println( "Found a new published article: " + permalink ); |
63 | |
|
64 | 0 | } |
65 | |
|
66 | |
public void onCreated( FeedParserState state, Date date ) throws FeedParserException { |
67 | 0 | System.out.println( "Which was created on: " + date ); |
68 | 0 | } |
69 | |
|
70 | |
}; |
71 | |
|
72 | |
|
73 | |
|
74 | 0 | String resource = "http://peerfear.org/rss/index.rss"; |
75 | |
|
76 | 0 | if ( args.length == 1 ) |
77 | 0 | resource = args[0]; |
78 | |
|
79 | 0 | System.out.println( "Fetching resource:" + resource ); |
80 | |
|
81 | |
|
82 | 0 | ResourceRequest request = ResourceRequestFactory.getResourceRequest( resource ); |
83 | |
|
84 | |
|
85 | 0 | InputStream is = request.getInputStream(); |
86 | |
|
87 | |
|
88 | 0 | parser.parse( listener, is, resource ); |
89 | |
|
90 | 0 | } |
91 | |
|
92 | |
} |
93 | |
|