1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.io.impl;
18
19 import com.sun.syndication.feed.module.DCModuleImpl;
20 import com.sun.syndication.feed.module.DCSubjectImpl;
21 import com.sun.syndication.feed.module.Module;
22 import com.sun.syndication.feed.module.DCModule;
23 import com.sun.syndication.feed.module.DCSubject;
24 import com.sun.syndication.io.ModuleParser;
25 import com.sun.syndication.io.WireFeedParser;
26 import org.jdom.Attribute;
27 import org.jdom.Element;
28 import org.jdom.Namespace;
29
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33
34 /***
35 * Parser for the Dublin Core module.
36 */
37 public class DCModuleParser implements ModuleParser {
38
39 private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
40 private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
41
42 private static final Namespace DC_NS = Namespace.getNamespace(DCModule.URI);
43 private static final Namespace RDF_NS = Namespace.getNamespace(RDF_URI);
44 private static final Namespace TAXO_NS = Namespace.getNamespace(TAXO_URI);
45
46 public final String getNamespaceUri() {
47 return DCModule.URI;
48 }
49
50 private final Namespace getDCNamespace() {
51 return DC_NS;
52 }
53
54 private final Namespace getRDFNamespace() {
55 return RDF_NS;
56 }
57
58 private final Namespace getTaxonomyNamespace() {
59 return TAXO_NS;
60 }
61
62 /***
63 * Parse an element tree and return the module found in it.
64 * <p>
65 * @param dcRoot the root element containing the module elements.
66 * @return the module parsed from the element tree, <i>null</i> if none.
67 */
68 public Module parse(Element dcRoot) {
69 boolean foundSomething = false;
70 DCModule dcm = new DCModuleImpl();
71
72 List eList = dcRoot.getChildren("title", getDCNamespace());
73 if (eList.size() > 0) {
74 foundSomething = true;
75 dcm.setTitles(parseElementList(eList));
76 }
77 eList = dcRoot.getChildren("creator", getDCNamespace());
78 if (eList.size() > 0) {
79 foundSomething = true;
80 dcm.setCreators(parseElementList(eList));
81 }
82 eList = dcRoot.getChildren("subject", getDCNamespace());
83 if (eList.size() > 0) {
84 foundSomething = true;
85 dcm.setSubjects(parseSubjects(eList));
86 }
87 eList = dcRoot.getChildren("description", getDCNamespace());
88 if (eList.size() > 0) {
89 foundSomething = true;
90 dcm.setDescriptions(parseElementList(eList));
91 }
92 eList = dcRoot.getChildren("publisher", getDCNamespace());
93 if (eList.size() > 0) {
94 foundSomething = true;
95 dcm.setPublishers(parseElementList(eList));
96 }
97 eList = dcRoot.getChildren("contributor", getDCNamespace());
98 if (eList.size() > 0) {
99 foundSomething = true;
100 dcm.setContributors(parseElementList(eList));
101 }
102 eList = dcRoot.getChildren("date", getDCNamespace());
103 if (eList.size() > 0) {
104 foundSomething = true;
105 dcm.setDates(parseElementListDate(eList));
106 }
107 eList = dcRoot.getChildren("type", getDCNamespace());
108 if (eList.size() > 0) {
109 foundSomething = true;
110 dcm.setTypes(parseElementList(eList));
111 }
112 eList = dcRoot.getChildren("format", getDCNamespace());
113 if (eList.size() > 0) {
114 foundSomething = true;
115 dcm.setFormats(parseElementList(eList));
116 }
117 eList = dcRoot.getChildren("identifier", getDCNamespace());
118 if (eList.size() > 0) {
119 foundSomething = true;
120 dcm.setIdentifiers(parseElementList(eList));
121 }
122 eList = dcRoot.getChildren("source", getDCNamespace());
123 if (eList.size() > 0) {
124 foundSomething = true;
125 dcm.setSources(parseElementList(eList));
126 }
127 eList = dcRoot.getChildren("language", getDCNamespace());
128 if (eList.size() > 0) {
129 foundSomething = true;
130 dcm.setLanguages(parseElementList(eList));
131 }
132 eList = dcRoot.getChildren("relation", getDCNamespace());
133 if (eList.size() > 0) {
134 foundSomething = true;
135 dcm.setRelations(parseElementList(eList));
136 }
137 eList = dcRoot.getChildren("coverage", getDCNamespace());
138 if (eList.size() > 0) {
139 foundSomething = true;
140 dcm.setCoverages(parseElementList(eList));
141 }
142 eList = dcRoot.getChildren("rights", getDCNamespace());
143 if (eList.size() > 0) {
144 foundSomething = true;
145 dcm.setRightsList(parseElementList(eList));
146 }
147
148 return (foundSomething) ? dcm : null;
149 }
150
151 /***
152 * Utility method to parse a taxonomy from an element.
153 * <p>
154 * @param desc the taxonomy description element.
155 * @return the string contained in the resource of the element.
156 */
157 protected final String getTaxonomy(Element desc) {
158 String d = null;
159 Element taxo = desc.getChild("topic", getTaxonomyNamespace());
160 if (taxo!=null) {
161 Attribute a = taxo.getAttribute("resource", getRDFNamespace());
162 if (a!=null) {
163 d = a.getValue();
164 }
165 }
166 return d;
167 }
168
169 /***
170 * Utility method to parse a list of subjects out of a list of elements.
171 * <p>
172 * @param eList the element list to parse.
173 * @return a list of subjects parsed from the elements.
174 */
175 protected final List parseSubjects(List eList) {
176 List subjects = new ArrayList();
177 for (Iterator i = eList.iterator(); i.hasNext();) {
178 Element eSubject = (Element) i.next();
179 Element eDesc = eSubject.getChild("Description", getRDFNamespace());
180 if (eDesc != null) {
181 String taxonomy = getTaxonomy(eDesc);
182 List eValues = eDesc.getChildren("value", getRDFNamespace());
183 for (Iterator v = eValues.iterator(); v.hasNext();) {
184 Element eValue = (Element) v.next();
185 DCSubject subject = new DCSubjectImpl();
186 subject.setTaxonomyUri(taxonomy);
187 subject.setValue(eValue.getText());
188 subjects.add(subject);
189 }
190 } else {
191 DCSubject subject = new DCSubjectImpl();
192 subject.setValue(eSubject.getText());
193 subjects.add(subject);
194 }
195 }
196
197 return subjects;
198 }
199
200 /***
201 * Utility method to parse a list of strings out of a list of elements.
202 * <p>
203 * @param eList the list of elements to parse.
204 * @return the list of strings
205 */
206 protected final List parseElementList(List eList) {
207 List values= new ArrayList();
208 for (Iterator i = eList.iterator(); i.hasNext();) {
209 Element e = (Element) i.next();
210 values.add(e.getText());
211 }
212
213 return values;
214 }
215
216 /***
217 * Utility method to parse a list of dates out of a list of elements.
218 * <p>
219 * @param eList the list of elements to parse.
220 * @return the list of dates.
221 */
222 protected final List parseElementListDate(List eList) {
223 List values = new ArrayList();
224 for (Iterator i = eList.iterator(); i.hasNext();) {
225 Element e = (Element) i.next();
226 values.add(DateParser.parseDate(e.getText()));
227 }
228
229 return values;
230 }
231 }