1
|
package eu.dnetlib.data.transform.xml;
|
2
|
|
3
|
import java.util.*;
|
4
|
|
5
|
import com.google.common.collect.Maps;
|
6
|
import com.ximpleware.*;
|
7
|
|
8
|
public class VtdUtilityParser {
|
9
|
|
10
|
public static String xpath(final String ... p) {
|
11
|
return Arrays.stream(p)
|
12
|
.map(s -> String.format("/*[local-name()='%s']", s))
|
13
|
.reduce((s1, s2) -> s1 + s2)
|
14
|
.get();
|
15
|
}
|
16
|
|
17
|
public static VTDGen parseXml(final String xml) throws VtdException {
|
18
|
final VTDGen vg = new VTDGen();
|
19
|
vg.setDoc(xml.getBytes());
|
20
|
try {
|
21
|
vg.parse(true);
|
22
|
} catch (ParseException e) {
|
23
|
throw new VtdException(e);
|
24
|
}
|
25
|
return vg;
|
26
|
}
|
27
|
|
28
|
public static int countNodes(final AutoPilot ap, final VTDNav vn, final String xpath) throws VtdException {
|
29
|
try {
|
30
|
ap.selectXPath(xpath);
|
31
|
ap.bind(vn);
|
32
|
final Double i = ap.evalXPathToNumber();
|
33
|
return i.intValue();
|
34
|
} catch (XPathParseException e) {
|
35
|
throw new VtdException(e);
|
36
|
}
|
37
|
}
|
38
|
|
39
|
public static List<Node> getNodes(final AutoPilot ap, final VTDNav vn, final String xpath) throws VtdException {
|
40
|
final List<Node> results = new ArrayList<>();
|
41
|
try {
|
42
|
ap.selectXPath(xpath);
|
43
|
|
44
|
while (ap.evalXPath() != -1) {
|
45
|
final Node currentNode = new Node();
|
46
|
int t = vn.getText();
|
47
|
if (t >= 0) {
|
48
|
currentNode.setTextValue(vn.toNormalizedString(t));
|
49
|
}
|
50
|
final AutoPilot apAttr = new AutoPilot(vn);
|
51
|
apAttr.selectAttr("*");
|
52
|
Map<String, String> attributes = new HashMap<>();
|
53
|
int i;
|
54
|
while((i = apAttr.iterateAttr()) != -1){
|
55
|
attributes.put(vn.toNormalizedString(i),vn.toNormalizedString(i + 1) );
|
56
|
}
|
57
|
currentNode.setAttributes(attributes);
|
58
|
results.add(currentNode);
|
59
|
}
|
60
|
return results;
|
61
|
} catch (Exception e) {
|
62
|
throw new VtdException(e);
|
63
|
}
|
64
|
}
|
65
|
|
66
|
@Deprecated
|
67
|
public static List<Node> getTextValuesWithAttributes(final AutoPilot ap, final VTDNav vn, final String xpath, final List<String> attributes)
|
68
|
throws VtdException {
|
69
|
final List<Node> results = new ArrayList<>();
|
70
|
try {
|
71
|
ap.selectXPath(xpath);
|
72
|
|
73
|
while (ap.evalXPath() != -1) {
|
74
|
final Node currentNode = new Node();
|
75
|
int t = vn.getText();
|
76
|
if (t >= 0) {
|
77
|
currentNode.setTextValue(vn.toNormalizedString(t));
|
78
|
}
|
79
|
currentNode.setAttributes(getAttributes(vn, attributes));
|
80
|
results.add(currentNode);
|
81
|
}
|
82
|
return results;
|
83
|
} catch (Exception e) {
|
84
|
throw new VtdException(e);
|
85
|
}
|
86
|
}
|
87
|
|
88
|
private static final Map<String, String> getAttributes(final VTDNav vn, final List<String> attributes) {
|
89
|
final Map<String, String> currentAttributes = Maps.newHashMap();
|
90
|
if (attributes != null) {
|
91
|
|
92
|
attributes.forEach(attributeKey -> {
|
93
|
try {
|
94
|
int attr = vn.getAttrVal(attributeKey);
|
95
|
if (attr > -1) {
|
96
|
currentAttributes.put(attributeKey, vn.toNormalizedString(attr));
|
97
|
}
|
98
|
} catch (Throwable e) {
|
99
|
throw new RuntimeException(e);
|
100
|
}
|
101
|
});
|
102
|
}
|
103
|
return currentAttributes;
|
104
|
}
|
105
|
|
106
|
public static List<String> getTextValue(final AutoPilot ap, final VTDNav vn, final String xpath) throws VtdException {
|
107
|
List<String> results = new ArrayList<>();
|
108
|
try {
|
109
|
ap.selectXPath(xpath);
|
110
|
while (ap.evalXPath() != -1) {
|
111
|
int t = vn.getText();
|
112
|
if (t > -1) results.add(vn.toNormalizedString(t));
|
113
|
}
|
114
|
return results;
|
115
|
} catch (Exception e) {
|
116
|
throw new VtdException(e);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
public static String getFirstValue(final AutoPilot ap, final VTDNav nav, final String xpath) throws VtdException {
|
121
|
try {
|
122
|
ap.selectXPath(xpath);
|
123
|
while (ap.evalXPath() != -1) {
|
124
|
int it = nav.getText();
|
125
|
if (it > -1)
|
126
|
return nav.toNormalizedString(it);
|
127
|
}
|
128
|
return null;
|
129
|
} catch (Exception e) {
|
130
|
throw new VtdException(e);
|
131
|
}
|
132
|
}
|
133
|
|
134
|
@Deprecated
|
135
|
public static String getSingleValue(final AutoPilot ap, final VTDNav nav, final String xpath) throws VtdException {
|
136
|
return getFirstValue(ap, nav, xpath);
|
137
|
}
|
138
|
|
139
|
public static class Node {
|
140
|
|
141
|
private String textValue;
|
142
|
|
143
|
private Map<String, String> attributes;
|
144
|
|
145
|
public String getTextValue() {
|
146
|
return textValue;
|
147
|
}
|
148
|
|
149
|
public void setTextValue(final String textValue) {
|
150
|
this.textValue = textValue;
|
151
|
}
|
152
|
|
153
|
public Map<String, String> getAttributes() {
|
154
|
return attributes;
|
155
|
}
|
156
|
|
157
|
public void setAttributes(final Map<String, String> attributes) {
|
158
|
this.attributes = attributes;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
}
|