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