-
Notifications
You must be signed in to change notification settings - Fork 12.9k
/
Copy pathXNode.java.html
363 lines (309 loc) · 17.5 KB
/
XNode.java.html
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
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang=""><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>XNode.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">mybatis</a> > <a href="index.source.html" class="el_package">org.apache.ibatis.parsing</a> > <span class="el_source">XNode.java</span></div><h1>XNode.java</h1><pre class="source lang-java linenums">/*
* Copyright 2009-2024 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;
<span class="fc" id="L42"> public XNode(XPathParser xpathParser, Node node, Properties variables) {</span>
<span class="fc" id="L43"> this.xpathParser = xpathParser;</span>
<span class="fc" id="L44"> this.node = node;</span>
<span class="fc" id="L45"> this.name = node.getNodeName();</span>
<span class="fc" id="L46"> this.variables = variables;</span>
<span class="fc" id="L47"> this.attributes = parseAttributes(node);</span>
<span class="fc" id="L48"> this.body = parseBody(node);</span>
<span class="fc" id="L49"> }</span>
public XNode newXNode(Node node) {
<span class="fc" id="L52"> return new XNode(xpathParser, node, variables);</span>
}
public XNode getParent() {
<span class="fc" id="L56"> Node parent = node.getParentNode();</span>
<span class="fc bfc" id="L57" title="All 2 branches covered."> if (!(parent instanceof Element)) {</span>
<span class="fc" id="L58"> return null;</span>
}
<span class="fc" id="L60"> return new XNode(xpathParser, parent, variables);</span>
}
public String getPath() {
<span class="fc" id="L64"> StringBuilder builder = new StringBuilder();</span>
<span class="fc" id="L65"> Node current = node;</span>
<span class="fc bfc" id="L66" title="All 2 branches covered."> while (current instanceof Element) {</span>
<span class="fc bfc" id="L67" title="All 2 branches covered."> if (current != node) {</span>
<span class="fc" id="L68"> builder.insert(0, "/");</span>
}
<span class="fc" id="L70"> builder.insert(0, current.getNodeName());</span>
<span class="fc" id="L71"> current = current.getParentNode();</span>
}
<span class="fc" id="L73"> return builder.toString();</span>
}
public String getValueBasedIdentifier() {
<span class="fc" id="L77"> StringBuilder builder = new StringBuilder();</span>
<span class="fc" id="L78"> XNode current = this;</span>
<span class="fc bfc" id="L79" title="All 2 branches covered."> while (current != null) {</span>
<span class="fc bfc" id="L80" title="All 2 branches covered."> if (current != this) {</span>
<span class="fc" id="L81"> builder.insert(0, "_");</span>
}
<span class="fc" id="L83"> String value = current.getStringAttribute("id",</span>
<span class="fc" id="L84"> current.getStringAttribute("value", current.getStringAttribute("property", (String) null)));</span>
<span class="fc bfc" id="L85" title="All 2 branches covered."> if (value != null) {</span>
<span class="fc" id="L86"> value = value.replace('.', '_');</span>
<span class="fc" id="L87"> builder.insert(0, "]");</span>
<span class="fc" id="L88"> builder.insert(0, value);</span>
<span class="fc" id="L89"> builder.insert(0, "[");</span>
}
<span class="fc" id="L91"> builder.insert(0, current.getName());</span>
<span class="fc" id="L92"> current = current.getParent();</span>
<span class="fc" id="L93"> }</span>
<span class="fc" id="L94"> return builder.toString();</span>
}
public String evalString(String expression) {
<span class="fc" id="L98"> return xpathParser.evalString(node, expression);</span>
}
public Boolean evalBoolean(String expression) {
<span class="fc" id="L102"> return xpathParser.evalBoolean(node, expression);</span>
}
public Double evalDouble(String expression) {
<span class="fc" id="L106"> return xpathParser.evalDouble(node, expression);</span>
}
public List<XNode> evalNodes(String expression) {
<span class="fc" id="L110"> return xpathParser.evalNodes(node, expression);</span>
}
public XNode evalNode(String expression) {
<span class="fc" id="L114"> return xpathParser.evalNode(node, expression);</span>
}
public Node getNode() {
<span class="fc" id="L118"> return node;</span>
}
public String getName() {
<span class="fc" id="L122"> return name;</span>
}
public String getStringBody() {
<span class="fc" id="L126"> return getStringBody(null);</span>
}
public String getStringBody(String def) {
<span class="pc bpc" id="L130" title="1 of 2 branches missed."> return body == null ? def : body;</span>
}
public Boolean getBooleanBody() {
<span class="fc" id="L134"> return getBooleanBody(null);</span>
}
public Boolean getBooleanBody(Boolean def) {
<span class="pc bpc" id="L138" title="1 of 2 branches missed."> return body == null ? def : Boolean.valueOf(body);</span>
}
public Integer getIntBody() {
<span class="fc" id="L142"> return getIntBody(null);</span>
}
public Integer getIntBody(Integer def) {
<span class="pc bpc" id="L146" title="1 of 2 branches missed."> return body == null ? def : Integer.valueOf(body);</span>
}
public Long getLongBody() {
<span class="fc" id="L150"> return getLongBody(null);</span>
}
public Long getLongBody(Long def) {
<span class="pc bpc" id="L154" title="1 of 2 branches missed."> return body == null ? def : Long.valueOf(body);</span>
}
public Double getDoubleBody() {
<span class="fc" id="L158"> return getDoubleBody(null);</span>
}
public Double getDoubleBody(Double def) {
<span class="pc bpc" id="L162" title="1 of 2 branches missed."> return body == null ? def : Double.valueOf(body);</span>
}
public Float getFloatBody() {
<span class="fc" id="L166"> return getFloatBody(null);</span>
}
public Float getFloatBody(Float def) {
<span class="pc bpc" id="L170" title="1 of 2 branches missed."> return body == null ? def : Float.valueOf(body);</span>
}
public <T extends Enum<T>> T getEnumAttribute(Class<T> enumType, String name) {
<span class="fc" id="L174"> return getEnumAttribute(enumType, name, null);</span>
}
public <T extends Enum<T>> T getEnumAttribute(Class<T> enumType, String name, T def) {
<span class="fc" id="L178"> String value = getStringAttribute(name);</span>
<span class="pc bpc" id="L179" title="1 of 2 branches missed."> return value == null ? def : Enum.valueOf(enumType, value);</span>
}
/**
* 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) {
<span class="fc" id="L197"> String value = attributes.getProperty(name);</span>
<span class="fc bfc" id="L198" title="All 2 branches covered."> return value == null ? defSupplier.get() : value;</span>
}
public String getStringAttribute(String name) {
<span class="fc" id="L202"> return getStringAttribute(name, (String) null);</span>
}
public String getStringAttribute(String name, String def) {
<span class="fc" id="L206"> String value = attributes.getProperty(name);</span>
<span class="fc bfc" id="L207" title="All 2 branches covered."> return value == null ? def : value;</span>
}
public Boolean getBooleanAttribute(String name) {
<span class="fc" id="L211"> return getBooleanAttribute(name, null);</span>
}
public Boolean getBooleanAttribute(String name, Boolean def) {
<span class="fc" id="L215"> String value = attributes.getProperty(name);</span>
<span class="fc bfc" id="L216" title="All 2 branches covered."> return value == null ? def : Boolean.valueOf(value);</span>
}
public Integer getIntAttribute(String name) {
<span class="fc" id="L220"> return getIntAttribute(name, null);</span>
}
public Integer getIntAttribute(String name, Integer def) {
<span class="fc" id="L224"> String value = attributes.getProperty(name);</span>
<span class="fc bfc" id="L225" title="All 2 branches covered."> return value == null ? def : Integer.valueOf(value);</span>
}
public Long getLongAttribute(String name) {
<span class="fc" id="L229"> return getLongAttribute(name, null);</span>
}
public Long getLongAttribute(String name, Long def) {
<span class="fc" id="L233"> String value = attributes.getProperty(name);</span>
<span class="fc bfc" id="L234" title="All 2 branches covered."> return value == null ? def : Long.valueOf(value);</span>
}
public Double getDoubleAttribute(String name) {
<span class="fc" id="L238"> return getDoubleAttribute(name, null);</span>
}
public Double getDoubleAttribute(String name, Double def) {
<span class="fc" id="L242"> String value = attributes.getProperty(name);</span>
<span class="pc bpc" id="L243" title="1 of 2 branches missed."> return value == null ? def : Double.valueOf(value);</span>
}
public Float getFloatAttribute(String name) {
<span class="fc" id="L247"> return getFloatAttribute(name, null);</span>
}
public Float getFloatAttribute(String name, Float def) {
<span class="fc" id="L251"> String value = attributes.getProperty(name);</span>
<span class="pc bpc" id="L252" title="1 of 2 branches missed."> return value == null ? def : Float.valueOf(value);</span>
}
public List<XNode> getChildren() {
<span class="fc" id="L256"> List<XNode> children = new ArrayList<>();</span>
<span class="fc" id="L257"> NodeList nodeList = node.getChildNodes();</span>
<span class="pc bpc" id="L258" title="1 of 2 branches missed."> if (nodeList != null) {</span>
<span class="fc bfc" id="L259" title="All 2 branches covered."> for (int i = 0, n = nodeList.getLength(); i < n; i++) {</span>
<span class="fc" id="L260"> Node node = nodeList.item(i);</span>
<span class="fc bfc" id="L261" title="All 2 branches covered."> if (node.getNodeType() == Node.ELEMENT_NODE) {</span>
<span class="fc" id="L262"> children.add(new XNode(xpathParser, node, variables));</span>
}
}
}
<span class="fc" id="L266"> return children;</span>
}
public Properties getChildrenAsProperties() {
<span class="fc" id="L270"> Properties properties = new Properties();</span>
<span class="fc bfc" id="L271" title="All 2 branches covered."> for (XNode child : getChildren()) {</span>
<span class="fc" id="L272"> String name = child.getStringAttribute("name");</span>
<span class="fc" id="L273"> String value = child.getStringAttribute("value");</span>
<span class="pc bpc" id="L274" title="2 of 4 branches missed."> if (name != null && value != null) {</span>
<span class="fc" id="L275"> properties.setProperty(name, value);</span>
}
<span class="fc" id="L277"> }</span>
<span class="fc" id="L278"> return properties;</span>
}
@Override
public String toString() {
<span class="fc" id="L283"> return buildToString(new StringBuilder(), 0).toString();</span>
}
private StringBuilder buildToString(StringBuilder builder, int indentLevel) {
<span class="fc" id="L287"> indent(builder, indentLevel).append("<").append(name);</span>
<span class="fc bfc" id="L288" title="All 2 branches covered."> for (Map.Entry<Object, Object> entry : attributes.entrySet()) {</span>
<span class="fc" id="L289"> builder.append(" ");</span>
<span class="fc" id="L290"> builder.append(entry.getKey());</span>
<span class="fc" id="L291"> builder.append("=\"");</span>
<span class="fc" id="L292"> builder.append(entry.getValue());</span>
<span class="fc" id="L293"> builder.append("\"");</span>
<span class="fc" id="L294"> }</span>
<span class="fc" id="L296"> NodeList nodeList = node.getChildNodes();</span>
<span class="pc bpc" id="L297" title="1 of 4 branches missed."> if (nodeList == null || nodeList.getLength() == 0) {</span>
<span class="fc" id="L298"> builder.append(" />\n");</span>
} else {
<span class="fc" id="L300"> builder.append(">\n");</span>
<span class="fc bfc" id="L301" title="All 2 branches covered."> for (int i = 0, n = nodeList.getLength(); i < n; i++) {</span>
<span class="fc" id="L302"> Node node = nodeList.item(i);</span>
<span class="fc" id="L303"> short nodeType = node.getNodeType();</span>
<span class="fc bfc" id="L304" title="All 2 branches covered."> if (nodeType == Node.ELEMENT_NODE) {</span>
<span class="fc" id="L305"> new XNode(xpathParser, node, variables).buildToString(builder, indentLevel + 1);</span>
} else {
<span class="fc" id="L307"> String text = getBodyData(node).trim();</span>
<span class="fc bfc" id="L308" title="All 2 branches covered."> if (text.length() > 0) {</span>
<span class="fc" id="L309"> indent(builder, indentLevel + 1).append(text).append("\n");</span>
}
}
}
<span class="fc" id="L313"> indent(builder, indentLevel).append("</").append(name).append(">\n");</span>
}
<span class="fc" id="L316"> return builder;</span>
}
private StringBuilder indent(StringBuilder builder, int level) {
<span class="fc bfc" id="L320" title="All 2 branches covered."> for (int i = 0; i < level; i++) {</span>
<span class="fc" id="L321"> builder.append(" ");</span>
}
<span class="fc" id="L323"> return builder;</span>
}
private Properties parseAttributes(Node n) {
<span class="fc" id="L327"> Properties attributes = new Properties();</span>
<span class="fc" id="L328"> NamedNodeMap attributeNodes = n.getAttributes();</span>
<span class="fc bfc" id="L329" title="All 2 branches covered."> if (attributeNodes != null) {</span>
<span class="fc bfc" id="L330" title="All 2 branches covered."> for (int i = 0; i < attributeNodes.getLength(); i++) {</span>
<span class="fc" id="L331"> Node attribute = attributeNodes.item(i);</span>
<span class="fc" id="L332"> String value = PropertyParser.parse(attribute.getNodeValue(), variables);</span>
<span class="fc" id="L333"> attributes.put(attribute.getNodeName(), value);</span>
}
}
<span class="fc" id="L336"> return attributes;</span>
}
private String parseBody(Node node) {
<span class="fc" id="L340"> String data = getBodyData(node);</span>
<span class="fc bfc" id="L341" title="All 2 branches covered."> if (data == null) {</span>
<span class="fc" id="L342"> NodeList children = node.getChildNodes();</span>
<span class="fc bfc" id="L343" title="All 2 branches covered."> for (int i = 0; i < children.getLength(); i++) {</span>
<span class="fc" id="L344"> Node child = children.item(i);</span>
<span class="fc" id="L345"> data = getBodyData(child);</span>
<span class="fc bfc" id="L346" title="All 2 branches covered."> if (data != null) {</span>
<span class="fc" id="L347"> break;</span>
}
}
}
<span class="fc" id="L351"> return data;</span>
}
private String getBodyData(Node child) {
<span class="fc bfc" id="L355" title="All 4 branches covered."> if (child.getNodeType() == Node.CDATA_SECTION_NODE || child.getNodeType() == Node.TEXT_NODE) {</span>
<span class="fc" id="L356"> String data = ((CharacterData) child).getData();</span>
<span class="fc" id="L357"> return PropertyParser.parse(data, variables);</span>
}
<span class="fc" id="L359"> return null;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.12.202403310830</span></div></body></html>