Revision 52048
Added by Claudio Atzori almost 6 years ago
VtdUtilityParser.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.util.*; |
4 | 4 |
|
5 |
import com.google.common.collect.Maps; |
|
6 | 5 |
import com.ximpleware.*; |
7 | 6 |
import org.apache.commons.lang3.StringUtils; |
8 | 7 |
|
9 | 8 |
public class VtdUtilityParser { |
10 | 9 |
|
10 |
public static final String NS_SEPARATOR = ":"; |
|
11 |
|
|
11 | 12 |
public static String xpath(final String ... p) { |
12 | 13 |
return Arrays.stream(p) |
13 | 14 |
.map(s -> String.format("/*[local-name()='%s']", s)) |
... | ... | |
48 | 49 |
ap.selectXPath(xpath); |
49 | 50 |
|
50 | 51 |
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; |
|
52 |
return asNode(vn); |
|
65 | 53 |
} |
66 | 54 |
|
67 | 55 |
return null; |
... | ... | |
79 | 67 |
ap.selectXPath(xpath); |
80 | 68 |
|
81 | 69 |
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); |
|
70 |
results.add(asNode(vn)); |
|
96 | 71 |
} |
97 | 72 |
return results; |
98 | 73 |
} catch (Exception e) { |
... | ... | |
100 | 75 |
} |
101 | 76 |
} |
102 | 77 |
|
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; |
|
78 |
private static Node asNode(final VTDNav vn) throws NavException { |
|
79 |
final Node currentNode = new Node(); |
|
80 |
int t = vn.getText(); |
|
81 |
if (t >= 0) { |
|
82 |
final String name = vn.toRawString(vn.getCurrentIndex()); |
|
83 |
currentNode.setName(name.contains(NS_SEPARATOR) ? StringUtils.substringAfter(name, NS_SEPARATOR) : name); |
|
84 |
currentNode.setTextValue(vn.toNormalizedString(t)); |
|
109 | 85 |
} |
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 |
} |
|
86 |
currentNode.setAttributes(getAttributes(vn)); |
|
87 |
return currentNode; |
|
126 | 88 |
} |
127 | 89 |
|
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 |
}); |
|
90 |
private static Map<String, String> getAttributes(final VTDNav vn) throws NavException { |
|
91 |
final AutoPilot ap = new AutoPilot(vn); |
|
92 |
ap.selectAttr("*"); |
|
93 |
Map<String, String> attributes = new HashMap<>(); |
|
94 |
int i; |
|
95 |
while((i = ap.iterateAttr()) != -1){ |
|
96 |
attributes.put(vn.toNormalizedString(i),vn.toNormalizedString(i + 1) ); |
|
143 | 97 |
} |
144 |
return res;
|
|
98 |
return attributes;
|
|
145 | 99 |
} |
146 | 100 |
|
147 | 101 |
public static List<String> getTextValue(final AutoPilot ap, final VTDNav vn, final String xpath) throws VtdException { |
... | ... | |
185 | 139 |
|
186 | 140 |
public static class Node { |
187 | 141 |
|
142 |
private String name; |
|
143 |
|
|
188 | 144 |
private String textValue; |
189 | 145 |
|
190 | 146 |
private Map<String, String> attributes; |
... | ... | |
204 | 160 |
public void setAttributes(final Map<String, String> attributes) { |
205 | 161 |
this.attributes = attributes; |
206 | 162 |
} |
163 |
|
|
164 |
public String getName() { |
|
165 |
return name; |
|
166 |
} |
|
167 |
|
|
168 |
public void setName(final String name) { |
|
169 |
this.name = name; |
|
170 |
} |
|
207 | 171 |
} |
208 | 172 |
|
209 | 173 |
} |
Also available in: Unified diff
VTD based publication and dataset parser