代码拉取完成,页面将自动刷新
/**
* Copyright 2009-2019 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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
*
* 功能和流程:
* 1、传入openToken、closeToken、TokenHandler实现类的实例,进行初始化
* 2、通过传入openToken、closeToken来对传入内容进行匹配并生成expression
* 3、将expression传入自定义的TokenHandler处理类中,调用handleToken()方法返回解析出的值
*
*/
public class GenericTokenParser {
/**
* 开始的 Token 字符串
*/
private final String openToken;
/**
* 结束的 Token 字符串
*/
private final String closeToken;
/**
* 处理expression(通过token生成)的handler
*/
private final TokenHandler handler;
public GenericTokenParser(String openToken, String closeToken, TokenHandler handler) {
this.openToken = openToken;
this.closeToken = closeToken;
this.handler = handler;
}
public String parse(String text) {
if (text == null || text.isEmpty()) {
return "";
}
// 获取text中openToken的起始位置
int start = text.indexOf(openToken);
// 未找到则直接返回
if (start == -1) {
return text;
}
char[] src = text.toCharArray();
int offset = 0;
final StringBuilder builder = new StringBuilder();
StringBuilder expression = null;
while (start > -1) {
if (start > 0 && src[start - 1] == '\\') {
// this open token is escaped. remove the backslash and continue.
// 该openToken被转义,删除反斜杠并继续
builder.append(src, offset, start - offset - 1).append(openToken);
offset = start + openToken.length();
} else {
// found open token. let's search close token.
// 通过openToken来查找closeToken的位置
if (expression == null) {
expression = new StringBuilder();
} else {
// 初始化StringBuilder的长度,相当于清空其内容,实现复用
expression.setLength(0);
}
builder.append(src, offset, start - offset);
offset = start + openToken.length();
int end = text.indexOf(closeToken, offset);
while (end > -1) {
if (end > offset && src[end - 1] == '\\') {
// this close token is escaped. remove the backslash and continue.
// 该closeToken被转义,删除反斜杠并继续
expression.append(src, offset, end - offset - 1).append(closeToken);
offset = end + closeToken.length();
end = text.indexOf(closeToken, offset);
} else {
expression.append(src, offset, end - offset);
break;
}
}
if (end == -1) {
// close token was not found.
// 如果只有openToken而没有closeToken,那么直接将openToken连同后面的所有字符完全填充入builder中
builder.append(src, start, src.length - start);
offset = src.length;
} else {
// 将通过openToken和closeToken匹配生成的字符串expression交由TokenHandler进行处理
builder.append(handler.handleToken(expression.toString()));
offset = end + closeToken.length();
}
}
start = text.indexOf(openToken, offset);
}
// 匹配完所有token字符串之后,剩下不需要匹配的,则直接添加入builder中返回(如:#{test} is ok的is ok)
if (offset < src.length) {
builder.append(src, offset, src.length - offset);
}
return builder.toString();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。