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.List;
31
32 public class DCModuleParser implements ModuleParser {
33
34 public String getNamespaceUri() {
35 return DCModule.URI;
36 }
37
38 private Namespace getDCNamespace() {
39 return Namespace.getNamespace(DCModule.URI);
40 }
41
42 public Module parse(Element dcRoot) {
43 boolean foundSomething = false;
44 DCModule dcm = new DCModuleImpl();
45
46 Element e = dcRoot.getChild("title",getDCNamespace());
47 if (e!=null) {
48 foundSomething = true;
49 dcm.setTitle(e.getText());
50 }
51 e = dcRoot.getChild("creator",getDCNamespace());
52 if (e!=null) {
53 foundSomething = true;
54 dcm.setCreator(e.getText());
55 }
56 List eList = dcRoot.getChildren("subject",getDCNamespace());
57 if (eList.size()>0) {
58 foundSomething = true;
59 dcm.setSubjects(parseSubjects(eList));
60 }
61 e = dcRoot.getChild("description",getDCNamespace());
62 if (e!=null) {
63 foundSomething = true;
64 dcm.setDescription(e.getText());
65 }
66 e = dcRoot.getChild("publisher",getDCNamespace());
67 if (e!=null) {
68 foundSomething = true;
69 dcm.setPublisher(e.getText());
70 }
71 eList = dcRoot.getChildren("contributor",getDCNamespace());
72 if (eList.size()>0) {
73 foundSomething = true;
74 dcm.setContributors(parseContributors(eList));
75 }
76 e = dcRoot.getChild("date",getDCNamespace());
77 if (e!=null) {
78 foundSomething = true;
79 dcm.setDate(DateParser.parseW3CDateTime(e.getText()));
80 }
81 e = dcRoot.getChild("type",getDCNamespace());
82 if (e!=null) {
83 foundSomething = true;
84 dcm.setType(e.getText());
85 }
86 e = dcRoot.getChild("format",getDCNamespace());
87 if (e!=null) {
88 foundSomething = true;
89 dcm.setFormat(e.getText());
90 }
91 e = dcRoot.getChild("identifier",getDCNamespace());
92 if (e!=null) {
93 foundSomething = true;
94 dcm.setIdentifier(e.getText());
95 }
96 e = dcRoot.getChild("source",getDCNamespace());
97 if (e!=null) {
98 foundSomething = true;
99 dcm.setSource(e.getText());
100 }
101 e = dcRoot.getChild("language",getDCNamespace());
102 if (e!=null) {
103 foundSomething = true;
104 dcm.setLanguage(e.getText());
105 }
106 e = dcRoot.getChild("relation",getDCNamespace());
107 if (e!=null) {
108 foundSomething = true;
109 dcm.setRelation(e.getText());
110 }
111 e = dcRoot.getChild("coverage",getDCNamespace());
112 if (e!=null) {
113 foundSomething = true;
114 dcm.setCoverage(e.getText());
115 }
116 e = dcRoot.getChild("rights",getDCNamespace());
117 if (e!=null) {
118 foundSomething = true;
119 dcm.setRights(e.getText());
120 }
121
122 return (foundSomething) ? dcm : null;
123 }
124
125 private static final String RDF_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
126 private static final String TAXO_URI = "http://purl.org/rss/1.0/modules/taxonomy/";
127
128 private String getTaxonomy(Element desc) {
129 String d = null;
130 Element taxo = desc.getChild("topic",Namespace.getNamespace(TAXO_URI));
131 if (taxo!=null) {
132 Attribute a = taxo.getAttribute("resource",Namespace.getNamespace((RDF_URI)));
133 if (a!=null) {
134 d = a.getValue();
135 }
136 }
137 return d;
138 }
139
140
141
142 private List parseSubjects(List eList) {
143 List subjects = new ArrayList();
144 for (int i=0;i<eList.size();i++) {
145 Element eSubject = (Element) eList.get(i);
146 Element eDesc = eSubject.getChild("Description",Namespace.getNamespace(RDF_URI));
147 if (eDesc!=null) {
148 String taxonomy = getTaxonomy(eDesc);
149 List eValues = eDesc.getChildren("value",Namespace.getNamespace(RDF_URI));
150 for (int j=0;j<eValues.size();j++) {
151 Element eValue = (Element) eValues.get(j);
152 DCSubject subject = new DCSubjectImpl();
153 subject.setTaxonomyUri(taxonomy);;
154 subject.setValue(eValue.getText());
155 subjects.add(subject);
156 }
157 }
158 else {
159 DCSubject subject = new DCSubjectImpl();
160 subject.setValue(eSubject.getText());
161 subjects.add(subject);
162 }
163 }
164 return subjects;
165 }
166
167 private List parseContributors(List eList) {
168 List contributors = new ArrayList();
169 for (int i=0;i<eList.size();i++) {
170 Element e = (Element) eList.get(i);
171 contributors.add(e.getText());
172 }
173 return contributors;
174
175 }
176
177 }