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;
018
019 import java.util.Iterator;
020 import java.util.List;
021
022 import org.jaxen.jdom.JDOMXPath;
023 import org.jdom.Attribute;
024 import org.jdom.Element;
025 import org.jdom.Namespace;
026
027 /**
028 * Handles parsing FOAF.
029 *
030 * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a>
031 * @version $Id: FOAFFeedParser.java 373614 2006-01-30 22:31:21Z mvdb $
032 */
033 public class FOAFFeedParser {
034
035 /**
036 * Parse this feed.
037 *
038 *
039 */
040 public static void parse( FeedParserListener listener,
041 org.jdom.Document doc ) throws Exception {
042
043 try {
044
045 FeedParserState state = new FeedParserState();
046
047 FeedVersion v = new FeedVersion();
048 v.isFOAF = true;
049 listener.onFeedVersion( v );
050
051 listener.init();
052
053 FOAFFeedParserListener flistener = null;
054
055 if ( listener instanceof FOAFFeedParserListener )
056 flistener = (FOAFFeedParserListener)listener;
057
058 //this should be the root directory.
059 JDOMXPath xpath = new JDOMXPath( "/rdf:RDF/foaf:Person" );
060 xpath.setNamespaceContext( NS.context );
061
062 Element person = (Element)xpath.selectSingleNode( doc );
063
064 String name = RSSFeedParser.getChildElementTextByName( state, "name" );
065
066 String homepage = getAttributeValue( state,
067 "homepage", NS.FOAF,
068 "resource", NS.RDF );
069
070 if ( flistener != null )
071 flistener.onPerson( state, name );
072
073 xpath = new JDOMXPath( "foaf:knows" );
074
075 xpath.setNamespaceContext( NS.context );
076
077 List list = xpath.selectNodes( person );
078 Iterator i = list.iterator();
079
080 while ( i.hasNext() ) {
081
082 Element knows = (Element)i.next();
083
084 Element currentPerson = knows.getChild( "Person", NS.FOAF );
085
086 state.current = currentPerson;
087
088 String title = RSSFeedParser.getChildElementTextByName( state, "name" );
089
090 String seeAlso = getAttributeValue( state,
091 "seeAlso", NS.RDFS,
092 "resource", NS.RDF );
093
094 homepage = getAttributeValue( state,
095 "homepage", NS.FOAF,
096 "resource", NS.RDF );
097
098 System.out.println( "seeAlso: " + seeAlso );
099 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