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
	private  static String emptyXml;
213
	private  static Object lockEmptyXml = new Object();
214
	public static String emptyXml() {
215
		if(DatasetDocument.emptyXml!=null) return DatasetDocument.emptyXml;
216

    
217
		String xml = null;
218
		try {
219
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
220
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
221
			Document doc = docBuilder.newDocument();
222

    
223
			Element root = doc.createElement("dataset");
224
			doc.appendChild(root);
225

    
226
			TransformerFactory tf = TransformerFactory.newInstance();
227
			Transformer transformer = tf.newTransformer();
228
			StringWriter writer = new StringWriter();
229
			transformer.transform(new DOMSource(doc), new StreamResult(writer));
230
			xml = writer.getBuffer().toString();
231
		}catch(Exception ex){
232
			xml = "<dataset/>";
233
		}
234

    
235
		synchronized (DatasetDocument.lockEmptyXml) {
236
			if (DatasetDocument.emptyXml == null) DatasetDocument.emptyXml = xml;
237
		}
238

    
239
		return DatasetDocument.emptyXml;
240
	}
241

    
242
	public String toXml() throws Exception {
243
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
244
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
245
		Document doc = docBuilder.newDocument();
246

    
247
		Element root = doc.createElement("dataset");
248
		doc.appendChild(root);
249

    
250
		if(this.identifiers!=null){
251
			for(Identifier item : this.identifiers){
252
				item.toXml(root);
253
			}
254
		}
255
		if(this.creators!=null){
256
			Element creators = doc.createElement("creators");
257
			root.appendChild(creators);
258
			for(Creator item : this.creators){
259
				item.toXml(creators);
260
			}
261
		}
262
		if(this.titles!=null || this.alternativeTitles!=null){
263
			Element titles = doc.createElement("titles");
264
			root.appendChild(titles);
265
			if(this.titles!=null) {
266
				for (String item : this.titles) {
267
					Element title = doc.createElement("title");
268
					titles.appendChild(title);
269
					title.appendChild(doc.createTextNode(item));
270
				}
271
			}
272
			if(this.alternativeTitles!=null) {
273
				for (String item : this.alternativeTitles) {
274
					Element title = doc.createElement("title");
275
					titles.appendChild(title);
276
					title.setAttribute("titleType", "AlternativeTitle");
277
					title.appendChild(doc.createTextNode(item));
278
				}
279
			}
280
		}
281
		if(this.publishers!=null){
282
			for(String item : this.publishers){
283
				Element publisher = doc.createElement("publisher");
284
				root.appendChild(publisher);
285
				publisher.appendChild(doc.createTextNode(item));
286
			}
287
		}
288
		if(this.publicationDates!=null){
289
			for(LocalDate item : this.publicationDates){
290
				Element publicationYear = doc.createElement("publicationYear");
291
				root.appendChild(publicationYear);
292
				publicationYear.appendChild(doc.createTextNode(Integer.toString(item.getYear())));
293
			}
294
		}
295
		if(this.subjects!=null){
296
			Element subjects = doc.createElement("subjects");
297
			root.appendChild(subjects);
298
			for(String item : this.subjects){
299
				Element subject = doc.createElement("subject");
300
				subjects.appendChild(subject);
301
				subject.appendChild(doc.createTextNode(item));
302
			}
303
		}
304
		if(this.contributors!=null){
305
			for(Contributor item : this.contributors){
306
				item.toXml(root);
307
			}
308
		}
309
		if(this.createdDates!=null || this.updatedDates!=null){
310
			Element dates = doc.createElement("dates");
311
			root.appendChild(dates);
312

    
313
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-DD");
314

    
315
			if(createdDates!=null) {
316
				for (LocalDate item : this.createdDates) {
317
					Element date = doc.createElement("date");
318
					root.appendChild(date);
319
					date.setAttribute("dateType", "Created");
320
					date.appendChild(doc.createTextNode(item.format(formatter)));
321
				}
322
			}
323
			if(updatedDates!=null) {
324
				for (LocalDate item : this.updatedDates) {
325
					Element date = doc.createElement("date");
326
					root.appendChild(date);
327
					date.setAttribute("dateType", "Updated");
328
					date.appendChild(doc.createTextNode(item.format(formatter)));
329
				}
330
			}
331
		}
332
		if(this.languages!=null){
333
			for(String item : this.languages){
334
				Element language = doc.createElement("language");
335
				root.appendChild(language);
336
				language.appendChild(doc.createTextNode(item));
337
			}
338
		}
339
		if(this.resourceTypes!=null){
340
			for(ResourceType item : this.resourceTypes){
341
				item.toXml(root);
342
			}
343
		}
344
		if(this.alternateIdentifier!=null){
345
			Element alternateIdentifiers = doc.createElement("alternateIdentifiers");
346
			root.appendChild(alternateIdentifiers);
347
			for(AlternateIdentifier item : this.alternateIdentifier){
348
				item.toXml(alternateIdentifiers);
349
			}
350
		}
351
		if(this.citations!=null){
352
			for(Citation item : this.citations){
353
				item.toXml(root);
354
			}
355
		}
356
		if(this.sizes!=null){
357
			Element sizes = doc.createElement("sizes");
358
			root.appendChild(sizes);
359
			for(String item : this.sizes){
360
				Element size = doc.createElement("size");
361
				sizes.appendChild(size);
362
				size.appendChild(doc.createTextNode(item));
363
			}
364
		}
365
		if(this.format!=null){
366
			Element formats = doc.createElement("formats");
367
			root.appendChild(formats);
368
			for(String item : this.format){
369
				Element format = doc.createElement("format");
370
				formats.appendChild(format);
371
				format.appendChild(doc.createTextNode(item));
372
			}
373
		}
374
		if(this.version!=null){
375
			for(String item : this.version){
376
				Element version = doc.createElement("version");
377
				root.appendChild(version);
378
				version.appendChild(doc.createTextNode(item));
379
			}
380
		}
381
		if(this.licenses!=null){
382
			Element rightsList = doc.createElement("rightsList");
383
			root.appendChild(rightsList);
384
			for(License item : this.licenses){
385
				item.toXml(rightsList);
386
			}
387
		}
388
		if(this.descriptions!=null || this.disambiguatingDescriptions!=null){
389
			Element descriptions = doc.createElement("descriptions");
390
			root.appendChild(descriptions);
391
			if(this.descriptions!=null) {
392
				for (String item : this.descriptions) {
393
					Element description = doc.createElement("description");
394
					descriptions.appendChild(description);
395
					description.setAttribute("descriptionType", "Abstract");
396
					description.appendChild(doc.createTextNode(item));
397
				}
398
			}
399
			if(this.disambiguatingDescriptions!=null) {
400
				for (String item : this.disambiguatingDescriptions) {
401
					Element description = doc.createElement("description");
402
					descriptions.appendChild(description);
403
					description.setAttribute("descriptionType", "Other");
404
					description.appendChild(doc.createTextNode(item));
405
				}
406
			}
407
		}
408
		if(this.geoLocations!=null){
409
			Element geoLocations = doc.createElement("geoLocations");
410
			root.appendChild(geoLocations);
411
			for(SpatialCoverage item : this.geoLocations){
412
				item.toXml(geoLocations);
413
			}
414
		}
415

    
416
		TransformerFactory tf = TransformerFactory.newInstance();
417
		Transformer transformer = tf.newTransformer();
418
		StringWriter writer = new StringWriter();
419
		transformer.transform(new DOMSource(doc), new StreamResult(writer));
420
		String xml = writer.getBuffer().toString();
421
		return xml;
422
	}
423

    
424
	public static class SpatialCoverage{
425
		public static class Point{
426
			public String latitude;
427
			public String longitude;
428

    
429
			public Point() {}
430

    
431
			public Point(String latitude, String longitude){
432
				this.latitude = latitude;
433
				this.longitude = longitude;
434
			}
435
		}
436
		public String name;
437
		public List<Point> points;
438
		public List<String> boxes;
439

    
440
		public SpatialCoverage() {}
441

    
442
		public SpatialCoverage(String name, List<Point> points, List<String> boxes ) {
443
			this.name = name;
444
			this.points = points;
445
			this.boxes = boxes;
446
		}
447

    
448
		public void toXml(Element parent){
449
			Element node = parent.getOwnerDocument().createElement("geoLocation");
450
			parent.appendChild(node);
451

    
452
			if(this.points!=null) {
453
				for(Point point : this.points) {
454
					if(point.latitude == null || point.longitude == null) continue;
455
					Element geoLocationPoint = parent.getOwnerDocument().createElement("geoLocationPoint");
456
					geoLocationPoint.appendChild(parent.getOwnerDocument().createTextNode(String.format("%s %s", point.latitude, point.longitude)));
457
					node.appendChild(geoLocationPoint);
458
				}
459
			}
460
			if(this.boxes!=null) {
461
				for(String box : this.boxes) {
462
					if(box == null) continue;
463
					Element geoLocationBox = parent.getOwnerDocument().createElement("geoLocationBox");
464
					geoLocationBox.appendChild(parent.getOwnerDocument().createTextNode(box));
465
					node.appendChild(geoLocationBox);
466
				}
467
			}
468
			if(this.name!=null) {
469
				Element geoLocationPlace = parent.getOwnerDocument().createElement("geoLocationPlace");
470
				geoLocationPlace.appendChild(parent.getOwnerDocument().createTextNode(this.name));
471
				node.appendChild(geoLocationPlace);
472
			}
473
		}
474
	}
475

    
476
	public static class License{
477
		public String name;
478
		public String url;
479

    
480
		public License() {}
481

    
482
		public License(String name, String url) {
483
			this.name = name;
484
			this.url = url;
485
		}
486

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

    
491
			if(this.url!=null) {
492
				node.setAttribute("rightsURI", this.url);
493
			}
494
			if(this.name!=null) {
495
				node.appendChild(parent.getOwnerDocument().createTextNode(this.name));
496
			}
497
		}
498
	}
499

    
500
	public static class Citation{
501
		public enum CitationIdentifierType{
502
			ARK, arXiv, bibcode, DOI, EAN13, EISSN, Handle, ISBN, ISSN, ISTC, LISSN, LSID, PMID,
503
			PURL, UPC, URL, URN
504
		}
505

    
506
		public CitationIdentifierType type;
507
		public String value;
508

    
509
		public Citation() {}
510

    
511
		public Citation(String value, CitationIdentifierType type) {
512
			this.value = value;
513
			this.type = type;
514
		}
515

    
516
		public void toXml(Element parent){
517
			Element node = parent.getOwnerDocument().createElement("relatedIdentifier");
518
			parent.appendChild(node);
519

    
520
			node.setAttribute("relatedIdentifierType", this.type.toString());
521
			node.setAttribute("relationType", "Cites");
522
			node.appendChild(parent.getOwnerDocument().createTextNode(this.value));
523
		}
524
	}
525

    
526
	public static class Contributor{
527
		public enum ContributorType{
528
			ContactPerson, DataCollector, DataCurator, DataManager, Distributor, Editor, Funder, HostingInstitution,
529
			Producer, ProjectLeader, ProjectManager, ProjectMember, RegistrationAgency, RegistrationAuthority,
530
			RelatedPerson, Researcher, ResearchGroup, RightsHolder, Sponsor, Supervisor, WorkPackageLeader, Other
531
		}
532

    
533
		public String name;
534
		public List<String> affiliations;
535
		public ContributorType type;
536

    
537
		public Contributor() {
538
		}
539

    
540
		public Contributor(String name) {
541
			this.name = name;
542
		}
543

    
544
		public Contributor(String name, List<String> affiliations) {
545
			this.name = name;
546
			this.affiliations = affiliations;
547
		}
548

    
549
		public Contributor(String name, List<String> affiliations, ContributorType type) {
550
			this.name = name;
551
			this.affiliations = affiliations;
552
			this.type = type;
553
		}
554

    
555
		public void toXml(Element parent){
556
			Element node = parent.getOwnerDocument().createElement("contributor");
557
			parent.appendChild(node);
558

    
559
			node.setAttribute("contributorType", this.type.toString());
560

    
561
			if(this.name!=null) {
562
				Element contributorName = parent.getOwnerDocument().createElement("contributorName");
563
				node.appendChild(contributorName);
564
				contributorName.appendChild(parent.getOwnerDocument().createTextNode(this.name));
565
			}
566
			if(this.affiliations!=null) {
567
				for(String item : this.affiliations) {
568
					Element affiliation = parent.getOwnerDocument().createElement("affiliation");
569
					node.appendChild(affiliation);
570
					affiliation.appendChild(parent.getOwnerDocument().createTextNode(item));
571
				}
572
			}
573
		}
574
	}
575

    
576
	public static class AlternateIdentifier{
577
		public String identifier;
578
		public String type;
579

    
580
		public AlternateIdentifier() {}
581

    
582
		public AlternateIdentifier(String identifier, String type) {
583
			this.identifier = identifier;
584
			this.type = type;
585
		}
586

    
587
		public void toXml(Element parent){
588
			Element node = parent.getOwnerDocument().createElement("alternateIdentifier");
589
			parent.appendChild(node);
590

    
591
			if(this.type!=null) {
592
				node.setAttribute("alternateIdentifierType", this.type);
593
			}
594
			if(this.identifier!=null) {
595
				node.appendChild(parent.getOwnerDocument().createTextNode(this.identifier));
596
			}
597
		}
598
	}
599

    
600
	public static class ResourceType{
601
		public enum ResourceTypeGeneralType {
602
			Audiovisual, Collection, Dataset, Event, Image, InteractiveResource, Model, PhysicalObject, Service,
603
			Software, Sound, Text, Workflow, Other
604
		}
605

    
606
		public ResourceTypeGeneralType type;
607

    
608
		public ResourceType() {}
609

    
610
		public ResourceType(ResourceTypeGeneralType type) {
611
			this.type = type;
612
		}
613

    
614
		public void toXml(Element parent){
615
			Element node = parent.getOwnerDocument().createElement("resourceType");
616
			parent.appendChild(node);
617

    
618
			if(this.type!=null) {
619
				node.setAttribute("resourceTypeGeneral", this.type.toString());
620
			}
621
		}
622
	}
623

    
624
	public static class Creator {
625
		public String name;
626
		public List<String> affiliations;
627

    
628
		public Creator() {
629
		}
630

    
631
		public Creator(String name) {
632
			this.name = name;
633
		}
634

    
635
		public Creator(String name, List<String> affiliations) {
636
			this.name = name;
637
			this.affiliations = affiliations;
638
		}
639

    
640
		public void toXml(Element parent){
641
			Element node = parent.getOwnerDocument().createElement("creator");
642
			parent.appendChild(node);
643

    
644
			if(this.name!=null) {
645
				Element creatorName = parent.getOwnerDocument().createElement("creatorName");
646
				node.appendChild(creatorName);
647
				creatorName.appendChild(parent.getOwnerDocument().createTextNode(this.name));
648
			}
649
			if(this.affiliations!=null) {
650
				for(String item : this.affiliations) {
651
					Element affiliation = parent.getOwnerDocument().createElement("affiliation");
652
					node.appendChild(affiliation);
653
					affiliation.appendChild(parent.getOwnerDocument().createTextNode(item));
654
				}
655
			}
656
		}
657
	}
658

    
659
	public static class Identifier {
660
		public enum IdentifierType {
661
			ARK, DOI, Handle, PURL, URN, URL
662
		}
663

    
664
		public String value;
665
		public IdentifierType type;
666

    
667
		public Identifier() {
668
		}
669

    
670
		public Identifier(IdentifierType type, String value) {
671
			this.type = type;
672
			this.value = value;
673
		}
674

    
675
		public void toXml(Element parent){
676
			Element node = parent.getOwnerDocument().createElement("identifier");
677
			parent.appendChild(node);
678

    
679
			node.setAttribute("identifierType", this.type.toString());
680
			if(this.value!=null) {
681
				node.appendChild(parent.getOwnerDocument().createTextNode(this.value));
682
			}
683
		}
684
	}
685
}
(1-1/11)