-
Notifications
You must be signed in to change notification settings - Fork 12.9k
/
Copy pathXNode.java
383 lines (328 loc) · 10.7 KB
/
XNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright 2009-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.parsing;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author Clinton Begin
*/
public class XNode {
private final Node node;
private final String name;
private final String body;
private final Properties attributes;
private final Properties variables;
private final XPathParser xpathParser;
public XNode(XPathParser xpathParser, Node node, Properties variables) {
this.xpathParser = xpathParser;
this.node = node;
this.name = node.getNodeName();
this.variables = variables;
this.attributes = parseAttributes(node);
this.body = parseBody(node);
}
public XNode newXNode(Node node) {
return new XNode(xpathParser, node, variables);
}
public XNode getParent() {
Node parent = node.getParentNode();
if (!(parent instanceof Element)) {
return null;
}
return new XNode(xpathParser, parent, variables);
}
public String getPath() {
StringBuilder builder = new StringBuilder();
Node current = node;
while (current instanceof Element) {
if (current != node) {
builder.insert(0, "/");
}
builder.insert(0, current.getNodeName());
current = current.getParentNode();
}
return builder.toString();
}
public String getValueBasedIdentifier() {
StringBuilder builder = new StringBuilder();
XNode current = this;
while (current != null) {
if (current != this) {
builder.insert(0, "_");
}
String value = current.getStringAttribute("id",
current.getStringAttribute("value", current.getStringAttribute("property", (String) null)));
if (value != null) {
value = value.replace('.', '_');
builder.insert(0, "]");
builder.insert(0, value);
builder.insert(0, "[");
}
builder.insert(0, current.getName());
current = current.getParent();
}
return builder.toString();
}
public String evalString(String expression) {
return xpathParser.evalString(node, expression);
}
public Boolean evalBoolean(String expression) {
return xpathParser.evalBoolean(node, expression);
}
public Double evalDouble(String expression) {
return xpathParser.evalDouble(node, expression);
}
public List<XNode> evalNodes(String expression) {
return xpathParser.evalNodes(node, expression);
}
public XNode evalNode(String expression) {
return xpathParser.evalNode(node, expression);
}
public Node getNode() {
return node;
}
public String getName() {
return name;
}
public String getStringBody() {
return getStringBody(null);
}
public String getStringBody(String def) {
return body == null ? def : body;
}
public Boolean getBooleanBody() {
return getBooleanBody(null);
}
public Boolean getBooleanBody(Boolean def) {
return body == null ? def : Boolean.valueOf(body);
}
public Integer getIntBody() {
return getIntBody(null);
}
public Integer getIntBody(Integer def) {
return body == null ? def : Integer.valueOf(body);
}
public Long getLongBody() {
return getLongBody(null);
}
public Long getLongBody(Long def) {
return body == null ? def : Long.valueOf(body);
}
public Double getDoubleBody() {
return getDoubleBody(null);
}
public Double getDoubleBody(Double def) {
return body == null ? def : Double.valueOf(body);
}
public Float getFloatBody() {
return getFloatBody(null);
}
public Float getFloatBody(Float def) {
return body == null ? def : Float.valueOf(body);
}
public <T extends Enum<T>> T getEnumAttribute(Class<T> enumType, String name) {
return getEnumAttribute(enumType, name, null);
}
public <T extends Enum<T>> T getEnumAttribute(Class<T> enumType, String name, T def) {
String value = getStringAttribute(name);
return value == null ? def : Enum.valueOf(enumType, value);
}
/**
* Return a attribute value as String.
* <p>
* If attribute value is absent, return value that provided from supplier of default value.
*
* @param name
* attribute name
* @param defSupplier
* a supplier of default value
*
* @return the string attribute
*
* @since 3.5.4
*/
public String getStringAttribute(String name, Supplier<String> defSupplier) {
String value = attributes.getProperty(name);
return value == null ? defSupplier.get() : value;
}
public String getStringAttribute(String name) {
return getStringAttribute(name, (String) null);
}
public String getStringAttribute(String name, String def) {
String value = attributes.getProperty(name);
return value == null ? def : value;
}
public Boolean getBooleanAttribute(String name) {
return getBooleanAttribute(name, null);
}
public Boolean getBooleanAttribute(String name, Boolean def) {
String value = attributes.getProperty(name);
return value == null ? def : Boolean.valueOf(value);
}
public Integer getIntAttribute(String name) {
return getIntAttribute(name, null);
}
public Integer getIntAttribute(String name, Integer def) {
String value = attributes.getProperty(name);
return value == null ? def : Integer.valueOf(value);
}
public Long getLongAttribute(String name) {
return getLongAttribute(name, null);
}
public Long getLongAttribute(String name, Long def) {
String value = attributes.getProperty(name);
return value == null ? def : Long.valueOf(value);
}
public Double getDoubleAttribute(String name) {
return getDoubleAttribute(name, null);
}
public Double getDoubleAttribute(String name, Double def) {
String value = attributes.getProperty(name);
return value == null ? def : Double.valueOf(value);
}
public Float getFloatAttribute(String name) {
return getFloatAttribute(name, null);
}
public Float getFloatAttribute(String name, Float def) {
String value = attributes.getProperty(name);
return value == null ? def : Float.valueOf(value);
}
public List<XNode> getChildren() {
List<XNode> children = new ArrayList<>();
NodeList nodeList = node.getChildNodes();
if (nodeList != null) {
for (int i = 0, n = nodeList.getLength(); i < n; i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
children.add(new XNode(xpathParser, node, variables));
}
}
}
return children;
}
public Properties getChildrenAsProperties() {
Properties properties = new Properties();
for (XNode child : getChildren()) {
String name = child.getStringAttribute("name");
String value = child.getStringAttribute("value");
if (name != null && value != null) {
properties.setProperty(name, value);
}
}
return properties;
}
@Override
public String toString() {
return buildToString(new StringBuilder(), 0).toString();
}
private StringBuilder buildToString(StringBuilder builder, int indentLevel) {
indent(builder, indentLevel).append("<").append(name);
for (Map.Entry<Object, Object> entry : attributes.entrySet()) {
builder.append(" ");
builder.append(entry.getKey());
builder.append("=\"");
builder.append(entry.getValue());
builder.append("\"");
}
NodeList nodeList = node.getChildNodes();
if (nodeList == null || nodeList.getLength() == 0) {
builder.append(" />\n");
} else {
builder.append(">\n");
for (int i = 0, n = nodeList.getLength(); i < n; i++) {
Node node = nodeList.item(i);
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
new XNode(xpathParser, node, variables).buildToString(builder, indentLevel + 1);
} else {
String text = getBodyData(node).trim();
if (text.length() > 0) {
indent(builder, indentLevel + 1).append(text).append("\n");
}
}
}
indent(builder, indentLevel).append("</").append(name).append(">\n");
}
return builder;
}
private StringBuilder indent(StringBuilder builder, int level) {
for (int i = 0; i < level; i++) {
builder.append(" ");
}
return builder;
}
private Properties parseAttributes(Node n) {
Properties attributes = new Properties();
NamedNodeMap attributeNodes = n.getAttributes();
if (attributeNodes != null) {
for (int i = 0; i < attributeNodes.getLength(); i++) {
Node attribute = attributeNodes.item(i);
String value = PropertyParser.parse(attribute.getNodeValue(), variables);
attributes.put(attribute.getNodeName(), value);
}
}
return attributes;
}
private String parseBody(Node node) {
String data = getBodyData(node);
if (data == null) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
data = getBodyData(child);
if (data != null) {
break;
}
}
}
return data;
}
private String getBodyData(Node child) {
if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) {
String data = ((CharacterData) child).getData();
return PropertyParser.parse(data, variables);
}
return null;
}
/**
* Builds text representation of this node, which can be persisted as key and maps the SQLNode.
* So that, subsequent references of this Xnode will avoid building SQLNodes
* @return string
*/
public String toStringWithContent() {
StringBuilder builder = new StringBuilder();
toStringWithContent(builder);
return builder.toString();
}
private void toStringWithContent(StringBuilder builder) {
builder.append(toString());
Node clonedNode = getNode().cloneNode(true);
builder.append("<textContent>");
builder.append(clonedNode.getTextContent());
builder.append("<textContent>");
builder.append("<body>");
builder.append(body);
builder.append("<body>");
}
}