Project

General

Profile

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.google.common.collect.Lists;
8
import com.ximpleware.AutoPilot;
9
import com.ximpleware.VTDGen;
10
import com.ximpleware.VTDNav;
11

    
12
import eu.dnetlib.dli.resolver.model.*;
13
import eu.dnetlib.resolver.parser.UtilityParser.Node;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16

    
17
/**
18
 * Created by sandro on 9/13/16.
19
 */
20
public class DMFResolverParser extends AbstractResolverParser {
21

    
22
	private static final Log log = LogFactory.getLog(DMFResolverParser.class);
23

    
24
	@Override
25
	public ResolvedObject parseObject(final String record) {
26
		try {
27
			final ResolvedObject parsedObject = new ResolvedObject();
28
			final VTDGen vg = new VTDGen();
29
			vg.setDoc(record.getBytes());
30
			vg.parse(true);
31

    
32
			final VTDNav vn = vg.getNav();
33
			final AutoPilot ap = new AutoPilot(vn);
34
			ap.declareXPathNameSpace("dri", "http://www.driver-repository.eu/namespace/dri");
35

    
36
			final String datasourcePrefix = UtilityParser.getSingleValue(ap, vn, "//dri:datasourceprefix");
37
			ap.declareXPathNameSpace("oaf", "http://namespace.dnet.eu/oaf");
38

    
39
			final String completionStatus = UtilityParser.getSingleValue(ap, vn, "//oaf:completionStatus");
40
			final String provisionMode = UtilityParser.getSingleValue(ap, vn, "//oaf:provisionMode");
41

    
42
			final ObjectProvenance provenance = new ObjectProvenance();
43
			provenance.setDatasourceId(datasourcePrefix);
44
			provenance.setDatasource(datasourcePrefix);
45
			provenance.setCompletionStatus(completionStatus);
46
			provenance.setProvisionMode(provisionMode);
47
			parsedObject.setDatasourceProvenance(Lists.newArrayList(provenance));
48

    
49
			ap.declareXPathNameSpace("datacite", "http://datacite.org/schema/kernel-3");
50
			final List<Node> identifierType =
51
					UtilityParser.getTextValuesWithAttributes(ap, vn, "//datacite:identifier", Lists.newArrayList("identifierType"));
52

    
53
			if (identifierType != null && identifierType.size() > 0) {
54

    
55
				final Node result = identifierType.get(0);
56
				parsedObject.setPid(result.getTextValue());
57
				parsedObject.setPidType(result.getAttributes().get("identifierType"));
58
			} else {
59
				log.debug("Error on parsing record the identifier should not null ");
60
				return null;
61
			}
62

    
63
			final List<Node> relations =
64
					UtilityParser.getTextValuesWithAttributes(ap, vn, "//datacite:relatedIdentifier", Arrays.asList("relatedIdentifierType", "relationType"));
65

    
66
			if (relations != null && relations.size() > 0) {
67
				final List<ObjectRelation> relationsResult = new ArrayList<>();
68
				relations.forEach(relationMap -> {
69
					final String relationType = relationMap.getAttributes().get("relationType");
70
                    final String inverseRelationType = relationMap.getAttributes().get("inverseRelationType");
71
                    final String relatedIdentifierType = relationMap.getAttributes().get("relatedIdentifierType");
72
					final String relatedPid = relationMap.getTextValue();
73
					final ObjectRelation currentRelation = new ObjectRelation();
74
					currentRelation.setTargetPID(new PID(relatedPid, relatedIdentifierType));
75
					currentRelation.setRelationSemantics(relationType);
76
                    currentRelation.setInverseRelation(inverseRelationType);
77
                    currentRelation.setCompletionStatus(CompletionStatus.incomplete.toString());
78
					relationsResult.add(currentRelation);
79
				});
80
				parsedObject.setRelations(relationsResult);
81
			}
82

    
83
			final List<Node> subjects = UtilityParser.getTextValuesWithAttributes(ap, vn, "//datacite:subject", Arrays.asList("subjectScheme"));
84

    
85
			if (subjects != null && subjects.size() > 0) {
86
				final List<SubjectType> subjectResult = new ArrayList<>();
87
				subjects.forEach(subjectMap -> {
88
					final SubjectType subject = new SubjectType(subjectMap.getAttributes().get("subjectScheme"), subjectMap.getTextValue());
89
					subjectResult.add(subject);
90
				});
91
				parsedObject.setSubjects(subjectResult);
92
			}
93

    
94
			parsedObject.setCompletionStatus(completionStatus);
95

    
96
			final List<String> creators = UtilityParser.getTextValue(ap, vn, "//datacite:creator/datacite:creatorName");
97
			if (creators != null && creators.size() > 0) {
98
				parsedObject.setAuthors(creators);
99
			}
100
			final List<String> titles = UtilityParser.getTextValue(ap, vn, "//datacite:title");
101
			if (titles != null && titles.size() > 0) {
102
				parsedObject.setTitles(titles);
103
			}
104
			final String type = UtilityParser.getSingleValue(ap, vn, "//datacite:resourceType");
105

    
106
			setType(parsedObject, type);
107

    
108
			final List<String> dates = UtilityParser.getTextValue(ap, vn, "//datacite:dates/date");
109

    
110
			if (dates != null && dates.size() > 0) {
111
				parsedObject.setDate(dates.get(0));
112
			}
113
			return parsedObject;
114
		} catch (Throwable e) {
115
			log.error("Error on parsing record " + record, e);
116
			return null;
117
		}
118
	}
119

    
120

    
121

    
122
}
123

    
(3-3/6)