Project

General

Profile

1
package eu.dnetlib.data.collector.plugins.schemaorg;
2

    
3
import org.w3c.dom.Attr;
4
import org.w3c.dom.Document;
5
import org.w3c.dom.Element;
6

    
7
import javax.xml.parsers.DocumentBuilder;
8
import javax.xml.parsers.DocumentBuilderFactory;
9
import javax.xml.parsers.ParserConfigurationException;
10
import javax.xml.transform.Transformer;
11
import javax.xml.transform.TransformerFactory;
12
import javax.xml.transform.dom.DOMSource;
13
import javax.xml.transform.stream.StreamResult;
14
import java.io.StringWriter;
15
import java.time.LocalDate;
16
import java.time.format.DateTimeFormatter;
17
import java.util.Calendar;
18
import java.util.Date;
19
import java.util.List;
20

    
21
public class DatasetDocument {
22
	private List<Identifier> identifiers;
23
	private List<Creator> creators;
24
	private List<String> titles;
25
	private List<String> alternativeTitles;
26
	private List<String> publishers;
27
	private List<LocalDate> publicationDates;
28
	private List<String> subjects;
29
	private List<Contributor> contributors;
30
	private List<LocalDate> createdDates;
31
	private List<LocalDate> updatedDates;
32
	private List<String> languages;
33
	private List<ResourceType> resourceTypes;
34
	private List<AlternateIdentifier> alternateIdentifier;
35
	private List<Citation> citations;
36
	private List<String> sizes;
37
	private List<String> format;
38
	private List<String> version;
39
	private List<License> licenses;
40
	private List<String> descriptions;
41
	private List<String> disambiguatingDescriptions;
42
	private List<SpatialCoverage> geoLocations;
43

    
44
	public List<Identifier> getIdentifiers() {
45
		return identifiers;
46
	}
47

    
48
	public void setIdentifiers(List<Identifier> identifiers) {
49
		this.identifiers = identifiers;
50
	}
51

    
52
	public List<Creator> getCreators() {
53
		return creators;
54
	}
55

    
56
	public void setCreators(List<Creator> creators) {
57
		this.creators = creators;
58
	}
59

    
60
	public List<String> getTitles() {
61
		return titles;
62
	}
63

    
64
	public void setTitles(List<String> titles) {
65
		this.titles = titles;
66
	}
67

    
68
	public List<String> getAlternativeTitles() {
69
		return alternativeTitles;
70
	}
71

    
72
	public void setAlternativeTitles(List<String> alternativeTitles) {
73
		this.alternativeTitles = alternativeTitles;
74
	}
75

    
76
	public List<String> getPublishers() {
77
		return publishers;
78
	}
79

    
80
	public void setPublishers(List<String> publishers) {
81
		this.publishers = publishers;
82
	}
83

    
84
	public List<LocalDate> getPublicationDates() {
85
		return publicationDates;
86
	}
87

    
88
	public void setPublicationDates(List<LocalDate> publicationDates) {
89
		this.publicationDates = publicationDates;
90
	}
91

    
92
	public List<String> getSubjects() {
93
		return subjects;
94
	}
95

    
96
	public void setSubjects(List<String> subjects) {
97
		this.subjects = subjects;
98
	}
99

    
100
	public List<Contributor> getContributors() {
101
		return contributors;
102
	}
103

    
104
	public void setContributors(List<Contributor> contributors) {
105
		this.contributors = contributors;
106
	}
107

    
108
	public List<LocalDate> getCreatedDates() {
109
		return createdDates;
110
	}
111

    
112
	public void setCreatedDates(List<LocalDate> createdDates) {
113
		this.createdDates = createdDates;
114
	}
115

    
116
	public List<LocalDate> getUpdatedDates() {
117
		return updatedDates;
118
	}
119

    
120
	public void setUpdatedDates(List<LocalDate> updatedDates) {
121
		this.updatedDates = updatedDates;
122
	}
123

    
124
	public List<String> getLanguages() {
125
		return languages;
126
	}
127

    
128
	public void setLanguages(List<String> languages) {
129
		this.languages = languages;
130
	}
131

    
132
	public List<ResourceType> getResourceTypes() {
133
		return resourceTypes;
134
	}
135

    
136
	public void setResourceTypes(List<ResourceType> resourceTypes) {
137
		this.resourceTypes = resourceTypes;
138
	}
139

    
140
	public List<AlternateIdentifier> getAlternateIdentifier() {
141
		return alternateIdentifier;
142
	}
143

    
144
	public void setAlternateIdentifier(List<AlternateIdentifier> alternateIdentifier) {
145
		this.alternateIdentifier = alternateIdentifier;
146
	}
147

    
148
	public List<Citation> getCitations() {
149
		return citations;
150
	}
151

    
152
	public void setCitations(List<Citation> citations) {
153
		this.citations = citations;
154
	}
155

    
156
	public List<String> getSizes() {
157
		return sizes;
158
	}
159

    
160
	public void setSizes(List<String> sizes) {
161
		this.sizes = sizes;
162
	}
163

    
164
	public List<String> getFormat() {
165
		return format;
166
	}
167

    
168
	public void setFormat(List<String> format) {
169
		this.format = format;
170
	}
171

    
172
	public List<String> getVersion() {
173
		return version;
174
	}
175

    
176
	public void setVersion(List<String> version) {
177
		this.version = version;
178
	}
179

    
180
	public List<License> getLicenses() {
181
		return licenses;
182
	}
183

    
184
	public void setLicenses(List<License> licenses) {
185
		this.licenses = licenses;
186
	}
187

    
188
	public List<String> getDescriptions() {
189
		return descriptions;
190
	}
191

    
192
	public void setDescriptions(List<String> descriptions) {
193
		this.descriptions = descriptions;
194
	}
195

    
196
	public List<String> getDisambiguatingDescriptions() {
197
		return disambiguatingDescriptions;
198
	}
199

    
200
	public void setDisambiguatingDescriptions(List<String> disambiguatingDescriptions) {
201
		this.disambiguatingDescriptions = disambiguatingDescriptions;
202
	}
203

    
204
	public List<SpatialCoverage> getGeoLocations() {
205
		return geoLocations;
206
	}
207

    
208
	public void setGeoLocations(List<SpatialCoverage> geoLocations) {
209
		this.geoLocations = geoLocations;
210
	}
211

    
212
	public String toXml() throws Exception {
213
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
214
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
215
		Document doc = docBuilder.newDocument();
216

    
217
		Element root = doc.createElement("dataset");
218
		doc.appendChild(root);
219

    
220
		if(this.identifiers!=null){
221
			for(Identifier item : this.identifiers){
222
				item.toXml(root);
223
			}
224
		}
225
		if(this.creators!=null){
226
			Element creators = doc.createElement("creators");
227
			root.appendChild(creators);
228
			for(Creator item : this.creators){
229
				item.toXml(creators);
230
			}
231
		}
232
		if(this.titles!=null || this.alternativeTitles!=null){
233
			Element titles = doc.createElement("titles");
234
			root.appendChild(titles);
235
			if(this.titles!=null) {
236
				for (String item : this.titles) {
237
					Element title = doc.createElement("title");
238
					titles.appendChild(title);
239
					title.appendChild(doc.createTextNode(item));
240
				}
241
			}
242
			if(this.alternativeTitles!=null) {
243
				for (String item : this.alternativeTitles) {
244
					Element title = doc.createElement("title");
245
					titles.appendChild(title);
246
					title.setAttribute("titleType", "AlternativeTitle");
247
					title.appendChild(doc.createTextNode(item));
248
				}
249
			}
250
		}
251
		if(this.publishers!=null){
252
			for(String item : this.publishers){
253
				Element publisher = doc.createElement("publisher");
254
				root.appendChild(publisher);
255
				publisher.appendChild(doc.createTextNode(item));
256
			}
257
		}
258
		if(this.publicationDates!=null){
259
			for(LocalDate item : this.publicationDates){
260
				Element publicationYear = doc.createElement("publicationYear");
261
				root.appendChild(publicationYear);
262
				publicationYear.appendChild(doc.createTextNode(Integer.toString(item.getYear())));
263
			}
264
		}
265
		if(this.subjects!=null){
266
			Element subjects = doc.createElement("subjects");
267
			root.appendChild(subjects);
268
			for(String item : this.subjects){
269
				Element subject = doc.createElement("subject");
270
				subjects.appendChild(subject);
271
				subject.appendChild(doc.createTextNode(item));
272
			}
273
		}
274
		if(this.contributors!=null){
275
			for(Contributor item : this.contributors){
276
				item.toXml(root);
277
			}
278
		}
279
		if(this.createdDates!=null || this.updatedDates!=null){
280
			Element dates = doc.createElement("dates");
281
			root.appendChild(dates);
282

    
283
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-DD");
284

    
285
			if(createdDates!=null) {
286
				for (LocalDate item : this.createdDates) {
287
					Element date = doc.createElement("date");
288
					root.appendChild(date);
289
					date.setAttribute("dateType", "Created");
290
					date.appendChild(doc.createTextNode(item.format(formatter)));
291
				}
292
			}
293
			if(updatedDates!=null) {
294
				for (LocalDate item : this.updatedDates) {
295
					Element date = doc.createElement("date");
296
					root.appendChild(date);
297
					date.setAttribute("dateType", "Updated");
298
					date.appendChild(doc.createTextNode(item.format(formatter)));
299
				}
300
			}
301
		}
302
		if(this.languages!=null){
303
			for(String item : this.languages){
304
				Element language = doc.createElement("language");
305
				root.appendChild(language);
306
				language.appendChild(doc.createTextNode(item));
307
			}
308
		}
309
		if(this.resourceTypes!=null){
310
			for(ResourceType item : this.resourceTypes){
311
				item.toXml(root);
312
			}
313
		}
314
		if(this.alternateIdentifier!=null){
315
			Element alternateIdentifiers = doc.createElement("alternateIdentifiers");
316
			root.appendChild(alternateIdentifiers);
317
			for(AlternateIdentifier item : this.alternateIdentifier){
318
				item.toXml(alternateIdentifiers);
319
			}
320
		}
321
		if(this.citations!=null){
322
			for(Citation item : this.citations){
323
				item.toXml(root);
324
			}
325
		}
326
		if(this.sizes!=null){
327
			Element sizes = doc.createElement("sizes");
328
			root.appendChild(sizes);
329
			for(String item : this.sizes){
330
				Element size = doc.createElement("size");
331
				sizes.appendChild(size);
332
				size.appendChild(doc.createTextNode(item));
333
			}
334
		}
335
		if(this.format!=null){
336
			Element formats = doc.createElement("formats");
337
			root.appendChild(formats);
338
			for(String item : this.format){
339
				Element format = doc.createElement("format");
340
				formats.appendChild(format);
341
				format.appendChild(doc.createTextNode(item));
342
			}
343
		}
344
		if(this.version!=null){
345
			for(String item : this.version){
346
				Element version = doc.createElement("version");
347
				root.appendChild(version);
348
				version.appendChild(doc.createTextNode(item));
349
			}
350
		}
351
		if(this.licenses!=null){
352
			Element rightsList = doc.createElement("rightsList");
353
			root.appendChild(rightsList);
354
			for(License item : this.licenses){
355
				item.toXml(rightsList);
356
			}
357
		}
358
		if(this.descriptions!=null || this.disambiguatingDescriptions!=null){
359
			Element descriptions = doc.createElement("descriptions");
360
			root.appendChild(descriptions);
361
			if(this.descriptions!=null) {
362
				for (String item : this.descriptions) {
363
					Element description = doc.createElement("description");
364
					descriptions.appendChild(description);
365
					description.setAttribute("descriptionType", "Abstract");
366
					description.appendChild(doc.createTextNode(item));
367
				}
368
			}
369
			if(this.disambiguatingDescriptions!=null) {
370
				for (String item : this.disambiguatingDescriptions) {
371
					Element description = doc.createElement("description");
372
					descriptions.appendChild(description);
373
					description.setAttribute("descriptionType", "Other");
374
					description.appendChild(doc.createTextNode(item));
375
				}
376
			}
377
		}
378
		if(this.geoLocations!=null){
379
			Element geoLocations = doc.createElement("geoLocations");
380
			root.appendChild(geoLocations);
381
			for(SpatialCoverage item : this.geoLocations){
382
				item.toXml(geoLocations);
383
			}
384
		}
385

    
386
		TransformerFactory tf = TransformerFactory.newInstance();
387
		Transformer transformer = tf.newTransformer();
388
		StringWriter writer = new StringWriter();
389
		transformer.transform(new DOMSource(doc), new StreamResult(writer));
390
		String xml = writer.getBuffer().toString();
391
		return xml;
392
	}
393

    
394
	public static class SpatialCoverage{
395
		public static class Point{
396
			public String latitude;
397
			public String longitude;
398

    
399
			public Point() {}
400

    
401
			public Point(String latitude, String longitude){
402
				this.latitude = latitude;
403
				this.longitude = longitude;
404
			}
405
		}
406
		public String name;
407
		public List<Point> points;
408
		public List<String> boxes;
409

    
410
		public SpatialCoverage() {}
411

    
412
		public SpatialCoverage(String name, List<Point> points, List<String> boxes ) {
413
			this.name = name;
414
			this.points = points;
415
			this.boxes = boxes;
416
		}
417

    
418
		public void toXml(Element parent){
419
			Element node = parent.getOwnerDocument().createElement("geoLocation");
420
			parent.appendChild(node);
421

    
422
			if(this.points!=null) {
423
				for(Point point : this.points) {
424
					if(point.latitude == null || point.longitude == null) continue;
425
					Element geoLocationPoint = parent.getOwnerDocument().createElement("geoLocationPoint");
426
					geoLocationPoint.appendChild(parent.getOwnerDocument().createTextNode(String.format("%s %s", point.latitude, point.longitude)));
427
					node.appendChild(geoLocationPoint);
428
				}
429
			}
430
			if(this.boxes!=null) {
431
				for(String box : this.boxes) {
432
					if(box == null) continue;
433
					Element geoLocationBox = parent.getOwnerDocument().createElement("geoLocationBox");
434
					geoLocationBox.appendChild(parent.getOwnerDocument().createTextNode(box));
435
					node.appendChild(geoLocationBox);
436
				}
437
			}
438
			if(this.name!=null) {
439
				Element geoLocationPlace = parent.getOwnerDocument().createElement("geoLocationPlace");
440
				geoLocationPlace.appendChild(parent.getOwnerDocument().createTextNode(this.name));
441
				node.appendChild(geoLocationPlace);
442
			}
443
		}
444
	}
445

    
446
	public static class License{
447
		public String name;
448
		public String url;
449

    
450
		public License() {}
451

    
452
		public License(String name, String url) {
453
			this.name = name;
454
			this.url = url;
455
		}
456

    
457
		public void toXml(Element parent){
458
			Element node = parent.getOwnerDocument().createElement("rights");
459
			parent.appendChild(node);
460

    
461
			if(this.url!=null) {
462
				node.setAttribute("rightsURI", this.url);
463
			}
464
			if(this.name!=null) {
465
				node.appendChild(parent.getOwnerDocument().createTextNode(this.name));
466
			}
467
		}
468
	}
469

    
470
	public static class Citation{
471
		public enum CitationIdentifierType{
472
			ARK, arXiv, bibcode, DOI, EAN13, EISSN, Handle, ISBN, ISSN, ISTC, LISSN, LSID, PMID,
473
			PURL, UPC, URL, URN
474
		}
475

    
476
		public CitationIdentifierType type;
477
		public String value;
478

    
479
		public Citation() {}
480

    
481
		public Citation(String value, CitationIdentifierType type) {
482
			this.value = value;
483
			this.type = type;
484
		}
485

    
486
		public void toXml(Element parent){
487
			Element node = parent.getOwnerDocument().createElement("relatedIdentifier");
488
			parent.appendChild(node);
489

    
490
			node.setAttribute("relatedIdentifierType", this.type.toString());
491
			node.setAttribute("relationType", "Cites");
492
			node.appendChild(parent.getOwnerDocument().createTextNode(this.value));
493
		}
494
	}
495

    
496
	public static class Contributor{
497
		public enum ContributorType{
498
			ContactPerson, DataCollector, DataCurator, DataManager, Distributor, Editor, Funder, HostingInstitution,
499
			Producer, ProjectLeader, ProjectManager, ProjectMember, RegistrationAgency, RegistrationAuthority,
500
			RelatedPerson, Researcher, ResearchGroup, RightsHolder, Sponsor, Supervisor, WorkPackageLeader, Other
501
		}
502

    
503
		public String name;
504
		public List<String> affiliations;
505
		public ContributorType type;
506

    
507
		public Contributor() {
508
		}
509

    
510
		public Contributor(String name) {
511
			this.name = name;
512
		}
513

    
514
		public Contributor(String name, List<String> affiliations) {
515
			this.name = name;
516
			this.affiliations = affiliations;
517
		}
518

    
519
		public Contributor(String name, List<String> affiliations, ContributorType type) {
520
			this.name = name;
521
			this.affiliations = affiliations;
522
			this.type = type;
523
		}
524

    
525
		public void toXml(Element parent){
526
			Element node = parent.getOwnerDocument().createElement("contributor");
527
			parent.appendChild(node);
528

    
529
			node.setAttribute("contributorType", this.type.toString());
530

    
531
			if(this.name!=null) {
532
				Element contributorName = parent.getOwnerDocument().createElement("contributorName");
533
				node.appendChild(contributorName);
534
				contributorName.appendChild(parent.getOwnerDocument().createTextNode(this.name));
535
			}
536
			if(this.affiliations!=null) {
537
				for(String item : this.affiliations) {
538
					Element affiliation = parent.getOwnerDocument().createElement("affiliation");
539
					node.appendChild(affiliation);
540
					affiliation.appendChild(parent.getOwnerDocument().createTextNode(item));
541
				}
542
			}
543
		}
544
	}
545

    
546
	public static class AlternateIdentifier{
547
		public String identifier;
548
		public String type;
549

    
550
		public AlternateIdentifier() {}
551

    
552
		public AlternateIdentifier(String identifier, String type) {
553
			this.identifier = identifier;
554
			this.type = type;
555
		}
556

    
557
		public void toXml(Element parent){
558
			Element node = parent.getOwnerDocument().createElement("alternateIdentifier");
559
			parent.appendChild(node);
560

    
561
			if(this.type!=null) {
562
				node.setAttribute("alternateIdentifierType", this.type);
563
			}
564
			if(this.identifier!=null) {
565
				node.appendChild(parent.getOwnerDocument().createTextNode(this.identifier));
566
			}
567
		}
568
	}
569

    
570
	public static class ResourceType{
571
		public enum ResourceTypeGeneralType {
572
			Audiovisual, Collection, Dataset, Event, Image, InteractiveResource, Model, PhysicalObject, Service,
573
			Software, Sound, Text, Workflow, Other
574
		}
575

    
576
		public ResourceTypeGeneralType type;
577

    
578
		public ResourceType() {}
579

    
580
		public ResourceType(ResourceTypeGeneralType type) {
581
			this.type = type;
582
		}
583

    
584
		public void toXml(Element parent){
585
			Element node = parent.getOwnerDocument().createElement("resourceType");
586
			parent.appendChild(node);
587

    
588
			if(this.type!=null) {
589
				node.setAttribute("resourceTypeGeneral", this.type.toString());
590
			}
591
		}
592
	}
593

    
594
	public static class Creator {
595
		public String name;
596
		public List<String> affiliations;
597

    
598
		public Creator() {
599
		}
600

    
601
		public Creator(String name) {
602
			this.name = name;
603
		}
604

    
605
		public Creator(String name, List<String> affiliations) {
606
			this.name = name;
607
			this.affiliations = affiliations;
608
		}
609

    
610
		public void toXml(Element parent){
611
			Element node = parent.getOwnerDocument().createElement("creator");
612
			parent.appendChild(node);
613

    
614
			if(this.name!=null) {
615
				Element creatorName = parent.getOwnerDocument().createElement("creatorName");
616
				node.appendChild(creatorName);
617
				creatorName.appendChild(parent.getOwnerDocument().createTextNode(this.name));
618
			}
619
			if(this.affiliations!=null) {
620
				for(String item : this.affiliations) {
621
					Element affiliation = parent.getOwnerDocument().createElement("affiliation");
622
					node.appendChild(affiliation);
623
					affiliation.appendChild(parent.getOwnerDocument().createTextNode(item));
624
				}
625
			}
626
		}
627
	}
628

    
629
	public static class Identifier {
630
		public enum IdentifierType {
631
			ARK, DOI, Handle, PURL, URN, URL
632
		}
633

    
634
		public String value;
635
		public IdentifierType type;
636

    
637
		public Identifier() {
638
		}
639

    
640
		public Identifier(IdentifierType type, String value) {
641
			this.type = type;
642
			this.value = value;
643
		}
644

    
645
		public void toXml(Element parent){
646
			Element node = parent.getOwnerDocument().createElement("identifier");
647
			parent.appendChild(node);
648

    
649
			node.setAttribute("identifierType", this.type.toString());
650
			if(this.value!=null) {
651
				node.appendChild(parent.getOwnerDocument().createTextNode(this.value));
652
			}
653
		}
654
	}
655
}
(1-1/11)