1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.feedparser; |
18 | |
|
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import org.jaxen.jdom.JDOMXPath; |
23 | |
import org.jdom.Element; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class OPMLFeedParser { |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public static void parse( FeedParserListener listener, |
39 | |
org.jdom.Document doc ) throws FeedParserException { |
40 | |
|
41 | |
try { |
42 | |
|
43 | 0 | FeedParserState state = new FeedParserState(); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | FeedVersion v = new FeedVersion(); |
49 | 0 | v.isOPML = true; |
50 | 0 | listener.onFeedVersion( v ); |
51 | |
|
52 | 0 | listener.init(); |
53 | |
|
54 | 0 | FeedDirectoryParserListener fdpl = (FeedDirectoryParserListener)listener; |
55 | |
|
56 | |
|
57 | 0 | JDOMXPath xpath = new JDOMXPath( "/opml/body/outline" ); |
58 | 0 | List list = xpath.selectNodes( doc ); |
59 | |
|
60 | 0 | Iterator i = list.iterator(); |
61 | 0 | while ( i.hasNext() ) { |
62 | |
|
63 | 0 | Element child = (Element)i.next(); |
64 | 0 | onOutline( fdpl, state, child ); |
65 | |
|
66 | 0 | } |
67 | |
|
68 | 0 | listener.finished(); |
69 | |
|
70 | 0 | } catch ( Throwable t ) { throw new FeedParserException( t ); } |
71 | |
|
72 | 0 | } |
73 | |
|
74 | |
private static void onOutlineFolder( FeedDirectoryParserListener listener, |
75 | |
FeedParserState state, |
76 | |
Element current ) throws Exception { |
77 | |
|
78 | 0 | JDOMXPath xpath = new JDOMXPath( "outline" ); |
79 | 0 | List list = xpath.selectNodes( current ); |
80 | |
|
81 | 0 | Iterator i = list.iterator(); |
82 | 0 | while ( i.hasNext() ) { |
83 | |
|
84 | 0 | Element child = (Element)i.next(); |
85 | 0 | onOutline( listener, state, child ); |
86 | |
|
87 | 0 | } |
88 | |
|
89 | 0 | } |
90 | |
|
91 | |
private static void onOutline( FeedDirectoryParserListener listener, |
92 | |
FeedParserState state, |
93 | |
Element current ) throws Exception { |
94 | |
|
95 | 0 | String title = current.getAttributeValue( "title" ); |
96 | |
|
97 | 0 | if ( title == null ) |
98 | 0 | title = current.getAttributeValue( "text" ); |
99 | |
|
100 | 0 | String weblog = current.getAttributeValue( "htmlUrl" ); |
101 | 0 | String description = current.getAttributeValue( "description" ); |
102 | 0 | String feed = current.getAttributeValue( "xmlUrl" ); |
103 | |
|
104 | 0 | if ( weblog == null ) |
105 | 0 | weblog = feed; |
106 | |
|
107 | 0 | if ( feed != null ) { |
108 | 0 | listener.onItem( state, title, weblog, description, feed ); |
109 | 0 | } else if ( title != null ) { |
110 | |
|
111 | |
|
112 | 0 | listener.onFolder( state, title ); |
113 | 0 | onOutlineFolder( listener, state, current ); |
114 | 0 | listener.onFolderEnd(); |
115 | |
|
116 | |
} |
117 | |
|
118 | 0 | } |
119 | |
|
120 | |
} |
121 | |
|