2021SC@SDUSC
core.OutputFormat及其子类
由于子类数量过多,本篇博客只分析OutputFormat、CSSOutputFormat、JSONOutputFormat、JavaScriptOutputFormat、PlainTextOutputFormat、UndefinedOutputFormat,剩余的类将于下一篇博客分析。
core.OutputFormat
代码:
public abstract class OutputFormat {
public abstract String getName();
public abstract boolean isOutputFormatMixingAllowed();
@Override
public final String toString() {
String extras = toStringExtraProperties();
return getName() + "("
+ "mimeType=" + StringUtil.jQuote(getMimeType()) + ", "
+ "class=" + ClassUtil.getShortClassNameOfObject(this, true)
+ (extras.length() != 0 ? ", " : "") + extras
+ ")";
}
protected String toStringExtraProperties() {
return "";
}
}
作用:表示一种输出格式。
CSSOutputFormat
代码:
public class CSSOutputFormat extends OutputFormat {
public static final CSSOutputFormat INSTANCE = new CSSOutputFormat();
private CSSOutputFormat() {
// Only to decrease visibility
}
@Override
public String getName() {
return "CSS";
}
@Override
public String getMimeType() {
return "text/css";
}
@Override
public boolean isOutputFormatMixingAllowed() {
return false;
}
}
作用:表示CSS输出格式(MIME type “text/CSS”,name “CSS”)。这种格式不支持转义。
JSONOutputFormat
代码:
public class JSONOutputFormat extends OutputFormat {
public static final JSONOutputFormat INSTANCE = new JSONOutputFormat();
private JSONOutputFormat() {
// Only to decrease visibility
}
@Override
public String getName() {
return "JSON";
}
@Override
public String getMimeType() {
return "application/json";
}
@Override
public boolean isOutputFormatMixingAllowed() {
return false;
}
}
作用:表示JSON输出格式(MIME type “application/JSON”,name “JSON”)。这种格式不支持转义。
JavaScriptOutputFormat
代码:
public class JavaScriptOutputFormat extends OutputFormat {
public static final JavaScriptOutputFormat INSTANCE = new JavaScriptOutputFormat();
private JavaScriptOutputFormat() {
// Only to decrease visibility
}
@Override
public String getName() {
return "JavaScript";
}
@Override
public String getMimeType() {
return "application/javascript";
}
@Override
public boolean isOutputFormatMixingAllowed() {
return false;
}
}
作用:表示JavaScript输出格式(MIME type “application/JavaScript”,name"JavaScript")。这种格式不支持转义。
PlainTextOutputFormat
代码:
public final class PlainTextOutputFormat extends OutputFormat {
public static final PlainTextOutputFormat INSTANCE = new PlainTextOutputFormat();
private PlainTextOutputFormat() {
// Only to decrease visibility
}
@Override
public boolean isOutputFormatMixingAllowed() {
return false;
}
@Override
public String getName() {
return "plainText";
}
@Override
public String getMimeType() {
return "text/plain";
}
}
作用:表示纯文本输出格式(MIME type “text/plain”,name “plainText”)。这种格式不支持转义。这种格式不允许混合其他输出格式的模板输出值。
UndefinedOutputFormat
代码:
public final class UndefinedOutputFormat extends OutputFormat {
public static final UndefinedOutputFormat INSTANCE = new UndefinedOutputFormat();
private UndefinedOutputFormat() {
// Only to decrease visibility
}
@Override
public boolean isOutputFormatMixingAllowed() {
return true;
}
@Override
public String getName() {
return "undefined";
}
@Override
public String getMimeType() {
return null;
}
}
作用:表示模板输出格式未确定时使用的输出格式。如果FreeMarker不能选择任何更具体的内容,这将会是默认的输出格式。这种格式不支持自动转义。
注:Freemarker代码来自FreeMarker 中文官方参考手册
新手写的代码分析,文章若有错误还请指出