Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
c8ffa4c530 | |||
2b296e73c3 | |||
c0b8f3842d | |||
14276a6c02 | |||
|
8f3ccc8435 | ||
61b0233918 | |||
|
11b6715082 | ||
|
aea176b589 | ||
|
04332bab30 | ||
|
a0dfe76bb1 | ||
|
468f75b39e | ||
|
b4af3f62f7 | ||
|
0f27710991 | ||
|
c4ef9561bc | ||
|
1996b12013 | ||
|
4069806028 | ||
|
8cf09eac35 | ||
|
550ddaba53 |
48
README.md
48
README.md
@ -8,7 +8,7 @@ Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 227 KB
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.jfinal</groupId>
|
<groupId>com.jfinal</groupId>
|
||||||
<artifactId>enjoy</artifactId>
|
<artifactId>enjoy</artifactId>
|
||||||
<version>4.4</version>
|
<version>4.8</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -25,7 +25,49 @@ Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 227 KB
|
|||||||
|
|
||||||
#### 简单示例:
|
#### 简单示例:
|
||||||
|
|
||||||
**1. 在 spring 中的配置**
|
**1. 与 Spring boot 整合**
|
||||||
|
```java
|
||||||
|
@Configuration
|
||||||
|
public class SpringBootConfig {
|
||||||
|
|
||||||
|
@Bean(name = "jfinalViewResolver")
|
||||||
|
public JFinalViewResolver getJFinalViewResolver() {
|
||||||
|
|
||||||
|
// 创建用于整合 spring boot 的 ViewResolver 扩展对象
|
||||||
|
JFinalViewResolver jfr = new JFinalViewResolver();
|
||||||
|
|
||||||
|
// 对 spring boot 进行配置
|
||||||
|
jfr.setSuffix(".html");
|
||||||
|
jfr.setContentType("text/html;charset=UTF-8");
|
||||||
|
jfr.setOrder(0);
|
||||||
|
|
||||||
|
// 获取 engine 对象,对 enjoy 模板引擎进行配置,配置方式与前面章节完全一样
|
||||||
|
Engine engine = JFinalViewResolver.engine;
|
||||||
|
|
||||||
|
// 热加载配置能对后续配置产生影响,需要放在最前面
|
||||||
|
engine.setDevMode(true);
|
||||||
|
|
||||||
|
// 使用 ClassPathSourceFactory 从 class path 与 jar 包中加载模板文件
|
||||||
|
engine.setToClassPathSourceFactory();
|
||||||
|
|
||||||
|
// 在使用 ClassPathSourceFactory 时要使用 setBaseTemplatePath
|
||||||
|
// 代替 jfr.setPrefix("/view/")
|
||||||
|
engine.setBaseTemplatePath("/view/");
|
||||||
|
|
||||||
|
// 添加模板函数
|
||||||
|
engine.addSharedFunction("/common/_layout.html");
|
||||||
|
engine.addSharedFunction("/common/_paginate.html");
|
||||||
|
|
||||||
|
// 更多配置与前面章节完全一样
|
||||||
|
// engine.addDirective(...)
|
||||||
|
// engine.addSharedMethod(...);
|
||||||
|
|
||||||
|
return jfr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**2. 与 Spring MVC 整合**
|
||||||
|
|
||||||
```java
|
```java
|
||||||
<bean id="viewResolver" class="com.jfinal.template.ext.spring.JFinalViewResolver">
|
<bean id="viewResolver" class="com.jfinal.template.ext.spring.JFinalViewResolver">
|
||||||
@ -43,7 +85,7 @@ Enjoy 是基于 Java 语言的极轻量极魔板引擎。极轻量级仅 227 KB
|
|||||||
</bean>
|
</bean>
|
||||||
```
|
```
|
||||||
|
|
||||||
**2.详细使用方法见 jfinal 手册**
|
**3.详细使用方法见官方文档**
|
||||||
|
|
||||||
read me 正在补充,详细使用文档见官网:[https://www.jfinal.com/doc/6-1](https://www.jfinal.com/doc/6-1)
|
read me 正在补充,详细使用文档见官网:[https://www.jfinal.com/doc/6-1](https://www.jfinal.com/doc/6-1)
|
||||||
|
|
||||||
|
24
pom.xml
24
pom.xml
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<groupId>com.jfinal</groupId>
|
<groupId>com.jfinal</groupId>
|
||||||
<artifactId>enjoy</artifactId>
|
<artifactId>enjoy</artifactId>
|
||||||
<version>4.4</version>
|
<version>4.8.dev</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
|
||||||
@ -29,7 +29,7 @@
|
|||||||
</license>
|
</license>
|
||||||
</licenses>
|
</licenses>
|
||||||
|
|
||||||
<developers>
|
<!--<developers>
|
||||||
<developer>
|
<developer>
|
||||||
<id>jfinal</id>
|
<id>jfinal</id>
|
||||||
<name>James</name>
|
<name>James</name>
|
||||||
@ -42,16 +42,13 @@
|
|||||||
<connection>scm:git:git@gitee.com:jfinal/enjoy.git</connection>
|
<connection>scm:git:git@gitee.com:jfinal/enjoy.git</connection>
|
||||||
<developerConnection>scm:git:git@gitee.com:jfinal/enjoy.git</developerConnection>
|
<developerConnection>scm:git:git@gitee.com:jfinal/enjoy.git</developerConnection>
|
||||||
<url>git@gitee.com:jfinal/enjoy.git</url>
|
<url>git@gitee.com:jfinal/enjoy.git</url>
|
||||||
</scm>
|
</scm>-->
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
<snapshotRepository>
|
|
||||||
<id>ossrh</id>
|
|
||||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
|
||||||
</snapshotRepository>
|
|
||||||
<repository>
|
<repository>
|
||||||
<id>ossrh</id>
|
<id>mvn-release</id>
|
||||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
<name>mvn-release</name>
|
||||||
|
<url>https://nexus.1216.top/repository/maven-releases/</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
@ -62,7 +59,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.8.2</version>
|
<version>4.13.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -80,6 +77,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
<defaultGoal>compile</defaultGoal>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
@ -127,7 +125,7 @@
|
|||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<!--<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-gpg-plugin</artifactId>
|
<artifactId>maven-gpg-plugin</artifactId>
|
||||||
<version>1.6</version>
|
<version>1.6</version>
|
||||||
@ -140,7 +138,7 @@
|
|||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>-->
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
215
src/main/java/com/jfinal/kit/TplKit.java
Normal file
215
src/main/java/com/jfinal/kit/TplKit.java
Normal file
@ -0,0 +1,215 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
|
||||||
|
* <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 com.jfinal.kit;
|
||||||
|
|
||||||
|
|
||||||
|
import com.jfinal.kit.tpl.NameSpaceDirective;
|
||||||
|
import com.jfinal.kit.tpl.ParaDirective;
|
||||||
|
import com.jfinal.kit.tpl.TplDirective;
|
||||||
|
import com.jfinal.kit.tpl.TplSource;
|
||||||
|
import com.jfinal.template.Engine;
|
||||||
|
import com.jfinal.template.Template;
|
||||||
|
import com.jfinal.template.source.FileSource;
|
||||||
|
import com.jfinal.template.source.ISource;
|
||||||
|
import com.jfinal.template.stat.Location;
|
||||||
|
import com.jfinal.template.stat.ParseException;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SqlKit
|
||||||
|
*/
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
|
public class TplKit {
|
||||||
|
public static String baseSqlTemplatePath;
|
||||||
|
public static final String SQL_TEMPLATE_MAP_KEY = "_SQL_TEMPLATE_MAP_";
|
||||||
|
public static final String SQL_PARA_KEY = "_SQL_PARA_";
|
||||||
|
public static final String PARA_ARRAY_KEY = "_PARA_ARRAY_"; // 此参数保持不动,已被用于模板取值 _PARA_ARRAY_[n]
|
||||||
|
|
||||||
|
private String configName;
|
||||||
|
private boolean devMode;
|
||||||
|
private Engine engine;
|
||||||
|
private List<TplSource> tplSourceList = new ArrayList<TplSource>();
|
||||||
|
private Map<String, Template> sqlTemplateMap;
|
||||||
|
|
||||||
|
private static Map<String, TplKit> kitMap = new HashMap<>();
|
||||||
|
|
||||||
|
public static TplKit use(String configName) {
|
||||||
|
return use(configName, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static synchronized TplKit use(String configName, boolean devMode) {
|
||||||
|
TplKit tplKit = kitMap.get(configName);
|
||||||
|
if (tplKit == null) {
|
||||||
|
tplKit = new TplKit(configName, devMode);
|
||||||
|
kitMap.put(configName, tplKit);
|
||||||
|
}
|
||||||
|
return tplKit;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TplKit(String configName, boolean devMode) {
|
||||||
|
this.configName = configName;
|
||||||
|
this.devMode = devMode;
|
||||||
|
|
||||||
|
engine = new Engine(configName);
|
||||||
|
engine.setDevMode(devMode);
|
||||||
|
engine.setToClassPathSourceFactory();
|
||||||
|
|
||||||
|
engine.addDirective("namespace", NameSpaceDirective.class);
|
||||||
|
engine.addDirective("tpl", TplDirective.class);
|
||||||
|
|
||||||
|
engine.addDirective("para", ParaDirective.class, true);
|
||||||
|
engine.addDirective("p", ParaDirective.class, true); // 配置 #para 指令的别名指令 #p,不建议使用,在此仅为兼容 3.0 版本
|
||||||
|
}
|
||||||
|
|
||||||
|
private TplKit(String configName) {
|
||||||
|
this(configName, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Engine getEngine() {
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDevMode(boolean devMode) {
|
||||||
|
this.devMode = devMode;
|
||||||
|
engine.setDevMode(devMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBaseTplTemplatePath(String baseSqlTemplatePath) {
|
||||||
|
this.baseSqlTemplatePath = baseSqlTemplatePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTplTemplate(String sqlTemplate) {
|
||||||
|
if (StrKit.isBlank(sqlTemplate)) {
|
||||||
|
throw new IllegalArgumentException("tplTemplate can not be blank");
|
||||||
|
}
|
||||||
|
if (baseSqlTemplatePath != null) {
|
||||||
|
addTplTemplate(new FileSource(baseSqlTemplatePath, "tpl.sql"));
|
||||||
|
} else {
|
||||||
|
tplSourceList.add(new TplSource(sqlTemplate));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTplTemplate(ISource sqlTemplate) {
|
||||||
|
if (sqlTemplate == null) {
|
||||||
|
throw new IllegalArgumentException("sqlTemplate can not be null");
|
||||||
|
}
|
||||||
|
tplSourceList.add(new TplSource(sqlTemplate));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTplTemplate(File file) {
|
||||||
|
addTplTemplate(file, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addTplTemplate(File file, FileFilter filter) {
|
||||||
|
if (file.isFile()) {
|
||||||
|
if (filter != null && !filter.accept(file)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
addTplTemplate(new FileSource(file.getParent(), file.getName()));
|
||||||
|
} else if (file.isDirectory()) {
|
||||||
|
File[] files = file.listFiles();
|
||||||
|
for (File file1 : files) {
|
||||||
|
addTplTemplate(file1, filter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void parseTplTemplate() {
|
||||||
|
Map<String, Template> sqlTemplateMap = new HashMap<String, Template>(512, 0.5F);
|
||||||
|
for (TplSource ss : tplSourceList) {
|
||||||
|
String fileName = ss.isFile() ? ss.file : ((FileSource) ss.source).getFinalFileName();
|
||||||
|
try {
|
||||||
|
Template template = ss.isFile() ? engine.getTemplate(ss.file) : engine.getTemplate(ss.source);
|
||||||
|
Map<Object, Object> data = new HashMap<>();
|
||||||
|
data.put(SQL_TEMPLATE_MAP_KEY, sqlTemplateMap);
|
||||||
|
template.renderToString(data);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(String.format("%s parse error", fileName), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.sqlTemplateMap = sqlTemplateMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reloadModifiedTplTemplate() {
|
||||||
|
engine.removeAllTemplateCache(); // 去除 Engine 中的缓存,以免 get 出来后重新判断 isModified
|
||||||
|
parseTplTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isTplTemplateModified() {
|
||||||
|
for (Template template : sqlTemplateMap.values()) {
|
||||||
|
if (template.isModified()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Template getTplTemplate(String key) {
|
||||||
|
Template template = sqlTemplateMap.get(key);
|
||||||
|
if (template == null) { // 此 if 分支,处理起初没有定义,但后续不断追加 sql 的情况
|
||||||
|
if (!devMode) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (isTplTemplateModified()) {
|
||||||
|
synchronized (this) {
|
||||||
|
if (isTplTemplateModified()) {
|
||||||
|
reloadModifiedTplTemplate();
|
||||||
|
template = sqlTemplateMap.get(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (devMode && template.isModified()) {
|
||||||
|
synchronized (this) {
|
||||||
|
template = sqlTemplateMap.get(key);
|
||||||
|
if (template.isModified()) {
|
||||||
|
reloadModifiedTplTemplate();
|
||||||
|
template = sqlTemplateMap.get(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTpl(String key) {
|
||||||
|
Template template = getTplTemplate(key);
|
||||||
|
return template != null ? template.renderToString(null).replaceAll("[\\s]+", " ") : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTpl(String key, Map para) {
|
||||||
|
Template template = getTplTemplate(key);
|
||||||
|
return template != null ? template.renderToString(para).replaceAll("[\\s]+", " ") : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "SqlKit for config : " + configName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
74
src/main/java/com/jfinal/kit/tpl/NameSpaceDirective.java
Normal file
74
src/main/java/com/jfinal/kit/tpl/NameSpaceDirective.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://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 com.jfinal.kit.tpl;
|
||||||
|
|
||||||
|
import com.jfinal.template.Directive;
|
||||||
|
import com.jfinal.template.Env;
|
||||||
|
import com.jfinal.template.TemplateException;
|
||||||
|
import com.jfinal.template.expr.ast.Const;
|
||||||
|
import com.jfinal.template.expr.ast.Expr;
|
||||||
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
|
import com.jfinal.template.io.Writer;
|
||||||
|
import com.jfinal.template.stat.ParseException;
|
||||||
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NameSpaceDirective
|
||||||
|
*/
|
||||||
|
public class NameSpaceDirective extends Directive {
|
||||||
|
|
||||||
|
static final String NAME_SPACE_KEY = "_NAME_SPACE_";
|
||||||
|
|
||||||
|
private String nameSpace;
|
||||||
|
|
||||||
|
public void setExprList(ExprList exprList) {
|
||||||
|
if (exprList.length() == 0) {
|
||||||
|
throw new ParseException("The parameter of #namespace directive can not be blank", location);
|
||||||
|
}
|
||||||
|
if (exprList.length() > 1) {
|
||||||
|
throw new ParseException("Only one parameter allowed for #namespace directive", location);
|
||||||
|
}
|
||||||
|
Expr expr = exprList.getExpr(0);
|
||||||
|
if (expr instanceof Const && ((Const)expr).isStr()) {
|
||||||
|
} else {
|
||||||
|
throw new ParseException("The parameter of #namespace directive must be String", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.nameSpace = ((Const)expr).getStr();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
|
if (scope.get(NAME_SPACE_KEY) != null) {
|
||||||
|
throw new TemplateException("#namespace directive can not be nested", location);
|
||||||
|
}
|
||||||
|
scope.set(NAME_SPACE_KEY, nameSpace);
|
||||||
|
try {
|
||||||
|
stat.exec(env, scope, writer);
|
||||||
|
} finally {
|
||||||
|
scope.remove(NAME_SPACE_KEY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasEnd() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
127
src/main/java/com/jfinal/kit/tpl/ParaDirective.java
Normal file
127
src/main/java/com/jfinal/kit/tpl/ParaDirective.java
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://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 com.jfinal.kit.tpl;
|
||||||
|
|
||||||
|
import com.jfinal.kit.TplKit;
|
||||||
|
import com.jfinal.template.Directive;
|
||||||
|
import com.jfinal.template.Env;
|
||||||
|
import com.jfinal.template.TemplateException;
|
||||||
|
import com.jfinal.template.expr.ast.Const;
|
||||||
|
import com.jfinal.template.expr.ast.Expr;
|
||||||
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
|
import com.jfinal.template.expr.ast.Id;
|
||||||
|
import com.jfinal.template.io.Writer;
|
||||||
|
import com.jfinal.template.stat.ParseException;
|
||||||
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* #para 指令用于在 sql 模板中根据参数名生成问号占位以及查询参数
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* 一、参数为表达式的用法
|
||||||
|
* 1:模板内容
|
||||||
|
* #sql("find")
|
||||||
|
* select * from user where nickName = #para(nickName) and age > #para(age)
|
||||||
|
* #end
|
||||||
|
*
|
||||||
|
* 2: java 代码
|
||||||
|
* SqlPara sp = getSqlPara("find", Kv.by("nickName", "prettyGirl").set("age", 18));
|
||||||
|
* user.find(sp)
|
||||||
|
* 或者:
|
||||||
|
* user.find(sp.getSql(), sp.getPara());
|
||||||
|
*
|
||||||
|
* 3:以上用法会在 #para(expr) 处生成问号占位字符,并且实际的参数放入 SqlPara 对象的参数列表中
|
||||||
|
* 后续可以通过 sqlPara.getPara() 获取到参数并直接用于查询
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* 二、参数为 int 型数字的用法
|
||||||
|
* 1:模板内容
|
||||||
|
* #sql("find")
|
||||||
|
* select * from user where id > #para(0) and id < #para(1)
|
||||||
|
* #end
|
||||||
|
*
|
||||||
|
* 2: java 代码
|
||||||
|
* SqlPara sp = getSqlPara("find", 10, 100);
|
||||||
|
* user.find(sp)
|
||||||
|
*
|
||||||
|
* 3:以上用法会在 #para(0) 与 #para(1) 处生成问号占位字符,并且将 10、100 这两个参数放入
|
||||||
|
* SqlPara 对象的参数列表中,后续可以通过 sqlPara.getPara() 获取到参数并直接用于查询
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
public class ParaDirective extends Directive {
|
||||||
|
|
||||||
|
private int index = -1;
|
||||||
|
private String paraName = null;
|
||||||
|
private static boolean checkParaAssigned = true;
|
||||||
|
|
||||||
|
public static void setCheckParaAssigned(boolean checkParaAssigned) {
|
||||||
|
ParaDirective.checkParaAssigned = checkParaAssigned;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExprList(ExprList exprList) {
|
||||||
|
if (exprList.length() == 0) {
|
||||||
|
throw new ParseException("The parameter of #para directive can not be blank", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exprList.length() == 1) {
|
||||||
|
Expr expr = exprList.getExpr(0);
|
||||||
|
if (expr instanceof Const && ((Const)expr).isInt()) {
|
||||||
|
index = ((Const)expr).getInt();
|
||||||
|
if (index < 0) {
|
||||||
|
throw new ParseException("The index of para array must greater than -1", location);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkParaAssigned && exprList.getLastExpr() instanceof Id) {
|
||||||
|
Id id = (Id)exprList.getLastExpr();
|
||||||
|
paraName = id.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.exprList = exprList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
|
TplPara tplPara = (TplPara)scope.get(TplKit.SQL_PARA_KEY);
|
||||||
|
if (tplPara == null) {
|
||||||
|
throw new TemplateException("#para directive invoked by getSqlPara(...) method only", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
write(writer, "?");
|
||||||
|
if (index == -1) {
|
||||||
|
// #para(paraName) 中的 paraName 没有赋值时抛出异常
|
||||||
|
// issue: http://www.jfinal.com/feedback/1832
|
||||||
|
if (checkParaAssigned && paraName != null && !scope.exists(paraName)) {
|
||||||
|
throw new TemplateException("The parameter \""+ paraName +"\" must be assigned", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
tplPara.addPara(exprList.eval(scope));
|
||||||
|
} else {
|
||||||
|
Object[] paras = (Object[])scope.get(TplKit.PARA_ARRAY_KEY);
|
||||||
|
if (paras == null) {
|
||||||
|
throw new TemplateException("The #para(" + index + ") directive must invoked by getSqlPara(String, Object...) method", location);
|
||||||
|
}
|
||||||
|
if (index >= paras.length) {
|
||||||
|
throw new TemplateException("The index of #para directive is out of bounds: " + index, location);
|
||||||
|
}
|
||||||
|
tplPara.addPara(paras[index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
74
src/main/java/com/jfinal/kit/tpl/TplDirective.java
Normal file
74
src/main/java/com/jfinal/kit/tpl/TplDirective.java
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://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 com.jfinal.kit.tpl;
|
||||||
|
|
||||||
|
import com.jfinal.kit.StrKit;
|
||||||
|
import com.jfinal.kit.TplKit;
|
||||||
|
import com.jfinal.template.Directive;
|
||||||
|
import com.jfinal.template.Env;
|
||||||
|
import com.jfinal.template.Template;
|
||||||
|
import com.jfinal.template.expr.ast.Const;
|
||||||
|
import com.jfinal.template.expr.ast.Expr;
|
||||||
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
|
import com.jfinal.template.io.Writer;
|
||||||
|
import com.jfinal.template.stat.ParseException;
|
||||||
|
import com.jfinal.template.stat.Scope;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SqlDirective
|
||||||
|
*/
|
||||||
|
public class TplDirective extends Directive {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
public void setExprList(ExprList exprList) {
|
||||||
|
if (exprList.length() == 0) {
|
||||||
|
throw new ParseException("The parameter of #sql directive can not be blank", location);
|
||||||
|
}
|
||||||
|
if (exprList.length() > 1) {
|
||||||
|
throw new ParseException("Only one parameter allowed for #sql directive", location);
|
||||||
|
}
|
||||||
|
Expr expr = exprList.getExpr(0);
|
||||||
|
if (expr instanceof Const && ((Const)expr).isStr()) {
|
||||||
|
} else {
|
||||||
|
throw new ParseException("The parameter of #sql directive must be String", location);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.id = ((Const)expr).getStr();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
|
String nameSpace = (String)scope.get(NameSpaceDirective.NAME_SPACE_KEY);
|
||||||
|
String key = StrKit.isBlank(nameSpace) ? id : nameSpace + "." + id;
|
||||||
|
Map<String, Template> sqlTemplateMap = (Map<String, Template>)scope.get(TplKit.SQL_TEMPLATE_MAP_KEY);
|
||||||
|
if (sqlTemplateMap.containsKey(key)) {
|
||||||
|
throw new ParseException("Sql already exists with key : " + key, location);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlTemplateMap.put(key, new Template(env, stat));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasEnd() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
71
src/main/java/com/jfinal/kit/tpl/TplPara.java
Normal file
71
src/main/java/com/jfinal/kit/tpl/TplPara.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://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 com.jfinal.kit.tpl;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SqlPara
|
||||||
|
* 封装查询使用的 sql 与参数,主要用于 getSqlPara(...) 返回值
|
||||||
|
*/
|
||||||
|
public class TplPara implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -8586448059592782381L;
|
||||||
|
|
||||||
|
String sql;
|
||||||
|
List<Object> paraList;
|
||||||
|
|
||||||
|
public TplPara setSql(String sql) {
|
||||||
|
this.sql = sql;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TplPara addPara(Object para) {
|
||||||
|
if (paraList == null) {
|
||||||
|
paraList = new ArrayList<Object>();
|
||||||
|
}
|
||||||
|
paraList.add(para);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSql() {
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object[] getPara() {
|
||||||
|
/*if (paraList == null || paraList.size() == 0) {
|
||||||
|
return DbKit.NULL_PARA_ARRAY;
|
||||||
|
} else {
|
||||||
|
return paraList.toArray(new Object[paraList.size()]);
|
||||||
|
}*/
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TplPara clear() {
|
||||||
|
sql = null;
|
||||||
|
if (paraList != null) {
|
||||||
|
paraList.clear();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "Sql: " + sql + "\nPara: " + paraList;
|
||||||
|
}
|
||||||
|
}
|
29
src/main/java/com/jfinal/kit/tpl/TplSource.java
Normal file
29
src/main/java/com/jfinal/kit/tpl/TplSource.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.jfinal.kit.tpl;
|
||||||
|
|
||||||
|
import com.jfinal.template.source.ISource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装 sql 模板源
|
||||||
|
*/
|
||||||
|
public class TplSource {
|
||||||
|
|
||||||
|
public String file;
|
||||||
|
public ISource source;
|
||||||
|
|
||||||
|
public TplSource(String file) {
|
||||||
|
this.file = file;
|
||||||
|
this.source = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TplSource(ISource source) {
|
||||||
|
this.file = null;
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFile() {
|
||||||
|
return file != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -128,7 +128,8 @@ public class ProxyCompiler {
|
|||||||
public void compile(ProxyClass proxyClass) {
|
public void compile(ProxyClass proxyClass) {
|
||||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
if (compiler == null) {
|
if (compiler == null) {
|
||||||
throw new RuntimeException("Can not get javax.tools.JavaCompiler, check whether \"tools.jar\" is in the environment variable CLASSPATH");
|
throw new RuntimeException("Can not get javax.tools.JavaCompiler, check whether \"tools.jar\" is in the environment variable CLASSPATH \n" +
|
||||||
|
"Visit https://jfinal.com/doc/4-8 for details \n");
|
||||||
}
|
}
|
||||||
|
|
||||||
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
|
DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<>();
|
||||||
|
@ -102,6 +102,14 @@ public class Template {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持无 data 参数,渲染到 String 中去 <br>
|
||||||
|
* 适用于数据在模板中通过表达式和语句直接计算得出等等应用场景
|
||||||
|
*/
|
||||||
|
public String renderToString() {
|
||||||
|
return renderToString(null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 渲染到 StringBuilder 中去
|
* 渲染到 StringBuilder 中去
|
||||||
*/
|
*/
|
||||||
|
@ -16,10 +16,6 @@
|
|||||||
|
|
||||||
package com.jfinal.template.expr.ast;
|
package com.jfinal.template.expr.ast;
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
import com.jfinal.template.TemplateException;
|
import com.jfinal.template.TemplateException;
|
||||||
import com.jfinal.template.expr.Sym;
|
import com.jfinal.template.expr.Sym;
|
||||||
import com.jfinal.template.stat.Location;
|
import com.jfinal.template.stat.Location;
|
||||||
@ -37,16 +33,6 @@ public class Logic extends Expr {
|
|||||||
private Expr left; // ! 运算没有 left 参数
|
private Expr left; // ! 运算没有 left 参数
|
||||||
private Expr right;
|
private Expr right;
|
||||||
|
|
||||||
// 默认为新工作模式
|
|
||||||
private static boolean newWorkMode = true;
|
|
||||||
/**
|
|
||||||
* 设置为旧工作模式,为了兼容 jfinal 3.3 之前的版本
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public static void setToOldWorkMode() {
|
|
||||||
newWorkMode = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造 || && 结点
|
* 构造 || && 结点
|
||||||
*/
|
*/
|
||||||
@ -107,12 +93,6 @@ public class Logic extends Expr {
|
|||||||
* 2:boolean 类型,原值返回
|
* 2:boolean 类型,原值返回
|
||||||
* 3:String、StringBuilder 等一切继承自 CharSequence 类的对象,返回 length > 0
|
* 3:String、StringBuilder 等一切继承自 CharSequence 类的对象,返回 length > 0
|
||||||
* 4:其它返回 true
|
* 4:其它返回 true
|
||||||
*
|
|
||||||
* 通过 Logic.setToOldWorkMode() 设置,可支持老版本中的以下四个规则:
|
|
||||||
* 1:Number 类型,返回 value != 0
|
|
||||||
* 2:Map、Collection(List被包括在内) 返回 size() > 0
|
|
||||||
* 3:数组,返回 length > 0
|
|
||||||
* 4:Iterator 返回 hasNext() 值
|
|
||||||
*/
|
*/
|
||||||
public static boolean isTrue(Object v) {
|
public static boolean isTrue(Object v) {
|
||||||
if (v == null) {
|
if (v == null) {
|
||||||
@ -122,38 +102,11 @@ public class Logic extends Expr {
|
|||||||
if (v instanceof Boolean) {
|
if (v instanceof Boolean) {
|
||||||
return (Boolean)v;
|
return (Boolean)v;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (v instanceof CharSequence) {
|
if (v instanceof CharSequence) {
|
||||||
return ((CharSequence)v).length() > 0;
|
return ((CharSequence)v).length() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果不是新工作模式,则对下面类型进行判断
|
|
||||||
if ( !newWorkMode ) {
|
|
||||||
if (v instanceof Number) {
|
|
||||||
if (v instanceof Double) {
|
|
||||||
return ((Number)v).doubleValue() != 0;
|
|
||||||
}
|
|
||||||
if (v instanceof Float) {
|
|
||||||
return ((Number)v).floatValue() != 0;
|
|
||||||
}
|
|
||||||
return ((Number)v).intValue() != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 下面四种类型的判断已提供了 shared method 扩展,用法如下:
|
|
||||||
// #if(notEmpty(object)) 以及 #if(isEmpty(object))
|
|
||||||
if (v instanceof Collection) {
|
|
||||||
return ((Collection<?>)v).size() > 0;
|
|
||||||
}
|
|
||||||
if (v instanceof Map) {
|
|
||||||
return ((Map<?, ?>)v).size() > 0;
|
|
||||||
}
|
|
||||||
if (v.getClass().isArray()) {
|
|
||||||
return Array.getLength(v) > 0;
|
|
||||||
}
|
|
||||||
if (v instanceof Iterator) {
|
|
||||||
return ((Iterator<?>)v).hasNext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ public class Method extends Expr {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
MethodInfo methodInfo = MethodKit.getMethod(target.getClass(), methodName, argValues);
|
MethodInfo methodInfo = MethodKit.getMethod(target.getClass(), methodName, argValues);
|
||||||
if (methodInfo != null) {
|
if (methodInfo.notNull()) {
|
||||||
return methodInfo.invoke(target, argValues);
|
return methodInfo.invoke(target, argValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,6 +93,29 @@ public class MethodInfo {
|
|||||||
}
|
}
|
||||||
return ret.append(")").toString();
|
return ret.append(")").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --------- 以下代码仅用于支持 NullMethodInfo
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 仅供 NullMethodInfo 继承使用
|
||||||
|
*/
|
||||||
|
protected MethodInfo() {
|
||||||
|
this.key = null;
|
||||||
|
this.clazz = null;
|
||||||
|
this.method = null;
|
||||||
|
this.isVarArgs = false;
|
||||||
|
this.paraTypes = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 仅仅 NullMethodInfo 会覆盖此方法并返回 false
|
||||||
|
*
|
||||||
|
* 1:MethodKit.getMethod(...) 消除 instanceof 判断
|
||||||
|
* 2:Method.exec(...) 消除 null 值判断
|
||||||
|
*/
|
||||||
|
public boolean notNull() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ public class MethodKit {
|
|||||||
private static final Set<String> forbiddenMethods = new HashSet<String>(64);
|
private static final Set<String> forbiddenMethods = new HashSet<String>(64);
|
||||||
private static final Set<Class<?>> forbiddenClasses = new HashSet<Class<?>>(64);
|
private static final Set<Class<?>> forbiddenClasses = new HashSet<Class<?>>(64);
|
||||||
private static final Map<Class<?>, Class<?>> primitiveMap = new HashMap<Class<?>, Class<?>>(64);
|
private static final Map<Class<?>, Class<?>> primitiveMap = new HashMap<Class<?>, Class<?>>(64);
|
||||||
private static final SyncWriteMap<Long, Object> methodCache = new SyncWriteMap<Long, Object>(2048, 0.25F);
|
private static final SyncWriteMap<Long, MethodInfo> methodCache = new SyncWriteMap<Long, MethodInfo>(2048, 0.25F);
|
||||||
|
|
||||||
// 初始化在模板中调用 method 时所在的被禁止使用类
|
// 初始化在模板中调用 method 时所在的被禁止使用类
|
||||||
static {
|
static {
|
||||||
@ -49,7 +49,9 @@ public class MethodKit {
|
|||||||
System.class, Runtime.class, Thread.class, Class.class, ClassLoader.class, File.class,
|
System.class, Runtime.class, Thread.class, Class.class, ClassLoader.class, File.class,
|
||||||
Compiler.class, InheritableThreadLocal.class, Package.class, Process.class,
|
Compiler.class, InheritableThreadLocal.class, Package.class, Process.class,
|
||||||
RuntimePermission.class, SecurityManager.class, ThreadGroup.class, ThreadLocal.class,
|
RuntimePermission.class, SecurityManager.class, ThreadGroup.class, ThreadLocal.class,
|
||||||
java.lang.reflect.Method.class
|
|
||||||
|
java.lang.reflect.Method.class,
|
||||||
|
java.lang.reflect.Proxy.class
|
||||||
};
|
};
|
||||||
for (Class<?> c : cs) {
|
for (Class<?> c : cs) {
|
||||||
forbiddenClasses.add(c);
|
forbiddenClasses.add(c);
|
||||||
@ -62,8 +64,8 @@ public class MethodKit {
|
|||||||
"getClass", "getDeclaringClass", "forName", "newInstance", "getClassLoader",
|
"getClass", "getDeclaringClass", "forName", "newInstance", "getClassLoader",
|
||||||
"invoke", // "getMethod", "getMethods", // "getField", "getFields",
|
"invoke", // "getMethod", "getMethods", // "getField", "getFields",
|
||||||
"notify", "notifyAll", "wait",
|
"notify", "notifyAll", "wait",
|
||||||
"load", "exit", "loadLibrary", "halt",
|
"exit", "loadLibrary", "halt", // "load",
|
||||||
"stop", "suspend", "resume", "setDaemon", "setPriority",
|
"stop", "suspend", "resume" // "setDaemon", "setPriority"
|
||||||
};
|
};
|
||||||
for (String m : ms) {
|
for (String m : ms) {
|
||||||
forbiddenMethods.add(m);
|
forbiddenMethods.add(m);
|
||||||
@ -122,36 +124,17 @@ public class MethodKit {
|
|||||||
public static MethodInfo getMethod(Class<?> targetClass, String methodName, Object[] argValues) {
|
public static MethodInfo getMethod(Class<?> targetClass, String methodName, Object[] argValues) {
|
||||||
Class<?>[] argTypes = getArgTypes(argValues);
|
Class<?>[] argTypes = getArgTypes(argValues);
|
||||||
Long key = getMethodKey(targetClass, methodName, argTypes);
|
Long key = getMethodKey(targetClass, methodName, argTypes);
|
||||||
Object method = methodCache.get(key);
|
MethodInfo method = methodCache.get(key);
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
|
// 已确保不会返回 null,对于不存在的 Method,只进行一次获取操作
|
||||||
|
// 提升 null safe 表达式性能,未来需要考虑内存泄漏风险
|
||||||
method = doGetMethod(key, targetClass, methodName, argTypes);
|
method = doGetMethod(key, targetClass, methodName, argTypes);
|
||||||
if (method != null) {
|
methodCache.putIfAbsent(key, method);
|
||||||
methodCache.putIfAbsent(key, method);
|
|
||||||
} else {
|
|
||||||
// 对于不存在的 Method,只进行一次获取操作,主要为了支持 null safe,未来需要考虑内存泄漏风险
|
|
||||||
methodCache.putIfAbsent(key, Void.class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return method instanceof MethodInfo ? (MethodInfo)method : null;
|
|
||||||
|
return method;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取 getter 方法
|
|
||||||
* 使用与 Field 相同的 key,避免生成两次 key值
|
|
||||||
* ---> jfinal 3.5 已将此功能转移至 FieldKit
|
|
||||||
public static MethodInfo getGetterMethod(Long key, Class<?> targetClass, String methodName) {
|
|
||||||
Object getterMethod = methodCache.get(key);
|
|
||||||
if (getterMethod == null) {
|
|
||||||
getterMethod = doGetMethod(key, targetClass, methodName, NULL_ARG_TYPES);
|
|
||||||
if (getterMethod != null) {
|
|
||||||
methodCache.putIfAbsent(key, getterMethod);
|
|
||||||
} else {
|
|
||||||
methodCache.putIfAbsent(key, Void.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return getterMethod instanceof MethodInfo ? (MethodInfo)getterMethod : null;
|
|
||||||
} */
|
|
||||||
|
|
||||||
static Class<?>[] getArgTypes(Object[] argValues) {
|
static Class<?>[] getArgTypes(Object[] argValues) {
|
||||||
if (argValues == null || argValues.length == 0) {
|
if (argValues == null || argValues.length == 0) {
|
||||||
return NULL_ARG_TYPES;
|
return NULL_ARG_TYPES;
|
||||||
@ -167,7 +150,9 @@ public class MethodKit {
|
|||||||
if (forbiddenClasses.contains(targetClass)) {
|
if (forbiddenClasses.contains(targetClass)) {
|
||||||
throw new RuntimeException("Forbidden class: " + targetClass.getName());
|
throw new RuntimeException("Forbidden class: " + targetClass.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 仅开启 forbiddenClasses 检测
|
// 仅开启 forbiddenClasses 检测
|
||||||
|
// Method、SharedMethod、StaticMethod 已用 MethodKit.isForbiddenMethod(...) 检测
|
||||||
// if (forbiddenMethods.contains(methodName)) {
|
// if (forbiddenMethods.contains(methodName)) {
|
||||||
// throw new RuntimeException("Forbidden method: " + methodName);
|
// throw new RuntimeException("Forbidden method: " + methodName);
|
||||||
// }
|
// }
|
||||||
@ -184,7 +169,8 @@ public class MethodKit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
return NullMethodInfo.me;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean matchFixedArgTypes(Class<?>[] paraTypes, Class<?>[] argTypes) {
|
static boolean matchFixedArgTypes(Class<?>[] paraTypes, Class<?>[] argTypes) {
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* http://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 com.jfinal.template.expr.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NullMethodInfo
|
||||||
|
*
|
||||||
|
* 1:MethodKit.getMethod(...) 消除 instanceof 判断
|
||||||
|
* 2:Method.exec(...) 消除 null 值判断
|
||||||
|
*/
|
||||||
|
public class NullMethodInfo extends MethodInfo {
|
||||||
|
|
||||||
|
public static final NullMethodInfo me = new NullMethodInfo();
|
||||||
|
|
||||||
|
public boolean notNull() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object invoke(Object target, Object... args) throws ReflectiveOperationException {
|
||||||
|
throw new RuntimeException("The method invoke(Object, Object...) of NullMethodInfo should not be invoked");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -50,6 +50,14 @@ public class StaticMethod extends Expr {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParseException(e.getMessage(), location, e);
|
throw new ParseException(e.getMessage(), location, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (MethodKit.isForbiddenClass(this.clazz)) {
|
||||||
|
throw new ParseException("Forbidden class: " + this.clazz.getName(), location);
|
||||||
|
}
|
||||||
|
if (MethodKit.isForbiddenMethod(methodName)) {
|
||||||
|
throw new ParseException("Forbidden method: " + methodName, location);
|
||||||
|
}
|
||||||
|
|
||||||
this.methodName = methodName;
|
this.methodName = methodName;
|
||||||
this.exprList = exprList;
|
this.exprList = exprList;
|
||||||
this.location = location;
|
this.location = location;
|
||||||
@ -61,7 +69,7 @@ public class StaticMethod extends Expr {
|
|||||||
try {
|
try {
|
||||||
MethodInfo methodInfo = MethodKit.getMethod(clazz, methodName, argValues);
|
MethodInfo methodInfo = MethodKit.getMethod(clazz, methodName, argValues);
|
||||||
|
|
||||||
if (methodInfo != null) {
|
if (methodInfo.notNull()) {
|
||||||
if (methodInfo.isStatic()) {
|
if (methodInfo.isStatic()) {
|
||||||
return methodInfo.invoke(null, argValues);
|
return methodInfo.invoke(null, argValues);
|
||||||
} else {
|
} else {
|
||||||
|
@ -53,10 +53,9 @@ public class NumberDirective extends Directive {
|
|||||||
|
|
||||||
private Expr valueExpr;
|
private Expr valueExpr;
|
||||||
private Expr patternExpr;
|
private Expr patternExpr;
|
||||||
private int paraNum;
|
|
||||||
|
|
||||||
public void setExprList(ExprList exprList) {
|
public void setExprList(ExprList exprList) {
|
||||||
this.paraNum = exprList.length();
|
int paraNum = exprList.length();
|
||||||
if (paraNum == 0) {
|
if (paraNum == 0) {
|
||||||
throw new ParseException("The parameter of #number directive can not be blank", location);
|
throw new ParseException("The parameter of #number directive can not be blank", location);
|
||||||
}
|
}
|
||||||
@ -64,13 +63,8 @@ public class NumberDirective extends Directive {
|
|||||||
throw new ParseException("Wrong number parameter of #number directive, two parameters allowed at most", location);
|
throw new ParseException("Wrong number parameter of #number directive, two parameters allowed at most", location);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (paraNum == 1) {
|
valueExpr = exprList.getExpr(0);
|
||||||
this.valueExpr = exprList.getExpr(0);
|
patternExpr = (paraNum == 1 ? null : exprList.getExpr(1));
|
||||||
this.patternExpr = null;
|
|
||||||
} else if (paraNum == 2) {
|
|
||||||
this.valueExpr = exprList.getExpr(0);
|
|
||||||
this.patternExpr = exprList.getExpr(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void exec(Env env, Scope scope, Writer writer) {
|
public void exec(Env env, Scope scope, Writer writer) {
|
||||||
@ -79,9 +73,9 @@ public class NumberDirective extends Directive {
|
|||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (paraNum == 1) {
|
if (patternExpr == null) {
|
||||||
outputWithoutPattern(writer, value);
|
outputWithoutPattern(writer, value);
|
||||||
} else if (paraNum == 2) {
|
} else {
|
||||||
outputWithPattern(scope, writer, value);
|
outputWithPattern(scope, writer, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,6 +194,14 @@ public class JFinalViewResolver extends AbstractTemplateViewResolver {
|
|||||||
engine.setSourceFactory(sourceFactory);
|
engine.setSourceFactory(sourceFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置为 ClassPathSourceFactory 的快捷方法
|
||||||
|
* ClassPathSourceFactory 将从 CLASSPATH 与 jar 包中读取模板
|
||||||
|
*/
|
||||||
|
public void setToClassPathSourceFactory() {
|
||||||
|
engine.setToClassPathSourceFactory();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置模板基础路径
|
* 设置模板基础路径
|
||||||
*/
|
*/
|
||||||
|
@ -78,6 +78,7 @@ public class Scope {
|
|||||||
if (cur.data == null) { // 支持顶层 data 为 null 值
|
if (cur.data == null) { // 支持顶层 data 为 null 值
|
||||||
cur.data = new HashMap();
|
cur.data = new HashMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
cur.data.put(key, value);
|
cur.data.put(key, value);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
@ -159,6 +160,10 @@ public class Scope {
|
|||||||
public void setGlobal(Object key, Object value) {
|
public void setGlobal(Object key, Object value) {
|
||||||
for (Scope cur=this; true; cur=cur.parent) {
|
for (Scope cur=this; true; cur=cur.parent) {
|
||||||
if (cur.parent == null) {
|
if (cur.parent == null) {
|
||||||
|
if (cur.data == null) {
|
||||||
|
cur.data = new HashMap();
|
||||||
|
}
|
||||||
|
|
||||||
cur.data.put(key, value);
|
cur.data.put(key, value);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
@ -172,7 +177,7 @@ public class Scope {
|
|||||||
public Object getGlobal(Object key) {
|
public Object getGlobal(Object key) {
|
||||||
for (Scope cur=this; true; cur=cur.parent) {
|
for (Scope cur=this; true; cur=cur.parent) {
|
||||||
if (cur.parent == null) {
|
if (cur.parent == null) {
|
||||||
return cur.data.get(key);
|
return cur.data != null ? cur.data.get(key) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,7 +189,10 @@ public class Scope {
|
|||||||
public void removeGlobal(Object key) {
|
public void removeGlobal(Object key) {
|
||||||
for (Scope cur=this; true; cur=cur.parent) {
|
for (Scope cur=this; true; cur=cur.parent) {
|
||||||
if (cur.parent == null) {
|
if (cur.parent == null) {
|
||||||
cur.data.remove(key);
|
if (cur.data != null) {
|
||||||
|
cur.data.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import com.jfinal.template.Env;
|
|||||||
import com.jfinal.template.expr.ast.Assign;
|
import com.jfinal.template.expr.ast.Assign;
|
||||||
import com.jfinal.template.expr.ast.Expr;
|
import com.jfinal.template.expr.ast.Expr;
|
||||||
import com.jfinal.template.expr.ast.ExprList;
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
|
import com.jfinal.template.expr.ast.IncDec;
|
||||||
import com.jfinal.template.io.Writer;
|
import com.jfinal.template.io.Writer;
|
||||||
import com.jfinal.template.stat.Location;
|
import com.jfinal.template.stat.Location;
|
||||||
import com.jfinal.template.stat.ParseException;
|
import com.jfinal.template.stat.ParseException;
|
||||||
@ -44,10 +45,11 @@ public class Set extends Stat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Expr expr : exprList.getExprArray()) {
|
for (Expr expr : exprList.getExprArray()) {
|
||||||
if ( !(expr instanceof Assign) ) {
|
if ( !(expr instanceof Assign || expr instanceof IncDec) ) {
|
||||||
throw new ParseException("#set directive only supports assignment expressions", location);
|
throw new ParseException("#set directive only supports assignment expressions", location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.expr = exprList.getActualExpr();
|
this.expr = exprList.getActualExpr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import com.jfinal.template.Env;
|
|||||||
import com.jfinal.template.expr.ast.Assign;
|
import com.jfinal.template.expr.ast.Assign;
|
||||||
import com.jfinal.template.expr.ast.Expr;
|
import com.jfinal.template.expr.ast.Expr;
|
||||||
import com.jfinal.template.expr.ast.ExprList;
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
|
import com.jfinal.template.expr.ast.IncDec;
|
||||||
import com.jfinal.template.io.Writer;
|
import com.jfinal.template.io.Writer;
|
||||||
import com.jfinal.template.stat.Ctrl;
|
import com.jfinal.template.stat.Ctrl;
|
||||||
import com.jfinal.template.stat.Location;
|
import com.jfinal.template.stat.Location;
|
||||||
@ -41,10 +42,11 @@ public class SetGlobal extends Stat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Expr expr : exprList.getExprArray()) {
|
for (Expr expr : exprList.getExprArray()) {
|
||||||
if ( !(expr instanceof Assign) ) {
|
if ( !(expr instanceof Assign || expr instanceof IncDec) ) {
|
||||||
throw new ParseException("#setGlobal directive only supports assignment expressions", location);
|
throw new ParseException("#setGlobal directive only supports assignment expressions", location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.expr = exprList.getActualExpr();
|
this.expr = exprList.getActualExpr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import com.jfinal.template.Env;
|
|||||||
import com.jfinal.template.expr.ast.Assign;
|
import com.jfinal.template.expr.ast.Assign;
|
||||||
import com.jfinal.template.expr.ast.Expr;
|
import com.jfinal.template.expr.ast.Expr;
|
||||||
import com.jfinal.template.expr.ast.ExprList;
|
import com.jfinal.template.expr.ast.ExprList;
|
||||||
|
import com.jfinal.template.expr.ast.IncDec;
|
||||||
import com.jfinal.template.io.Writer;
|
import com.jfinal.template.io.Writer;
|
||||||
import com.jfinal.template.stat.Ctrl;
|
import com.jfinal.template.stat.Ctrl;
|
||||||
import com.jfinal.template.stat.Location;
|
import com.jfinal.template.stat.Location;
|
||||||
@ -42,10 +43,11 @@ public class SetLocal extends Stat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (Expr expr : exprList.getExprArray()) {
|
for (Expr expr : exprList.getExprArray()) {
|
||||||
if ( !(expr instanceof Assign) ) {
|
if ( !(expr instanceof Assign || expr instanceof IncDec) ) {
|
||||||
throw new ParseException("#setLocal directive only supports assignment expressions", location);
|
throw new ParseException("#setLocal directive only supports assignment expressions", location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.expr = exprList.getActualExpr();
|
this.expr = exprList.getActualExpr();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
8
src/main/resources/tpl.sql
Normal file
8
src/main/resources/tpl.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#tpl("findGirl")
|
||||||
|
select * from girl where age > ? and age < ? and weight < 50
|
||||||
|
#end
|
||||||
|
|
||||||
|
#tpl("findGirl2")
|
||||||
|
select * from girl where age > ? and age < ? and weight < 50
|
||||||
|
#end
|
||||||
|
|
28
src/test/java/com/jfinal/template/TplKitTest.java
Normal file
28
src/test/java/com/jfinal/template/TplKitTest.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package com.jfinal.template;
|
||||||
|
|
||||||
|
import com.jfinal.kit.Kv;
|
||||||
|
import com.jfinal.kit.TplKit;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class TplKitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run() {
|
||||||
|
TplKit tplKit = TplKit.use("", true);
|
||||||
|
|
||||||
|
tplKit.addTplTemplate(new File("E:\\wk\\HaoGamePlatfProject\\conf"), x -> x.getName().endsWith(".tpl"));
|
||||||
|
|
||||||
|
|
||||||
|
tplKit.parseTplTemplate();
|
||||||
|
|
||||||
|
TplKit tplKit2 = TplKit.use("", true);
|
||||||
|
|
||||||
|
String findGirl = tplKit2.getTpl("abx", Kv.by("a", 12321));
|
||||||
|
|
||||||
|
System.out.println(findGirl);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user