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