1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.hostedby;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.dom4j.Document;
|
10
|
import org.dom4j.Element;
|
11
|
import org.dom4j.Node;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
|
14
|
/**
|
15
|
* The Class PatchHostedBy.
|
16
|
*/
|
17
|
public class PatchHostedBy implements UnaryFunction<String, String> {
|
18
|
|
19
|
/**
|
20
|
* The set spec hosted by map.
|
21
|
*/
|
22
|
private Map<String, HostedByEntry> setSpecHostedByMap;
|
23
|
|
24
|
/**
|
25
|
* The counters.
|
26
|
*/
|
27
|
private HostedByCounters counters;
|
28
|
|
29
|
/**
|
30
|
* The xpath.
|
31
|
*/
|
32
|
private String xpath;
|
33
|
|
34
|
/**
|
35
|
* The reader.
|
36
|
*/
|
37
|
private final SAXReader reader = new SAXReader();
|
38
|
|
39
|
/**
|
40
|
* The Constant log.
|
41
|
*/
|
42
|
private static final Log log = LogFactory.getLog(PatchHostedBy.class);
|
43
|
|
44
|
/**
|
45
|
* Instantiates a new patch hosted by.
|
46
|
*
|
47
|
* @param setSpecHostedByMap the set spec hosted by map
|
48
|
* @param xpath the xpath
|
49
|
* @param counters the counters
|
50
|
*/
|
51
|
public PatchHostedBy(final Map<String, HostedByEntry> setSpecHostedByMap, final String xpath, final HostedByCounters counters) {
|
52
|
this.setSpecHostedByMap = setSpecHostedByMap;
|
53
|
this.xpath = xpath;
|
54
|
this.counters = counters;
|
55
|
if (log.isDebugEnabled()) {
|
56
|
log.debug("****************************************");
|
57
|
log.debug("SetSpec/hostedBy map:");
|
58
|
for (Map.Entry<String, HostedByEntry> e : setSpecHostedByMap.entrySet()) {
|
59
|
log.debug(" " + e.getKey() + " -> " + e.getValue());
|
60
|
}
|
61
|
log.debug("****************************************");
|
62
|
}
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Evaluate.
|
67
|
*
|
68
|
* @param record the record
|
69
|
* @return the string
|
70
|
*/
|
71
|
@Override
|
72
|
public String evaluate(final String record) {
|
73
|
try {
|
74
|
final Document doc = reader.read(new StringReader(record));
|
75
|
final Element node = (Element) doc.selectSingleNode("//*[local-name()='metadata']/*[local-name()='hostedBy']");
|
76
|
if (node != null) {
|
77
|
final HostedByEntry ds = findHostedBy(doc);
|
78
|
if (ds != null) {
|
79
|
node.addAttribute("id", ds.getId());
|
80
|
node.addAttribute("name", ds.getName());
|
81
|
counters.increaseCounter(ds.getId());
|
82
|
}
|
83
|
return doc.asXML();
|
84
|
} else if (log.isDebugEnabled()) {
|
85
|
log.debug(" -- Missing hostedBy --");
|
86
|
}
|
87
|
} catch (Throwable e) {
|
88
|
log.error("Error adding hosted by to " + e.getMessage()+" on record "+record);
|
89
|
}
|
90
|
return record;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Find hosted by.
|
95
|
*
|
96
|
* @param doc the doc
|
97
|
* @return the hosted by entry
|
98
|
*/
|
99
|
private HostedByEntry findHostedBy(final Document doc) {
|
100
|
for (Object o : doc.selectNodes(this.xpath)) {
|
101
|
final String set = ((Node) o).getText().trim();
|
102
|
if (setSpecHostedByMap.containsKey(set)) {
|
103
|
if (log.isDebugEnabled()) {
|
104
|
log.debug(set + " -> " + setSpecHostedByMap.get(set));
|
105
|
}
|
106
|
return setSpecHostedByMap.get(set);
|
107
|
} else if (log.isDebugEnabled()) {
|
108
|
log.debug(set + " -> UNKNOWN REPO");
|
109
|
}
|
110
|
}
|
111
|
return null;
|
112
|
}
|
113
|
|
114
|
}
|