Freemarker源码分析(10)core.OutputFormat及其子类

2021SC@SDUSC

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 中文官方参考手册

新手写的代码分析,文章若有错误还请指出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值