1
|
package eu.dnetlib.resolver.parser;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
|
7
|
import com.ximpleware.AutoPilot;
|
8
|
import com.ximpleware.VTDGen;
|
9
|
import com.ximpleware.VTDNav;
|
10
|
import eu.dnetlib.resolver.model.*;
|
11
|
import eu.dnetlib.resolver.parser.UtilityParser.Node;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
|
15
|
/**
|
16
|
* Created by sandro on 9/29/16.
|
17
|
*/
|
18
|
public class ScholixResolverParser extends AbstractResolverParser {
|
19
|
|
20
|
private static final Log log = LogFactory.getLog(ScholixResolverParser.class);
|
21
|
|
22
|
@Override
|
23
|
public ResolvedObject parseObject(final String record) {
|
24
|
|
25
|
try {
|
26
|
final ResolvedObject parsedObject = new ResolvedObject();
|
27
|
final VTDGen vg = new VTDGen();
|
28
|
vg.setDoc(record.getBytes());
|
29
|
vg.parse(true);
|
30
|
|
31
|
final VTDNav vn = vg.getNav();
|
32
|
final AutoPilot ap = new AutoPilot(vn);
|
33
|
ap.declareXPathNameSpace("dri", "http://www.driver-repository.eu/namespace/dri");
|
34
|
|
35
|
final String datasourcePrefix = UtilityParser.getSingleValue(ap, vn, "//dri:datasourceprefix");
|
36
|
ap.declareXPathNameSpace("oaf", "http://namespace.dnet.eu/oaf");
|
37
|
|
38
|
final String completionStatus = UtilityParser.getSingleValue(ap, vn, "//oaf:completionStatus");
|
39
|
final String provisionMode = UtilityParser.getSingleValue(ap, vn, "//oaf:provisionMode");
|
40
|
|
41
|
List<Node> provenances =
|
42
|
UtilityParser.getTextValuesWithAttributes(ap, vn, "//oaf:collectedFrom", Arrays.asList("completionStatus", "id", "name"));
|
43
|
|
44
|
final List<ObjectProvenance> provenanceList = new ArrayList<>();
|
45
|
if (provenances != null) {
|
46
|
provenances.forEach(it -> {
|
47
|
final ObjectProvenance provenance = new ObjectProvenance();
|
48
|
provenance.setDatasourceId(it.getAttributes().get("id"));
|
49
|
provenance.setDatasource(it.getAttributes().get("name"));
|
50
|
provenance.setCompletionStatus(it.getAttributes().get("completionStatus"));
|
51
|
provenance.setProvisionMode("collected");
|
52
|
provenanceList.add(provenance);
|
53
|
|
54
|
});
|
55
|
}
|
56
|
|
57
|
parsedObject.setDatasourceProvenance(provenanceList);
|
58
|
|
59
|
parsedObject.setCompletionStatus(completionStatus);
|
60
|
|
61
|
ap.declareXPathNameSpace("scholix", "http://www.scholix.org");
|
62
|
|
63
|
List<Node> sourcePid = UtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='source']/*[local-name()='pid']", Arrays.asList("type"));
|
64
|
|
65
|
if (sourcePid == null || sourcePid.size() == 0) {
|
66
|
return null;
|
67
|
}
|
68
|
List<Node> targetPid = UtilityParser.getTextValuesWithAttributes(ap, vn, "//*[local-name()='target']/*[local-name()='pid']", Arrays.asList("type"));
|
69
|
if (targetPid == null || targetPid.size() == 0) {
|
70
|
return null;
|
71
|
}
|
72
|
parsedObject.setPid(sourcePid.get(0).getTextValue());
|
73
|
parsedObject.setPidType(sourcePid.get(0).getAttributes().get("type"));
|
74
|
|
75
|
List<String> sourceTypes = UtilityParser.getTextValue(ap, vn, "//*[local-name()='source']/*[local-name()='type']");
|
76
|
|
77
|
List<String> targetTypes = UtilityParser.getTextValue(ap, vn, "//*[local-name()='target']/*[local-name()='type']");
|
78
|
|
79
|
if (sourceTypes != null && sourceTypes.size() > 0) {
|
80
|
parsedObject.setType(ObjectType.valueOf(sourceTypes.get(0)));
|
81
|
}
|
82
|
|
83
|
List<String> relationType = UtilityParser.getTextValue(ap, vn, "//*[local-name()='relationType']");
|
84
|
String relationSemantic = null;
|
85
|
|
86
|
if (relationType != null && relationType.size() > 0) {
|
87
|
relationSemantic = relationType.get(0);
|
88
|
}
|
89
|
|
90
|
if (sourceTypes != null && sourceTypes.size() > 0) {
|
91
|
parsedObject.setType(ObjectType.valueOf(sourceTypes.get(0)));
|
92
|
}
|
93
|
|
94
|
final List<ObjectRelation> relations = new ArrayList<>();
|
95
|
|
96
|
ObjectRelation relation = new ObjectRelation();
|
97
|
relation.setCompletionStatus(CompletionStatus.incomplete.toString());
|
98
|
relation.setRelationProvenance(parsedObject.getDatasourceProvenance());
|
99
|
relation.setRelationSemantics(relationSemantic);
|
100
|
relation.setTargetPID(new PID(targetPid.get(0).getTextValue(), targetPid.get(0).getAttributes().get("type")));
|
101
|
if (targetTypes != null || targetTypes.size() > 0)
|
102
|
relation.setTargetType(ObjectType.valueOf(targetTypes.get(0)));
|
103
|
relations.add(relation);
|
104
|
parsedObject.setRelations(relations);
|
105
|
|
106
|
return parsedObject;
|
107
|
} catch (Throwable e) {
|
108
|
log.error("Error on parsing parser");
|
109
|
}
|
110
|
return null;
|
111
|
}
|
112
|
}
|