-
Notifications
You must be signed in to change notification settings - Fork 2
/
datacite.clj
415 lines (326 loc) · 12.7 KB
/
datacite.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
(ns org.cyverse.metadata-files.datacite
(:use [clojure.data.xml :only [alias-uri element]])
(:require [clojure.string :as string]
[org.cyverse.metadata-files :as mdf]
[org.cyverse.metadata-files.datacite.geolocation :as geolocation]
[org.cyverse.metadata-files.util :as util]))
;; Define aliases for XML namespaces.
(alias-uri :datacite "http://datacite.org/schema/kernel-3")
(alias-uri :xml "http://www.w3.org/XML/1998/namespace")
(alias-uri :xsi "http://www.w3.org/2001/XMLSchema-instance")
;; Required field: identifier
(deftype Identifier [type id]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/identifier {::datacite/identifierType type} id)))
(deftype IdentifierGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{"Identifier" "identifierType"})
(missing-attributes [self]
(util/missing-attributes (mdf/required-attributes self) attributes))
(generate [self]
(if-let [missing (seq (mdf/missing-attributes self))]
(util/missing-required-attributes missing)
(Identifier. (util/attr-value attributes "identifierType") (util/attr-value attributes "identifier")))))
;; Required field: creators
(deftype Creator [name affiliation name-id]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/creator {}
[(element ::datacite/creatorName {} name)
(when-not (string/blank? name-id) (element ::datacite/nameIdentifier {} name-id))
(element ::datacite/affiliation {} affiliation)])))
(deftype Creators [creators]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/creators {}
(mapv mdf/to-xml creators))))
(deftype CreatorsGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{"datacite.creator" "creatorAffiliation"})
(missing-attributes [self]
(util/missing-attributes (mdf/required-attributes self) attributes))
(generate [self]
(if-let [missing (seq (mdf/missing-attributes self))]
(util/missing-required-attributes missing)
(let [values (util/associated-attr-values
attributes ["datacite.creator" "creatorAffiliation"] ["creatorNameIdentifier"])]
(Creators. (mapv (fn [[name affiliation name-id]] (Creator. name affiliation name-id)) values))))))
;; Required field: title
(deftype Title [title]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/title {::xml/lang "en"} title)))
(deftype Titles [titles]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/titles {}
(mapv mdf/to-xml titles))))
(deftype TitlesGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{"datacite.title"})
(missing-attributes [self]
(util/missing-attributes (mdf/required-attributes self) attributes))
(generate [self]
(if-let [missing (seq (mdf/missing-attributes self))]
(util/missing-required-attributes missing)
(Titles. (mapv (fn [title] (Title. title)) (util/attr-values attributes "datacite.title"))))))
;; Required field: publisher
(deftype Publisher [publisher]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/publisher {} publisher)))
(deftype PublisherGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{"datacite.publisher"})
(missing-attributes [self]
(util/missing-attributes (mdf/required-attributes self) attributes))
(generate [self]
(if-let [missing (seq (mdf/missing-attributes self))]
(util/missing-required-attributes missing)
(Publisher. (util/attr-value attributes "datacite.publisher")))))
;; Required field: publication year
(deftype PublicationYear [year]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/publicationYear {} year)))
(deftype PublicationYearGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{"datacite.publicationyear"})
(missing-attributes [self]
(util/missing-attributes (mdf/required-attributes self) attributes))
(generate [self]
(if-let [missing (seq (mdf/missing-attributes self))]
(util/missing-required-attributes missing)
(PublicationYear. (util/attr-value attributes "datacite.publicationyear")))))
;; Required field: resource-type
(deftype ResourceType [resource-type]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/resourceType {} resource-type)))
(deftype ResourceTypeGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{"datacite.resourcetype"})
(missing-attributes [self]
(util/missing-attributes (mdf/required-attributes self) attributes))
(generate [self]
(if-let [missing (seq (mdf/missing-attributes self))]
(util/missing-required-attributes missing)
(ResourceType. (util/attr-value attributes "datacite.resourcetype")))))
;; Optional field: subjects
(deftype Subject [subject]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/subject {::xml/lang "en"} subject)))
(deftype Subjects [subjects]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/subjects {}
(mapv mdf/to-xml subjects))))
(deftype SubjectsGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{})
(missing-attributes [_]
#{})
(generate [_]
(when-let [values (seq (util/attr-values attributes "Subject"))]
(Subjects. (->> (mapcat (fn [s] (string/split s #"\s*,\s*")) values)
(mapv (fn [subject] (Subject. subject))))))))
;; Optional field: contributors
(deftype Contributor [name type]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/contributor {}
[(element ::datacite/contributorName {} name)
(element ::datacite/contributorType {} type)])))
(deftype Contributors [contributors]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/contributors {}
(mapv mdf/to-xml contributors))))
(deftype ContributorsGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{})
(missing-attributes [_]
#{})
(generate [_]
(when-let [values (seq (util/associated-attr-values attributes ["contributorName" "contributorType"] []))]
(Contributors. (mapv (fn [[name type]] (Contributor. name type)) values)))))
;; Optional field: alternate identifiers
(deftype AlternateId [id type]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/alternateIdentifier {::datacite/alternateIdentifierType type} id)))
(deftype AlternateIds [alternate-ids]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/alternateIdentifiers {}
(mapv mdf/to-xml alternate-ids))))
(deftype AlternateIdsGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{})
(missing-attributes [_]
#{})
(generate [_]
(let [required-attrs ["AlternateIdentifier" "alternateIdentifierType"]]
(when-let [vs (seq (util/associated-attr-values attributes required-attrs []))]
(AlternateIds. (mapv (fn [[id type]] (AlternateId. id type)) vs))))))
;; Optional field: related identifiers
(deftype RelatedId [id type relation-type]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/relatedIdentifier {::datacite/relatedIdentifierType type ::datacite/relationType relation-type}
id)))
(deftype RelatedIds [related-ids]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/relatedIdentifiers {}
(mapv mdf/to-xml related-ids))))
(deftype RelatedIdsGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{})
(missing-attributes [_]
#{})
(generate [_]
(let [required-attrs ["RelatedIdentifier" "relatedIdentifierType" "relationType"]]
(when-let [vs (seq (util/associated-attr-values attributes required-attrs []))]
(RelatedIds. (mapv (fn [[id type relation-type]] (RelatedId. id type relation-type)) vs))))))
;; Optional field: rightsList
(deftype Rights [rights]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/rights {::xml/lang "en"} rights)))
(deftype RightsList [rights-list]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/rightsList {}
(mapv mdf/to-xml rights-list))))
(deftype RightsListGenterator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{})
(missing-attributes [_]
#{})
(generate [_]
(when-let [vs (seq (util/attr-values attributes "Rights"))]
(RightsList. (mapv (fn [rights] (Rights. rights)) vs)))))
;; Optional field: descriptions
(deftype Description [description type]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/description {::datacite/descriptionType type ::xml/lang "en"} description)))
(deftype Descriptions [descriptions]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/descriptions {}
(mapv mdf/to-xml descriptions))))
(deftype DescriptionsGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{})
(missing-attributes [_]
#{})
(generate [_]
(let [required-attrs ["Description" "descriptionType"]]
(when-let [vs (seq (util/associated-attr-values attributes required-attrs []))]
(Descriptions. (mapv (fn [[description type]] (Description. description type)) vs))))))
;; Optional field: geoLocations
(deftype GeoLocationPlace [place]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/geoLocationPlace {} place)))
(deftype GeoLocationPoint [latitude longitude]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/geoLocationPoint {}
[(element ::datacite/pointLongitude {} longitude)
(element ::datacite/pointLatitude {} latitude)])))
(deftype GeoLocationBox [west east south north]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/geoLocationBox {}
[(element ::datacite/westBoundLongitude {} west)
(element ::datacite/eastBoundLongitude {} east)
(element ::datacite/southBoundLatitude {} south)
(element ::datacite/northBoundLatitude {} north)])))
(deftype GeoLocation [place point box]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/geoLocation {}
[(when place (mdf/to-xml place))
(when point (mdf/to-xml point))
(when box (mdf/to-xml box))])))
(deftype GeoLocations [geolocations]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/geoLocations {}
(mapv mdf/to-xml geolocations))))
(deftype GeoLocationsGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
#{})
(missing-attributes [_]
#{})
(generate [_]
(let [create-point (fn [[latitude longitude]] (GeoLocationPoint. latitude longitude))
create-box (fn [[west east north south]] (GeoLocationBox. west east north south))
optional-attrs ["geoLocationPlace" "geoLocationPoint" "geoLocationBox"]]
(when-let [vs (seq (util/associated-attr-values attributes [] optional-attrs))]
(GeoLocations. (vec (for [[place point box] (map geolocation/parse-attrs vs)]
(GeoLocation. (when place (GeoLocationPlace. place))
(when point (create-point point))
(when box (create-box box))))))))))
;; The datacite document itself.
(def ^:private schema-locations
(->> ["https://schema.datacite.org/meta/kernel-3.1 https://schema.datacite.org/meta/kernel-3.1/metadata.xsd"]
(string/join " ")))
(deftype Datacite [elements]
mdf/XmlSerializable
(to-xml [_]
(element ::datacite/resource {:xmlns/datacite "http://datacite.org/schema/kernel-3"
:xmlns/xsi "http://www.w3.org/2001/XMLSchema-instance"
::xsi/schemaLocation schema-locations}
(mapv mdf/to-xml elements))))
(defn- required-element-factories [attributes]
[(IdentifierGenerator. attributes)
(CreatorsGenerator. attributes)
(TitlesGenerator. attributes)
(PublisherGenerator. attributes)
(PublicationYearGenerator. attributes)
(ResourceTypeGenerator. attributes)])
(defn- optional-element-factories [attributes]
[(SubjectsGenerator. attributes)
(ContributorsGenerator. attributes)
(AlternateIdsGenerator. attributes)
(RelatedIdsGenerator. attributes)
(RightsListGenterator. attributes)
(DescriptionsGenerator. attributes)
(GeoLocationsGenerator. attributes)])
(defn- element-factories [attributes]
(concat (required-element-factories attributes)
(optional-element-factories attributes)))
(deftype DataciteGenerator [attributes]
mdf/ElementFactory
(required-attributes [_]
(->> (required-element-factories attributes)
(map mdf/required-attributes)
(apply concat)
set))
(missing-attributes [self]
(util/missing-attributes (mdf/required-attributes self) attributes))
(generate [self]
(if-let [missing (seq (mdf/missing-attributes self))]
(util/missing-required-attributes missing)
(Datacite. (remove nil? (mapv mdf/generate (element-factories attributes)))))))
(defn build-datacite [attributes]
(mdf/to-xml (mdf/generate (DataciteGenerator. attributes))))