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