Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TagFeedParser |
|
| 3.75;3.75 |
1 | /* | |
2 | * Copyright 1999,2004 The Apache Software Foundation. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
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 | * Handles parsing RSS metadata including dates | |
27 | * | |
28 | * @author <a href="mailto:burton@apache.org">Kevin A. Burton (burtonator)</a> | |
29 | * @version $Id: TagFeedParser.java 373614 2006-01-30 22:31:21Z mvdb $ | |
30 | */ | |
31 | 0 | public class TagFeedParser { |
32 | ||
33 | /** | |
34 | * | |
35 | */ | |
36 | public static void parse( FeedParserListener listener, | |
37 | FeedParserState state ) throws Exception { | |
38 | ||
39 | 0 | if ( listener instanceof TagFeedParserListener == false ) |
40 | 0 | return; |
41 | ||
42 | 0 | TagFeedParserListener tagFeedParserListener |
43 | = (TagFeedParserListener)listener; | |
44 | ||
45 | 0 | if ( doParseModTaxonomy( tagFeedParserListener, state ) ) |
46 | 0 | return; |
47 | ||
48 | 0 | if ( doParseCategory( tagFeedParserListener, state ) ) |
49 | 0 | return; |
50 | ||
51 | 0 | doDcSubject( tagFeedParserListener, state ); |
52 | ||
53 | 0 | } |
54 | ||
55 | /** | |
56 | * | |
57 | * Parse out dc:subject tags | |
58 | * | |
59 | * Its not very clear how this is supposed to work. | |
60 | * | |
61 | * "Comment: Typically, a Subject will be expressed as keywords, key phrases | |
62 | * or classification codes that describe a topic of the resource. | |
63 | * Recommended best practice is to select a value from a controlled | |
64 | * vocabulary or formal classification scheme." | |
65 | * | |
66 | * But this leaves it open to whether its a space separated list or that | |
67 | * this needs to be per element. It seems that the real infringer is | |
68 | * Delicious but I can fix this by processing dc:subject last. | |
69 | * | |
70 | * | |
71 | */ | |
72 | public static void doDcSubject( TagFeedParserListener listener, | |
73 | FeedParserState state ) throws Exception { | |
74 | ||
75 | 0 | JDOMXPath xpath = new JDOMXPath( "dc:subject" ); |
76 | 0 | xpath.addNamespace( NS.DC.getPrefix(), NS.DC.getURI() ); |
77 | ||
78 | 0 | List list = xpath.selectNodes( state.current ); |
79 | ||
80 | 0 | Iterator it = list.iterator(); |
81 | ||
82 | 0 | while ( it.hasNext() ) { |
83 | ||
84 | 0 | Element element = (Element)it.next(); |
85 | ||
86 | 0 | String tag = element.getText(); |
87 | 0 | String tagspace = null; |
88 | ||
89 | 0 | listener.onTag( state, tag, tagspace ); |
90 | 0 | listener.onTagEnd(); |
91 | ||
92 | 0 | } |
93 | ||
94 | 0 | } |
95 | ||
96 | /** | |
97 | * Parse out atom:category and RSS 2.0/0.91 category | |
98 | * | |
99 | * | |
100 | */ | |
101 | public static boolean doParseCategory( TagFeedParserListener listener, | |
102 | FeedParserState state ) throws Exception { | |
103 | ||
104 | //XPath xpath = new XPath( "local-name() = 'category'" ); | |
105 | ||
106 | 0 | JDOMXPath xpath = new JDOMXPath( "descendant::*[local-name() = 'category']" ); |
107 | ||
108 | //NOTE: this only works for elements without namespaces | |
109 | //XPath xpath = new XPath( "category" ); | |
110 | ||
111 | 0 | List list = xpath.selectNodes( state.current ); |
112 | ||
113 | 0 | Iterator it = list.iterator(); |
114 | ||
115 | 0 | boolean found = false; |
116 | ||
117 | 0 | while ( it.hasNext() ) { |
118 | ||
119 | 0 | Element element = (Element)it.next(); |
120 | ||
121 | 0 | String tag = element.getText(); |
122 | 0 | String tagspace = null; |
123 | ||
124 | 0 | listener.onTag( state, tag, tagspace ); |
125 | 0 | listener.onTagEnd(); |
126 | ||
127 | 0 | found = true; |
128 | ||
129 | 0 | } |
130 | ||
131 | 0 | return found; |
132 | ||
133 | } | |
134 | ||
135 | public static boolean doParseModTaxonomy( TagFeedParserListener listener, | |
136 | FeedParserState state ) throws Exception { | |
137 | ||
138 | // <taxo:topics> | |
139 | // <rdf:Bag> | |
140 | // <rdf:li resource="http://del.icio.us/tag/hacking" /> | |
141 | // <rdf:li resource="http://del.icio.us/tag/howto" /> | |
142 | // <rdf:li resource="http://del.icio.us/tag/programming" /> | |
143 | // <rdf:li resource="http://del.icio.us/tag/software" /> | |
144 | // <rdf:li resource="http://del.icio.us/tag/tech" /> | |
145 | // <rdf:li resource="http://del.icio.us/tag/technology" /> | |
146 | // <rdf:li resource="http://del.icio.us/tag/tools" /> | |
147 | // <rdf:li resource="http://del.icio.us/tag/tivo" /> | |
148 | // </rdf:Bag> | |
149 | // </taxo:topics> | |
150 | ||
151 | 0 | JDOMXPath xpath = new JDOMXPath( "taxo:topics/rdf:Bag/rdf:li" ); |
152 | 0 | xpath.addNamespace( NS.RDF.getPrefix(), NS.RDF.getURI() ); |
153 | 0 | xpath.addNamespace( NS.TAXO.getPrefix(), NS.TAXO.getURI() ); |
154 | ||
155 | 0 | List list = xpath.selectNodes( state.current ); |
156 | ||
157 | 0 | Iterator it = list.iterator(); |
158 | ||
159 | 0 | boolean found = false; |
160 | ||
161 | 0 | while ( it.hasNext() ) { |
162 | ||
163 | 0 | Element element = (Element)it.next(); |
164 | ||
165 | 0 | String resource = element.getAttributeValue( "resource" ); |
166 | ||
167 | 0 | if ( resource != "" && resource != null ) { |
168 | ||
169 | 0 | String tag = resource; |
170 | 0 | String tagspace = resource; |
171 | ||
172 | 0 | int begin = resource.lastIndexOf( "/" ); |
173 | ||
174 | 0 | if ( begin != -1 ) { |
175 | 0 | ++begin; |
176 | 0 | tag = resource.substring( begin, resource.length() ); |
177 | } | |
178 | ||
179 | 0 | listener.onTag( state, tag, tagspace ); |
180 | 0 | listener.onTagEnd(); |
181 | ||
182 | 0 | found = true; |
183 | } | |
184 | ||
185 | 0 | } |
186 | ||
187 | 0 | return found; |
188 | ||
189 | } | |
190 | ||
191 | } |