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