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
|
|
42
|
final String resolvedDate = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='resolvedDate']");
|
43
|
parsedObject.setResolvedDate(resolvedDate);
|
44
|
|
45
|
final String datasourcePrefix = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='datasourceprefix']");
|
46
|
|
47
|
final String completionStatus = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='completionStatus']");
|
48
|
final String provisionMode = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='provisionMode']");
|
49
|
|
50
|
final String publisher = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='resource']/*[local-name()='publisher']");
|
51
|
|
52
|
|
53
|
final DLIObjectProvenance provenance = new DLIObjectProvenance();
|
54
|
provenance.setDatasourceId(DLIUtils.getIdFromDataSourcePrefix(datasourcePrefix));
|
55
|
provenance.setDatasource(DLIUtils.getNameFromDataSourcePrefix(datasourcePrefix));
|
56
|
provenance.setCompletionStatus(completionStatus);
|
57
|
if (!StringUtils.isEmpty(publisher))
|
58
|
provenance.setPublisher(publisher);
|
59
|
provenance.setProvisionMode(provisionMode);
|
60
|
parsedObject.setDatasourceProvenance(Lists.newArrayList(provenance));
|
61
|
|
62
|
|
63
|
final List<Node> identifierType =
|
64
|
VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']/*[local-name()='identifier']", Lists.newArrayList("identifierType"));
|
65
|
|
66
|
if (extractIdentifier(parsedObject, identifierType)) return null;
|
67
|
|
68
|
final List<Node> relations =
|
69
|
VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']//*[local-name()='relatedIdentifier']", Arrays.asList("relatedIdentifierType", "relationType", "inverseRelationType"));
|
70
|
|
71
|
if (relations != null && relations.size() > 0) {
|
72
|
final List<ObjectRelation> relationsResult = new ArrayList<>();
|
73
|
relations.forEach(relationMap -> {
|
74
|
final String relationType = relationMap.getAttributes().get("relationType");
|
75
|
final String inverseRelationType = relationMap.getAttributes().get("inverseRelationType");
|
76
|
final String relatedIdentifierType = relationMap.getAttributes().get("relatedIdentifierType");
|
77
|
final String relatedPid = relationMap.getTextValue();
|
78
|
final DLIObjectRelation currentRelation = new DLIObjectRelation();
|
79
|
currentRelation.setTargetPID(new PID(relatedPid, relatedIdentifierType));
|
80
|
currentRelation.setRelationSemantics(relationType);
|
81
|
currentRelation.setInverseRelation(inverseRelationType);
|
82
|
currentRelation.setCompletionStatus(CompletionStatus.incomplete.toString());
|
83
|
relationsResult.add(currentRelation);
|
84
|
});
|
85
|
parsedObject.setRelations(relationsResult);
|
86
|
}
|
87
|
|
88
|
final List<Node> subjects = VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']//*[local-name()='subject']", Arrays.asList("subjectScheme"));
|
89
|
|
90
|
extractSubject(parsedObject, subjects);
|
91
|
|
92
|
parsedObject.setCompletionStatus(completionStatus);
|
93
|
|
94
|
final List<String> creators = VtdUtilityParser.getTextValue(ap, vn, "//*[local-name()='resource']//*[local-name()='creator']/*[local-name()='creatorName']");
|
95
|
if (creators != null && creators.size() > 0) {
|
96
|
parsedObject.setAuthors(creators);
|
97
|
}
|
98
|
final List<String> titles = VtdUtilityParser.getTextValue(ap, vn, "//*[local-name()='resource']//*[local-name()='title']");
|
99
|
if (titles != null && titles.size() > 0) {
|
100
|
parsedObject.setTitles(titles);
|
101
|
}
|
102
|
|
103
|
List<Node> resourceTypeGeneral = VtdUtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='resource']/*[local-name()='resourceType']", Arrays.asList("resourceTypeGeneral"));
|
104
|
|
105
|
|
106
|
if (resourceTypeGeneral != null && !resourceTypeGeneral.isEmpty()) {
|
107
|
final String type = resourceTypeGeneral.get(0).getAttributes().get("resourceTypeGeneral");
|
108
|
setType(parsedObject, type);
|
109
|
}
|
110
|
|
111
|
if (parsedObject.getType() == ObjectType.unknown) {
|
112
|
|
113
|
final String type = VtdUtilityParser.getSingleValue(ap, vn, "//*[local-name()='resource']/*[local-name()='resourceType']");
|
114
|
setType(parsedObject, type);
|
115
|
}
|
116
|
final List<String> dates = VtdUtilityParser.getTextValue(ap, vn, "//*[local-name()='resource']/*[local-name()='dates']/*[local-name()='date']");
|
117
|
|
118
|
|
119
|
if (dates != null && dates.size() > 0) {
|
120
|
parsedObject.setDate(dates.get(0));
|
121
|
}
|
122
|
return parsedObject;
|
123
|
} catch (Throwable e) {
|
124
|
log.error("Error on parsing record " + record, e);
|
125
|
return null;
|
126
|
}
|
127
|
}
|
128
|
|
129
|
|
130
|
}
|
131
|
|