📄 builtin.java
字号:
private class BIMethod implements TemplateMethodModelEx {
private String s;
private BIMethod(String s) {
this.s = s;
}
public Object exec(List args) throws TemplateModelException {
Object obj;
String sub;
int ln = args.size();
if (ln == 0) {
throw new TemplateModelException(
"?last_index_of(...) expects at least one argument.");
}
if (ln > 2) {
throw new TemplateModelException(
"?last_index_of(...) expects at most two arguments.");
}
obj = args.get(0);
if (!(obj instanceof TemplateScalarModel)) {
throw new TemplateModelException(
"?last_index_of(...) expects a string as "
+ "its first argument.");
}
sub = ((TemplateScalarModel) obj).getAsString();
if (ln > 1) {
obj = args.get(1);
if (!(obj instanceof TemplateNumberModel)) {
throw new TemplateModelException(
"?last_index_of(...) expects a number as "
+ "its second argument.");
}
int fidx = ((TemplateNumberModel) obj).getAsNumber().intValue();
return new SimpleNumber(s.lastIndexOf(sub, fidx));
} else {
return new SimpleNumber(s.lastIndexOf(sub));
}
}
}
}
static class starts_withBI extends BuiltIn {
TemplateModel _getAsTemplateModel(Environment env)
throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
if (model instanceof TemplateScalarModel) {
return new BIMethod(((TemplateScalarModel) model).getAsString());
}
throw invalidTypeException(model, target, env, "string");
}
private class BIMethod implements TemplateMethodModelEx {
private String s;
private BIMethod(String s) {
this.s = s;
}
public Object exec(List args) throws TemplateModelException {
String sub;
if (args.size() != 1) {
throw new TemplateModelException(
"?starts_with(...) expects exactly 1 argument.");
}
Object obj = args.get(0);
if (!(obj instanceof TemplateScalarModel)) {
throw new TemplateModelException(
"?starts_with(...) expects a string argument");
}
sub = ((TemplateScalarModel) obj).getAsString();
return s.startsWith(sub) ?
TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
}
}
}
static class ends_withBI extends BuiltIn {
TemplateModel _getAsTemplateModel(Environment env)
throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
if (model instanceof TemplateScalarModel) {
return new BIMethod(((TemplateScalarModel) model).getAsString());
}
throw invalidTypeException(model, target, env, "string");
}
private class BIMethod implements TemplateMethodModelEx {
private String s;
private BIMethod(String s) {
this.s = s;
}
public Object exec(List args) throws TemplateModelException {
String sub;
if (args.size() != 1) {
throw new TemplateModelException(
"?ends_with(...) expects exactly 1 argument.");
}
Object obj = args.get(0);
if (!(obj instanceof TemplateScalarModel)) {
throw new TemplateModelException(
"?ends_with(...) expects a string argument");
}
sub = ((TemplateScalarModel) obj).getAsString();
return s.endsWith(sub) ?
TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
}
}
}
static class replaceBI extends BuiltIn {
TemplateModel _getAsTemplateModel(Environment env)
throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
if (model instanceof TemplateScalarModel) {
return new BIMethod(((TemplateScalarModel) model).getAsString());
}
throw invalidTypeException(model, target, env, "string");
}
private class BIMethod implements TemplateMethodModel {
private String s;
private BIMethod(String s) {
this.s = s;
}
public Object exec(List args) throws TemplateModelException {
int numArgs = args.size();
if (numArgs < 2 || numArgs > 3) {
throw new TemplateModelException(
"?replace(...) needs 2 or 3 arguments.");
}
String first = (String) args.get(0);
String second = (String) args.get(1);
String flags = numArgs >2 ? (String) args.get(2) : "";
boolean caseInsensitive = flags.indexOf('i') >=0;
boolean firstOnly = flags.indexOf('f') >=0;
if (flags.indexOf('r') >=0) {
throw new TemplateModelException("The regular expression classes are not available.");
}
return new SimpleScalar(StringUtil.replace(
s, first, second, caseInsensitive, firstOnly));
}
}
}
static class splitBI extends BuiltIn {
TemplateModel _getAsTemplateModel(Environment env)
throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
if (model instanceof TemplateScalarModel) {
return new BIMethod(((TemplateScalarModel) model).getAsString());
}
throw invalidTypeException(model, target, env, "string");
}
private class BIMethod implements TemplateMethodModel {
private String s;
private BIMethod(String s) {
this.s = s;
}
public Object exec(List args) throws TemplateModelException {
int numArgs = args.size();
if (numArgs != 1 && numArgs !=2) {
throw new TemplateModelException(
"?split(...) expects 1 or 2 arguments.");
}
String splitString = (String) args.get(0);
String flags = numArgs ==2 ? (String) args.get(1) : "";
boolean caseInsensitive = flags.indexOf('i') >=0;
if (flags.indexOf('r') >=0) {
throw new TemplateModelException("regular expression classes not available");
}
return new StringArraySequence(StringUtil.split(
s, splitString, caseInsensitive));
}
}
}
static class left_padBI extends BuiltIn {
TemplateModel _getAsTemplateModel(Environment env)
throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
if (model instanceof TemplateScalarModel) {
return new BIMethod(((TemplateScalarModel) model).getAsString());
}
throw invalidTypeException(model, target, env, "string");
}
private class BIMethod implements TemplateMethodModelEx {
private String s;
private BIMethod(String s) {
this.s = s;
}
public Object exec(List args) throws TemplateModelException {
Object obj;
int ln = args.size();
if (ln == 0) {
throw new TemplateModelException(
"?left_pad(...) expects at least 1 argument.");
}
if (ln > 2) {
throw new TemplateModelException(
"?left_pad(...) expects at most 2 arguments.");
}
obj = args.get(0);
if (!(obj instanceof TemplateNumberModel)) {
throw new TemplateModelException(
"?left_pad(...) expects a number as "
+ "its 1st argument.");
}
int width = ((TemplateNumberModel) obj).getAsNumber().intValue();
if (ln > 1) {
obj = args.get(1);
if (!(obj instanceof TemplateScalarModel)) {
throw new TemplateModelException(
"?left_pad(...) expects a string as "
+ "its 2nd argument.");
}
String filling = ((TemplateScalarModel) obj).getAsString();
try {
return new SimpleScalar(StringUtil.leftPad(s, width, filling));
} catch (IllegalArgumentException e) {
if (filling.length() == 0) {
throw new TemplateModelException(
"The 2nd argument of ?left_pad(...) "
+ "can't be a 0 length string.");
} else {
throw new TemplateModelException(
"Error while executing the ?left_pad(...) "
+ "built-in.", e);
}
}
} else {
return new SimpleScalar(StringUtil.leftPad(s, width));
}
}
}
}
static class right_padBI extends BuiltIn {
TemplateModel _getAsTemplateModel(Environment env)
throws TemplateException {
TemplateModel model = target.getAsTemplateModel(env);
if (model instanceof TemplateScalarModel) {
return new BIMethod(((TemplateScalarModel) model).getAsString());
}
throw invalidTypeException(model, target, env, "string");
}
private class BIMethod implements TemplateMethodModelEx {
private String s;
private BIMethod(String s) {
this.s = s;
}
public Object exec(List args) throws TemplateModelException {
Object obj;
int ln = args.size();
if (ln == 0) {
throw new TemplateModelException(
"?right_pad(...) expects at least 1 argument.");
}
if (ln > 2) {
throw new TemplateModelException(
"?right_pad(...) expects at most 2 arguments.");
}
obj = args.get(0);
if (!(obj instanceof TemplateNumberModel)) {
throw new TemplateModelException(
"?right_pad(...) expects a number as "
+ "its 1st argument.");
}
int width = ((TemplateNumberModel) obj).getAsNumber().intValue();
if (ln > 1) {
obj = args.get(1);
if (!(obj instanceof TemplateScalarModel)) {
throw new TemplateModelException(
"?right_pad(...) expects a string as "
+ "its 2nd argument.");
}
String filling = ((TemplateScalarModel) obj).getAsString();
try {
return new SimpleScalar(StringUtil.rightPad(s, width, filling));
} catch (IllegalArgumentException e) {
if (filling.length() == 0) {
throw new TemplateModelException(
"The 2nd argument of ?right_pad(...) "
+ "can't be a 0 length string.");
} else {
throw new TemplateModelException(
"Error while executing the ?right_pad(...) "
+ "built-in.", e);
}
}
} else {
return new SimpleScalar(StringUtil.rightPad(s, width));
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -