1
|
package eu.dnetlib.data.transform;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import com.google.common.collect.Maps;
|
8
|
import com.ximpleware.AutoPilot;
|
9
|
import com.ximpleware.VTDNav;
|
10
|
|
11
|
/**
|
12
|
* Created by sandro on 9/29/16.
|
13
|
*/
|
14
|
public class VtdUtilityParser {
|
15
|
|
16
|
public static List<Node> getTextValuesWithAttributes(final AutoPilot ap, final VTDNav vn, final String xpath, final List<String> attributes)
|
17
|
throws VtdException {
|
18
|
final List<Node> results = new ArrayList<>();
|
19
|
try {
|
20
|
ap.selectXPath(xpath);
|
21
|
|
22
|
while (ap.evalXPath() != -1) {
|
23
|
final Node currentNode = new Node();
|
24
|
int t = vn.getText();
|
25
|
if (t >= 0) {
|
26
|
currentNode.setTextValue(vn.toNormalizedString(t));
|
27
|
}
|
28
|
currentNode.setAttributes(getAttributes(vn, attributes));
|
29
|
results.add(currentNode);
|
30
|
}
|
31
|
return results;
|
32
|
} catch (Exception e) {
|
33
|
throw new VtdException(e);
|
34
|
}
|
35
|
}
|
36
|
|
37
|
private static final Map<String, String> getAttributes(final VTDNav vn, final List<String> attributes) {
|
38
|
final Map<String, String> currentAttributes = Maps.newHashMap();
|
39
|
if (attributes != null) {
|
40
|
|
41
|
attributes.forEach(attributeKey -> {
|
42
|
try {
|
43
|
int attr = vn.getAttrVal(attributeKey);
|
44
|
if (attr > -1) {
|
45
|
currentAttributes.put(attributeKey, vn.toNormalizedString(attr));
|
46
|
}
|
47
|
} catch (Throwable e) {
|
48
|
throw new RuntimeException(e);
|
49
|
}
|
50
|
});
|
51
|
}
|
52
|
return currentAttributes;
|
53
|
}
|
54
|
|
55
|
public static List<String> getTextValue(final AutoPilot ap, final VTDNav vn, final String xpath) throws VtdException {
|
56
|
List<String> results = new ArrayList<>();
|
57
|
try {
|
58
|
ap.selectXPath(xpath);
|
59
|
while (ap.evalXPath() != -1) {
|
60
|
int t = vn.getText();
|
61
|
if (t > -1) results.add(vn.toNormalizedString(t));
|
62
|
}
|
63
|
return results;
|
64
|
} catch (Exception e) {
|
65
|
throw new VtdException(e);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
public static String getSingleValue(final AutoPilot ap, final VTDNav nav, final String xpath) throws VtdException {
|
70
|
try {
|
71
|
ap.selectXPath(xpath);
|
72
|
while (ap.evalXPath() != -1) {
|
73
|
int it = nav.getText();
|
74
|
if (it > -1)
|
75
|
return nav.toNormalizedString(it);
|
76
|
}
|
77
|
return null;
|
78
|
} catch (Exception e) {
|
79
|
throw new VtdException(e);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
public static class Node {
|
84
|
|
85
|
private String textValue;
|
86
|
|
87
|
private Map<String, String> attributes;
|
88
|
|
89
|
public String getTextValue() {
|
90
|
return textValue;
|
91
|
}
|
92
|
|
93
|
public void setTextValue(final String textValue) {
|
94
|
this.textValue = textValue;
|
95
|
}
|
96
|
|
97
|
public Map<String, String> getAttributes() {
|
98
|
return attributes;
|
99
|
}
|
100
|
|
101
|
public void setAttributes(final Map<String, String> attributes) {
|
102
|
this.attributes = attributes;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
}
|