1
|
package eu.dnetlib.resolver.parser;
|
2
|
|
3
|
import com.google.common.collect.Lists;
|
4
|
import com.ximpleware.AutoPilot;
|
5
|
import com.ximpleware.VTDGen;
|
6
|
import com.ximpleware.VTDNav;
|
7
|
import eu.dnetlib.data.transform.VtdUtilityParser;
|
8
|
import eu.dnetlib.data.transform.VtdUtilityParser.Node;
|
9
|
import eu.dnetlib.dli.DLIUtils;
|
10
|
import eu.dnetlib.dli.resolver.model.*;
|
11
|
import eu.dnetlib.pid.resolver.model.ObjectRelation;
|
12
|
import eu.dnetlib.pid.resolver.model.ObjectType;
|
13
|
import eu.dnetlib.pid.resolver.model.PID;
|
14
|
import eu.dnetlib.pid.resolver.model.SubjectType;
|
15
|
import eu.dnetlib.pid.resolver.parser.AbstractResolverParser;
|
16
|
import org.apache.commons.lang3.StringUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
|
20
|
import java.util.ArrayList;
|
21
|
import java.util.Arrays;
|
22
|
import java.util.List;
|
23
|
|
24
|
/**
|
25
|
* Created by sandro on 9/13/16.
|
26
|
*/
|
27
|
public class DMFResolverParser extends AbstractResolverParser {
|
28
|
|
29
|
private static final Log log = LogFactory.getLog(DMFResolverParser.class);
|
30
|
|
31
|
@Override
|
32
|
public DLIResolvedObject parseObject(final String record) {
|
33
|
try {
|
34
|
final DLIResolvedObject parsedObject = new DLIResolvedObject();
|
35
|
final VTDGen vg = new VTDGen();
|
36
|
vg.setDoc(record.getBytes());
|
37
|
vg.parse(true);
|
38
|
|
39
|
final VTDNav vn = vg.getNav();
|
40
|
final AutoPilot ap = new AutoPilot(vn);
|
41
|
ap.declareXPathNameSpace("dri", "http://www.driver-repository.eu/namespace/dri");
|
42
|
|
43
|
final String resolvedDate = VtdUtilityParser.getSingleValue(ap, vn, "//dri:resolvedDate");
|
44
|
parsedObject.setResolvedDate(resolvedDate);
|
45
|
|
46
|
final String datasourcePrefix = VtdUtilityParser.getSingleValue(ap, vn, "//dri:datasourceprefix");
|
47
|
ap.declareXPathNameSpace("oaf", "http://namespace.dnet.eu/oaf");
|
48
|
|
49
|
final String completionStatus = VtdUtilityParser.getSingleValue(ap, vn, "//oaf:completionStatus");
|
50
|
final String provisionMode = VtdUtilityParser.getSingleValue(ap, vn, "//oaf:provisionMode");
|
51
|
|
52
|
final String publisher = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='resource']/*[local-name()='publisher']");
|
53
|
|
54
|
|
55
|
final DLIObjectProvenance provenance = new DLIObjectProvenance();
|
56
|
provenance.setDatasourceId(DLIUtils.getIdFromDataSourcePrefix(datasourcePrefix));
|
57
|
provenance.setDatasource(DLIUtils.getNameFromDataSourcePrefix(datasourcePrefix));
|
58
|
provenance.setCompletionStatus(completionStatus);
|
59
|
if (!StringUtils.isEmpty(publisher))
|
60
|
provenance.setPublisher(publisher);
|
61
|
provenance.setProvisionMode(provisionMode);
|
62
|
parsedObject.setDatasourceProvenance(Lists.newArrayList(provenance));
|
63
|
|
64
|
ap.declareXPathNameSpace("datacite", "http://datacite.org/schema/kernel-3");
|
65
|
final List<Node> identifierType =
|
66
|
VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']/*[local-name()='identifier']", Lists.newArrayList("identifierType"));
|
67
|
|
68
|
if (extractIdentifier(parsedObject, identifierType)) return null;
|
69
|
|
70
|
final List<Node> relations =
|
71
|
VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']//*[local-name()='relatedIdentifier']", Arrays.asList("relatedIdentifierType", "relationType", "inverseRelationType"));
|
72
|
|
73
|
if (relations != null && relations.size() > 0) {
|
74
|
final List<ObjectRelation> relationsResult = new ArrayList<>();
|
75
|
relations.forEach(relationMap -> {
|
76
|
final String relationType = relationMap.getAttributes().get("relationType");
|
77
|
final String inverseRelationType = relationMap.getAttributes().get("inverseRelationType");
|
78
|
final String relatedIdentifierType = relationMap.getAttributes().get("relatedIdentifierType");
|
79
|
final String relatedPid = relationMap.getTextValue();
|
80
|
final DLIObjectRelation currentRelation = new DLIObjectRelation();
|
81
|
currentRelation.setTargetPID(new PID(relatedPid, relatedIdentifierType));
|
82
|
currentRelation.setRelationSemantics(relationType);
|
83
|
currentRelation.setInverseRelation(inverseRelationType);
|
84
|
currentRelation.setCompletionStatus(CompletionStatus.incomplete.toString());
|
85
|
relationsResult.add(currentRelation);
|
86
|
});
|
87
|
parsedObject.setRelations(relationsResult);
|
88
|
}
|
89
|
|
90
|
final List<Node> subjects = VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']//*[local-name()='subject']", Arrays.asList("subjectScheme"));
|
91
|
|
92
|
extractSubject(parsedObject, subjects);
|
93
|
|
94
|
parsedObject.setCompletionStatus(completionStatus);
|
95
|
|
96
|
final List<String> creators = VtdUtilityParser.getTextValue(ap, vn, "//*[local-name()='resource']//*[local-name()='creator']/*[local-name()='creatorName']");
|
97
|
if (creators != null && creators.size() > 0) {
|
98
|
parsedObject.setAuthors(creators);
|
99
|
}
|
100
|
final List<String> titles = VtdUtilityParser.getTextValue(ap, vn, "//*[local-name()='resource']//*[local-name()='title']");
|
101
|
if (titles != null && titles.size() > 0) {
|
102
|
parsedObject.setTitles(titles);
|
103
|
}
|
104
|
|
105
|
List<Node> resourceTypeGeneral = VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']/*[local-name()='resourceType']", Arrays.asList("resourceTypeGeneral"));
|
106
|
|
107
|
|
108
|
if (resourceTypeGeneral != null && !resourceTypeGeneral.isEmpty()) {
|
109
|
final String type = resourceTypeGeneral.get(0).getAttributes().get("resourceTypeGeneral");
|
110
|
setType(parsedObject, type);
|
111
|
}
|
112
|
|
113
|
if (parsedObject.getType() == ObjectType.unknown) {
|
114
|
|
115
|
final String type = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='resource']/*[local-name()='resourceType']");
|
116
|
setType(parsedObject, type);
|
117
|
}
|
118
|
final List<String> dates = VtdUtilityParser.getTextValue(ap, vn, "//*[local-name()='resource']/*[local-name()='dates']/*[local-name()='date']");
|
119
|
|
120
|
|
121
|
if (dates != null && dates.size() > 0) {
|
122
|
parsedObject.setDate(dates.get(0));
|
123
|
}
|
124
|
return parsedObject;
|
125
|
} catch (Throwable e) {
|
126
|
log.error("Error on parsing record " + record, e);
|
127
|
return null;
|
128
|
}
|
129
|
}
|
130
|
|
131
|
|
132
|
}
|
133
|
|