Skip to content

Commit

Permalink
完善部分功能的配置功能
Browse files Browse the repository at this point in the history
  • Loading branch information
blinkfox committed Mar 31, 2022
1 parent d7f0fa2 commit eb5d27c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 16 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/blinkfox/fenix/config/FenixConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public class FenixConfig {
*/
protected String handlerLocations;

/**
* 采用带前缀的下划线转自定义 Bean 时需要移除的前缀集合的字符串,多个前缀字符串用逗号隔开.
*
* @since v2.7.0
*/
private String underscoreTransformerPrefix;

/**
* 所有 Fenix XML 文档的缓存 map.
*
Expand Down Expand Up @@ -355,6 +362,19 @@ public FenixConfig setHandlerLocations(String handlerLocations) {
return this;
}

/**
* 设置自定义的 {@link com.blinkfox.fenix.config.entity.TagHandler} 处理器实现的所在位置,
* 多个用逗号隔开,可以是目录也可以是具体的 java 或 class 文件路径.
*
* @param underscoreTransformerPrefix 自定义的前缀字符串,多个用逗号隔开.
* @return {@link FenixConfig} 实例自身
* @since v2.7.0
*/
public FenixConfig setUnderscoreTransformerPrefix(String underscoreTransformerPrefix) {
this.underscoreTransformerPrefix = underscoreTransformerPrefix;
return this;
}

/**
* 添加自定义标签和该 SQL 片段对应的 {@link TagHandler} 处理器实现的 class.
*
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/com/blinkfox/fenix/config/FenixConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import com.blinkfox.fenix.helper.ParseHelper;
import com.blinkfox.fenix.helper.StringHelper;
import com.blinkfox.fenix.helper.XmlNodeHelper;
import com.blinkfox.fenix.jpa.transformer.PrefixUnderscoreTransformer;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -86,7 +88,7 @@ public void initLoad(FenixConfig fenixConfig) {
new TaggerScanner().scan(this.fenixConfig.getHandlerLocations());

// 初次测试表达式引擎是否能够正确工作和打印 banner 信息.
this.testFirstEvaluate();
this.asyncTestFirstEvaluate();
this.printBanner();
}

Expand All @@ -100,14 +102,28 @@ private void initFenixConfig(FenixConfig fenixConfig) {
throw new FenixException("【Fenix 异常】初始化加载的 FenixConfig 配置信息实例为空,请检查!");
}

// 尝试根据配置的前缀来设置结果转换器的前缀.
this.trySetUnderscoreTransformerPrefix(fenixConfig.getUnderscoreTransformerPrefix());

// 扫描和缓存 Fenix XML 文件资源信息、扫描和配置自定义的 Fenix 标签处理器实例类.
String xmlLocations = fenixConfig.getXmlLocations();
fenixConfig.setXmlLocations(StringHelper.isBlank(xmlLocations) ? Const.DEFAULT_FENIX_XML_DIR : xmlLocations);
this.fenixConfig = fenixConfig;
}

private void trySetUnderscoreTransformerPrefix(String underscoreTransformerPrefix) {
if (StringHelper.isNotBlank(underscoreTransformerPrefix)) {
// 清空 Fenix 中的所有默认前缀.
Set<String> prefixSet = PrefixUnderscoreTransformer.getPrefixSet();
prefixSet.clear();
for (String prefix : underscoreTransformerPrefix.split(Const.COMMA)) {
prefixSet.add(prefix.trim());
}
}
}

/**
* 打印 Finix Banner.
* 打印 Fenix Banner.
*/
private void printBanner() {
if (this.fenixConfig.isPrintBanner()) {
Expand Down Expand Up @@ -171,12 +187,18 @@ public void clear() {
}

/**
* 测试第一次 MVEL 表达式的计算,会缓存 MVEL 相关准备工作,从而加快后续的 MVEL 执行.
* 异步测试第一次 MVEL 表达式的计算,会缓存 MVEL 相关准备工作,从而加快后续的 MVEL 执行.
*/
private void testFirstEvaluate() {
Map<String, Object> context = ParamWrapper.newInstance("foo", "hello").toMap();
ParseHelper.parseTemplate("@if{?foo != empty}Hello World!@end{}", context);
ParseHelper.parseExpressWithException("foo != empty", context);
private void asyncTestFirstEvaluate() {
CompletableFuture.runAsync(() -> {
try {
Map<String, Object> context = ParamWrapper.newInstance("foo", "hello").toMap();
ParseHelper.parseTemplate("@if{?foo != empty}Hello World!@end{}", context);
ParseHelper.parseExpressWithException("foo != empty", context);
} catch (Exception e) {
log.error("【Fenix 异常】初次测试执行 MVEL 表达式时异常!", e);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.blinkfox.fenix.jpa.transformer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import lombok.Getter;

/**
* 字段带有某些前缀且含有下划线风格的查询结果实体对象转换器.
Expand All @@ -17,14 +19,8 @@ public class PrefixUnderscoreTransformer extends UnderscoreTransformer {
/**
* 需要从字段属性中去除的前缀集合.
*/
private static final Set<String> prefixSet = new HashSet<>();

static {
prefixSet.add("c_");
prefixSet.add("n_");
prefixSet.add("d_");
prefixSet.add("dt_");
}
@Getter
private static final Set<String> prefixSet = new HashSet<>(Arrays.asList("c_", "n_", "d_", "dt_"));

/**
* 将原来的字符串名称转换为小驼峰风格的 Java 属性名称.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public class FenixXmlBuilderTest {
public static void init() {
FenixConfig.add("hi", HelloTagHandler.class);
FenixConfig.add("andHi", " AND ", HelloTagHandler::new, " LIKE ");
FenixConfig fenixConfig = new FenixConfig().setHandlerLocations("com.blinkfox.fenix.handler");
FenixConfig fenixConfig = new FenixConfig()
.setHandlerLocations("com.blinkfox.fenix.handler")
.setUnderscoreTransformerPrefix("c_, n_, d_, dt_");
FenixConfigManager.getInstance().initLoad(fenixConfig);

context = new HashMap<>(4);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.blinkfox.fenix.jpa.transformer;

import java.util.Arrays;
import java.util.Set;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

/**
* {@link PrefixUnderscoreTransformer} 的单元测试类.
*
* @author blinkfox on 2020-12-07.
* @since v2.4.0
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PrefixUnderscoreTransformerTest {

@Test
public void test01Prefix() {
PrefixUnderscoreTransformer.getPrefixSet().clear();
Assert.assertTrue(PrefixUnderscoreTransformer.getPrefixSet().isEmpty());

Set<String> prefixSet = PrefixUnderscoreTransformer.getPrefixSet();
prefixSet.addAll(Arrays.asList("c_", "n_", "d_", "dt_"));
Assert.assertTrue(PrefixUnderscoreTransformer.getPrefixSet().contains("c_"));
}

@Test
public void test02LowerCamelCase() {
PrefixUnderscoreTransformer transformer = new PrefixUnderscoreTransformer();
Assert.assertEquals("abcDef", transformer.toLowerCamelCase("c_abc_def"));
Assert.assertEquals("abcDef", transformer.toLowerCamelCase("dt_abC_Def"));
Assert.assertEquals("aBcd", transformer.toLowerCamelCase("a_bcd"));
Assert.assertEquals("def", transformer.toLowerCamelCase("n_def"));
}

}

0 comments on commit eb5d27c

Please sign in to comment.