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.Attribute;
24 import org.jdom.Element;
25 import org.jdom.Namespace;
26
27
28
29
30
31
32
33 public class FOAFFeedParser {
34
35
36
37
38
39
40 public static void parse( FeedParserListener listener,
41 org.jdom.Document doc ) throws Exception {
42
43 try {
44
45 FeedParserState state = new FeedParserState();
46
47 FeedVersion v = new FeedVersion();
48 v.isFOAF = true;
49 listener.onFeedVersion( v );
50
51 listener.init();
52
53 FOAFFeedParserListener flistener = null;
54
55 if ( listener instanceof FOAFFeedParserListener )
56 flistener = (FOAFFeedParserListener)listener;
57
58
59 JDOMXPath xpath = new JDOMXPath( "/rdf:RDF/foaf:Person" );
60 xpath.setNamespaceContext( NS.context );
61
62 Element person = (Element)xpath.selectSingleNode( doc );
63
64 String name = RSSFeedParser.getChildElementTextByName( state, "name" );
65
66 String homepage = getAttributeValue( state,
67 "homepage", NS.FOAF,
68 "resource", NS.RDF );
69
70 if ( flistener != null )
71 flistener.onPerson( state, name );
72
73 xpath = new JDOMXPath( "foaf:knows" );
74
75 xpath.setNamespaceContext( NS.context );
76
77 List list = xpath.selectNodes( person );
78 Iterator i = list.iterator();
79
80 while ( i.hasNext() ) {
81
82 Element knows = (Element)i.next();
83
84 Element currentPerson = knows.getChild( "Person", NS.FOAF );
85
86 state.current = currentPerson;
87
88 String title = RSSFeedParser.getChildElementTextByName( state, "name" );
89
90 String seeAlso = getAttributeValue( state,
91 "seeAlso", NS.RDFS,
92 "resource", NS.RDF );
93
94 homepage = getAttributeValue( state,
95 "homepage", NS.FOAF,
96 "resource", NS.RDF );
97
98 System.out.println( "seeAlso: " + seeAlso );
99 System.out.println( "homepage: " + homepage );
100
101 if ( flistener != null )
102 flistener.onKnows( state );
103
104 listener.onItem( state,
105 title,
106 homepage,
107 null,
108 seeAlso );
109
110 if ( flistener != null ) {
111
112 flistener.onHomepage( state,
113 homepage );
114
115 flistener.onHomepageEnd();
116
117 flistener.onSeeAlso( state,
118 seeAlso );
119
120 flistener.onSeeAlsoEnd();
121
122 }
123
124 listener.onItemEnd();
125
126 if ( flistener != null )
127 flistener.onKnowsEnd();
128
129 }
130
131 if ( flistener != null )
132 flistener.onPersonEnd();
133
134 listener.finished();
135
136 } catch ( Throwable t ) { throw new FeedParserException( t ); }
137
138 }
139
140 public static String getAttributeValue( FeedParserState state,
141 String name,
142 Namespace name_ns,
143 String attr,
144 Namespace attr_ns ) {
145
146 Element e = state.current.getChild( name, name_ns );
147
148 if ( e == null )
149 return null;
150
151 Attribute a = e.getAttribute( attr, attr_ns );
152
153 if ( a == null )
154 return null;
155
156 return a.getValue();
157
158 }
159
160 private static void doParseKnows( FeedParserListener listener,
161 FeedParserState state ) throws Exception {
162
163 }
164
165 }
166