2021SC@SDUSC
Freemarker源码分析(1)catch.TemplateSourceMatcher及其子类
TemplateSourceMatcher及其子类
总览UML图
1.TemplateSourceMatcher
代码
public abstract class TemplateSourceMatcher {
abstract boolean matches(String sourceName, Object templateSource) throws IOException;
}
2.AndMatcher
代码
public class AndMatcher extends TemplateSourceMatcher {
private final TemplateSourceMatcher[] matchers;
public AndMatcher(TemplateSourceMatcher... matchers) {
if (matchers.length == 0) throw new IllegalArgumentException("Need at least 1 matcher, had 0.");
this.matchers = matchers;
}
@Override
public boolean matches(String sourceName, Object templateSource) throws IOException {
for (TemplateSourceMatcher matcher : matchers) {
if (!(matcher.matches(sourceName, templateSource))) return false;
}
return true;
}
}
作用:用给定的匹配器进行逻辑“和”操作
2.OrMatcher
代码
public class OrMatcher extends TemplateSourceMatcher {
private final TemplateSourceMatcher[] matchers;
public OrMatcher(TemplateSourceMatcher... matchers) {
if (matchers.length == 0) throw new IllegalArgumentException("Need at least 1 matcher, had 0.");
this.matchers = matchers;
}
@Override
public boolean matches(String sourceName, Object templateSource) throws IOException {
for (TemplateSourceMatcher matcher : matchers) {
if ((matcher.matches(sourceName, templateSource))) return true;
}
return false;
}
}
作用:用给定的匹配器进行逻辑“或”操作
3.FileNameGlobMatcher
代码
public class FileNameGlobMatcher extends TemplateSourceMatcher {
private final String glob;
private Pattern pattern;
private boolean caseInsensitive;
public FileNameGlobMatcher(String glob) {
if (glob.indexOf('/') != -1) {
throw new IllegalArgumentException("A file name glob can't contain \"/\": " + glob);
}
this.glob = glob;
buildPattern();
}
private void buildPattern() {
pattern = StringUtil.globToRegularExpression("**/" + glob, caseInsensitive);
}
@Override
public boolean matches(String sourceName, Object templateSource) throws IOException {
return pattern.matcher(sourceName).matches();
}
public boolean isCaseInsensitive() {
return caseInsensitive;
}
public void setCaseInsensitive(boolean caseInsensitive) {
boolean lastCaseInsensitive = this.caseInsensitive;
this.caseInsensitive = caseInsensitive;
if (lastCaseInsensitive != caseInsensitive) {
buildPattern();
}
}
public FileNameGlobMatcher caseInsensitive(boolean caseInsensitive) {
setCaseInsensitive(caseInsensitive);
return this;
}
}
作用:文件名匹配器,用于检测文件名是否匹配,要求文件名不能包含字符‘/’,可以设置是否严格匹配大小写(默认严格匹配)
4.FileExtensionMatcher
代码
public class FileExtensionMatcher extends TemplateSourceMatcher {
private final String extension;
private boolean caseInsensitive = true;
public FileExtensionMatcher(String extension) {
if (extension.indexOf('/') != -1) {
throw new IllegalArgumentException("A file extension can't contain \"/\": " + extension);
}
if (extension.indexOf('*') != -1) {
throw new IllegalArgumentException("A file extension can't contain \"*\": " + extension);
}
if (extension.indexOf('?') != -1) {
throw new IllegalArgumentException("A file extension can't contain \"*\": " + extension);
}
if (extension.startsWith(".")) {
throw new IllegalArgumentException("A file extension can't start with \".\": " + extension);
}
this.extension = extension;
}
@Override
public boolean matches(String sourceName, Object templateSource) throws IOException {
int ln = sourceName.length();
int extLn = extension.length();
if (ln < extLn + 1 || sourceName.charAt(ln - extLn - 1) != '.') {
return false;
}
return sourceName.regionMatches(caseInsensitive, ln - extLn, extension, 0, extLn);
}
public boolean isCaseInsensitive() {
return caseInsensitive;
}
public void setCaseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
}
public FileExtensionMatcher caseInsensitive(boolean caseInsensitive) {
setCaseInsensitive(caseInsensitive);
return this;
}
}
作用:文件扩展名匹配器,要求不能包含字符‘/’‘*’‘?’,且不能以字符‘.’开头
5.PathGlobMatcher
代码
public class PathGlobMatcher extends TemplateSourceMatcher {
private final String glob;
private Pattern pattern;
private boolean caseInsensitive;
public PathGlobMatcher(String glob) {
if (glob.startsWith("/")) {
throw new IllegalArgumentException("Absolute template paths need no inital \"/\"; remove it from: " + glob);
}
this.glob = glob;
buildPattern();
}
private void buildPattern() {
pattern = StringUtil.globToRegularExpression(glob, caseInsensitive);
}
@Override
public boolean matches(String sourceName, Object templateSource) throws IOException {
return pattern.matcher(sourceName).matches();
}
public boolean isCaseInsensitive() {
return caseInsensitive;
}
public void setCaseInsensitive(boolean caseInsensitive) {
boolean lastCaseInsensitive = this.caseInsensitive;
this.caseInsensitive = caseInsensitive;
if (lastCaseInsensitive != caseInsensitive) {
buildPattern();
}
}
public PathGlobMatcher caseInsensitive(boolean caseInsensitive) {
setCaseInsensitive(caseInsensitive);
return this;
}
}
作用: 文件路径通配符匹配器,不能以‘/’开头,这个文件路径是相对路径,并不是文件系统中的绝对路径
6.PathRegexMatcher
代码
public class PathRegexMatcher extends TemplateSourceMatcher {
private final Pattern pattern;
public PathRegexMatcher(String regex) {
if (regex.startsWith("/")) {
throw new IllegalArgumentException("Absolute template paths need no inital \"/\"; remove it from: " + regex);
}
pattern = Pattern.compile(regex);
}
@Override
public boolean matches(String sourceName, Object templateSource) throws IOException {
return pattern.matcher(sourceName).matches();
}
}
作用:文件路径正则表达式匹配器,不能以‘/’开头,这个文件路径是相对路径,并不是文件系统中的绝对路径
注:Freemarker代码来自FreeMarker 中文官方参考手册
新手第一次代码分析,文章若有错误还请指出