Revision 45492
Added by Claudio Atzori over 7 years ago
VtdUtilityParser.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.transform; |
2 | 2 |
|
3 | 3 |
import java.util.ArrayList; |
4 |
import java.util.HashMap; |
|
5 | 4 |
import java.util.List; |
6 | 5 |
import java.util.Map; |
7 | 6 |
|
7 |
import com.google.common.collect.Maps; |
|
8 | 8 |
import com.ximpleware.AutoPilot; |
9 | 9 |
import com.ximpleware.VTDNav; |
10 | 10 |
|
... | ... | |
14 | 14 |
public class VtdUtilityParser { |
15 | 15 |
|
16 | 16 |
public static List<Node> getTextValuesWithAttributes(final AutoPilot ap, final VTDNav vn, final String xpath, final List<String> attributes) |
17 |
throws Exception { |
|
18 |
List<Node> results = new ArrayList<>(); |
|
19 |
ap.selectXPath(xpath); |
|
20 |
while (ap.evalXPath() != -1) { |
|
21 |
final Node currentNode = new Node(); |
|
22 |
final Map<String, String> currentAttributes = new HashMap<>(); |
|
23 |
int t = vn.getText(); |
|
24 |
if (t >= 0) |
|
25 |
currentNode.setTextValue(vn.toNormalizedString(t)); |
|
17 |
throws VtdException { |
|
18 |
final List<Node> results = new ArrayList<>(); |
|
19 |
try { |
|
20 |
ap.selectXPath(xpath); |
|
26 | 21 |
|
27 |
attributes.forEach(attributeKey -> { |
|
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 -> { |
|
28 | 42 |
try { |
29 | 43 |
int attr = vn.getAttrVal(attributeKey); |
30 |
currentAttributes.put(attributeKey, vn.toNormalizedString(attr)); |
|
44 |
if (attr > -1) { |
|
45 |
currentAttributes.put(attributeKey, vn.toNormalizedString(attr)); |
|
46 |
} |
|
31 | 47 |
} catch (Throwable e) { |
32 |
|
|
48 |
throw new RuntimeException(e); |
|
33 | 49 |
} |
34 | 50 |
}); |
35 |
currentNode.setAttributes(currentAttributes); |
|
36 |
results.add(currentNode); |
|
37 | 51 |
} |
38 |
return results;
|
|
52 |
return currentAttributes;
|
|
39 | 53 |
} |
40 | 54 |
|
41 | 55 |
public static List<String> getTextValue(final AutoPilot ap, final VTDNav vn, final String xpath) throws VtdException { |
Also available in: Unified diff
fixed attribute parsing