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.HashSet; |
20 | |
|
21 | |
import org.apache.commons.feedparser.impl.DebugFeedParserListener; |
22 | |
import org.apache.commons.feedparser.locate.AnchorParser; |
23 | |
import org.apache.commons.feedparser.locate.AnchorParserException; |
24 | |
import org.apache.commons.feedparser.locate.AnchorParserListener; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class HTMLFeedParser extends BaseParser { |
36 | |
|
37 | 0 | public static final HashSet XFN_RELATIONS = new HashSet(); |
38 | |
|
39 | |
public static void parse( String content, final FeedParserListener listener ) throws Exception { |
40 | |
|
41 | 0 | if ( listener instanceof FeedDirectoryParserListener == false ) |
42 | 0 | return; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | final FeedDirectoryParserListener directoryParserLisener = |
49 | |
(FeedDirectoryParserListener)listener; |
50 | |
|
51 | 0 | directoryParserLisener.init(); |
52 | |
|
53 | 0 | final FeedParserState state = new FeedParserState(); |
54 | |
|
55 | 0 | AnchorParserListener alistener = new AnchorParserListener() { |
56 | |
|
57 | 0 | public void setContext( Object context ) {} |
58 | |
|
59 | 0 | public Object getResult() { return null; } |
60 | |
|
61 | |
public boolean onAnchor( String href, String rel, String title ) |
62 | |
throws AnchorParserException { |
63 | |
|
64 | |
try { |
65 | |
|
66 | 0 | if ( rel == null || "".equals( rel ) ) |
67 | 0 | return true; |
68 | |
|
69 | |
|
70 | 0 | String description = null; |
71 | 0 | String feed = null; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | directoryParserLisener.onItem( state, title, href, description, feed ); |
77 | |
|
78 | 0 | String[] rels = rel.split( " " ); |
79 | |
|
80 | 0 | for ( int i = 0; i < rels.length; ++i ) { |
81 | |
|
82 | 0 | String current = rels[i]; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | 0 | boolean isXFriendRel = XFN_RELATIONS.contains( current ); |
89 | |
|
90 | 0 | if ( isXFriendRel ) { |
91 | |
|
92 | 0 | directoryParserLisener.onRelation( state, |
93 | |
current ); |
94 | |
|
95 | 0 | directoryParserLisener.onRelationEnd(); |
96 | |
|
97 | |
} |
98 | |
|
99 | |
} |
100 | |
|
101 | 0 | directoryParserLisener.onItemEnd(); |
102 | |
|
103 | |
|
104 | |
|
105 | 0 | return true; |
106 | |
|
107 | 0 | } catch ( Exception e ) { |
108 | 0 | throw new AnchorParserException( e ); |
109 | |
} |
110 | |
|
111 | |
} |
112 | |
|
113 | |
}; |
114 | |
|
115 | 0 | AnchorParser.parse( content, alistener ); |
116 | |
|
117 | 0 | directoryParserLisener.finished(); |
118 | |
|
119 | 0 | } |
120 | |
|
121 | |
public static void main( String[] args ) throws Exception { |
122 | |
|
123 | 0 | FeedParserListener listener = new DebugFeedParserListener(); |
124 | |
|
125 | 0 | parse( "<a href='http://jane-blog.example.org/' rel='sweetheart date met'>Jane</a> ", |
126 | |
listener ); |
127 | |
|
128 | 0 | } |
129 | |
|
130 | |
static { |
131 | |
|
132 | 0 | XFN_RELATIONS.add( "contact" ); |
133 | 0 | XFN_RELATIONS.add( "acquaintance" ); |
134 | 0 | XFN_RELATIONS.add( "friend" ); |
135 | 0 | XFN_RELATIONS.add( "met" ); |
136 | 0 | XFN_RELATIONS.add( "co-worker" ); |
137 | 0 | XFN_RELATIONS.add( "colleague" ); |
138 | 0 | XFN_RELATIONS.add( "co-resident" ); |
139 | 0 | XFN_RELATIONS.add( "neighbor" ); |
140 | 0 | XFN_RELATIONS.add( "child" ); |
141 | 0 | XFN_RELATIONS.add( "parent" ); |
142 | 0 | XFN_RELATIONS.add( "sibling" ); |
143 | 0 | XFN_RELATIONS.add( "spouse" ); |
144 | 0 | XFN_RELATIONS.add( "kin" ); |
145 | 0 | XFN_RELATIONS.add( "muse" ); |
146 | 0 | XFN_RELATIONS.add( "crush" ); |
147 | 0 | XFN_RELATIONS.add( "date" ); |
148 | 0 | XFN_RELATIONS.add( "sweetheart" ); |
149 | 0 | XFN_RELATIONS.add( "me" ); |
150 | |
|
151 | 0 | } |
152 | |
|
153 | |
} |
154 | |
|