-
Notifications
You must be signed in to change notification settings - Fork 12.9k
/
Copy pathGenericTokenParser.java.html
88 lines (84 loc) · 5.78 KB
/
GenericTokenParser.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
<?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>GenericTokenParser.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">GenericTokenParser.java</span></div><h1>GenericTokenParser.java</h1><pre class="source lang-java linenums">/*
* 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;
/**
* @author Clinton Begin
*/
public class GenericTokenParser {
private final String openToken;
private final String closeToken;
private final TokenHandler handler;
<span class="fc" id="L27"> public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {</span>
<span class="fc" id="L28"> this.openToken = openToken;</span>
<span class="fc" id="L29"> this.closeToken = closeToken;</span>
<span class="fc" id="L30"> this.handler = handler;</span>
<span class="fc" id="L31"> }</span>
public String parse(String text) {
<span class="pc bpc" id="L34" title="1 of 4 branches missed."> if (text == null || text.isEmpty()) {</span>
<span class="fc" id="L35"> return "";</span>
}
// search open token
<span class="fc" id="L38"> int start = text.indexOf(openToken);</span>
<span class="fc bfc" id="L39" title="All 2 branches covered."> if (start == -1) {</span>
<span class="fc" id="L40"> return text;</span>
}
<span class="fc" id="L42"> char[] src = text.toCharArray();</span>
<span class="fc" id="L43"> int offset = 0;</span>
<span class="fc" id="L44"> final StringBuilder builder = new StringBuilder();</span>
<span class="fc" id="L45"> StringBuilder expression = null;</span>
do {
<span class="fc bfc" id="L47" title="All 4 branches covered."> if (start > 0 && src[start - 1] == '\\') {</span>
// this open token is escaped. remove the backslash and continue.
<span class="fc" id="L49"> builder.append(src, offset, start - offset - 1).append(openToken);</span>
<span class="fc" id="L50"> offset = start + openToken.length();</span>
} else {
// found open token. let's search close token.
<span class="fc bfc" id="L53" title="All 2 branches covered."> if (expression == null) {</span>
<span class="fc" id="L54"> expression = new StringBuilder();</span>
} else {
<span class="fc" id="L56"> expression.setLength(0);</span>
}
<span class="fc" id="L58"> builder.append(src, offset, start - offset);</span>
<span class="fc" id="L59"> offset = start + openToken.length();</span>
<span class="fc" id="L60"> int end = text.indexOf(closeToken, offset);</span>
<span class="fc bfc" id="L61" title="All 2 branches covered."> while (end > -1) {</span>
<span class="fc bfc" id="L62" title="All 4 branches covered."> if ((end <= offset) || (src[end - 1] != '\\')) {</span>
<span class="fc" id="L63"> expression.append(src, offset, end - offset);</span>
<span class="fc" id="L64"> break;</span>
}
// this close token is escaped. remove the backslash and continue.
<span class="fc" id="L67"> expression.append(src, offset, end - offset - 1).append(closeToken);</span>
<span class="fc" id="L68"> offset = end + closeToken.length();</span>
<span class="fc" id="L69"> end = text.indexOf(closeToken, offset);</span>
}
<span class="fc bfc" id="L71" title="All 2 branches covered."> if (end == -1) {</span>
// close token was not found.
<span class="fc" id="L73"> builder.append(src, start, src.length - start);</span>
<span class="fc" id="L74"> offset = src.length;</span>
} else {
<span class="fc" id="L76"> builder.append(handler.handleToken(expression.toString()));</span>
<span class="fc" id="L77"> offset = end + closeToken.length();</span>
}
}
<span class="fc" id="L80"> start = text.indexOf(openToken, offset);</span>
<span class="fc bfc" id="L81" title="All 2 branches covered."> } while (start > -1);</span>
<span class="fc bfc" id="L82" title="All 2 branches covered."> if (offset < src.length) {</span>
<span class="fc" id="L83"> builder.append(src, offset, src.length - offset);</span>
}
<span class="fc" id="L85"> return builder.toString();</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>