Freemarker源码分析(4)cache.TemplateConfigurationFactory及其子类

2021SC@SDUSC

TemplateConfigurationFactory及其子类

总览图:
TemplateConfigurationFactory及其子类总览图

1.TemplateConfigurationFactory抽象类

public abstract class TemplateConfigurationFactory {
    
    private Configuration cfg;

    public abstract TemplateConfiguration get(String sourceName, Object templateSource)
            throws IOException, TemplateConfigurationFactoryException;
    
    public final void setConfiguration(Configuration cfg) {
        if (this.cfg != null) {
            if (cfg != this.cfg) {
                throw new IllegalStateException(
                        "The TemplateConfigurationFactory is already bound to another Configuration");
            }
            return;
        } else {
            this.cfg = cfg;
            setConfigurationOfChildren(cfg);
        }
    }
    
    public Configuration getConfiguration() {
        return cfg;
    }
    
    protected abstract void setConfigurationOfChildren(Configuration cfg);

}

作用:为模板源创建(或返回)TemplateConfiguration对象。

2.ConditionalTemplateConfigurationFactory类

代码:

public class ConditionalTemplateConfigurationFactory extends TemplateConfigurationFactory {

    private final TemplateSourceMatcher matcher;
    private final TemplateConfiguration templateConfiguration;
    private final TemplateConfigurationFactory templateConfigurationFactory;

    public ConditionalTemplateConfigurationFactory(
            TemplateSourceMatcher matcher, TemplateConfigurationFactory templateConfigurationFactory) {
        this.matcher = matcher;
        this.templateConfiguration = null;
        this.templateConfigurationFactory = templateConfigurationFactory;
    }
    
    public ConditionalTemplateConfigurationFactory(
            TemplateSourceMatcher matcher, TemplateConfiguration templateConfiguration) {
        this.matcher = matcher;
        this.templateConfiguration = templateConfiguration;
        this.templateConfigurationFactory = null;
    }

    @Override
    public TemplateConfiguration get(String sourceName, Object templateSource)
            throws IOException, TemplateConfigurationFactoryException {
        if (matcher.matches(sourceName, templateSource)) {
            if (templateConfigurationFactory != null) {
                return templateConfigurationFactory.get(sourceName, templateSource);
            } else {
                return templateConfiguration;
            }
        } else {
            return null;
        }
    }

    @Override
    protected void setConfigurationOfChildren(Configuration cfg) {
        if (templateConfiguration != null) {
            templateConfiguration.setParentConfiguration(cfg);
        }
        if (templateConfigurationFactory != null) {
            templateConfigurationFactory.setConfiguration(cfg);
        }
    }
    
}

作用:当指定的匹配器与模板源匹配时,直接返回给定的TemplateConfiguration对象或另一个TemplateConfigationFactory的结果。

3.FirstMatchTemplateConfigurationFactory

代码:

public class FirstMatchTemplateConfigurationFactory extends TemplateConfigurationFactory {
    
    private final TemplateConfigurationFactory[] templateConfigurationFactories;
    private boolean allowNoMatch;
    private String noMatchErrorDetails;
    
    public FirstMatchTemplateConfigurationFactory(TemplateConfigurationFactory... templateConfigurationFactories) {
        this.templateConfigurationFactories = templateConfigurationFactories;
    }

    @Override
    public TemplateConfiguration get(String sourceName, Object templateSource)
            throws IOException, TemplateConfigurationFactoryException {
        for (TemplateConfigurationFactory tcf : templateConfigurationFactories) {
            TemplateConfiguration tc = tcf.get(sourceName, templateSource); 
            if (tc != null) {
                return tc;
            }
        }
        if (!allowNoMatch) {
            throw new TemplateConfigurationFactoryException(
                    FirstMatchTemplateConfigurationFactory.class.getSimpleName()
                    + " has found no matching choice for source name "
                    + StringUtil.jQuote(sourceName) + ". "
                    + (noMatchErrorDetails != null
                            ? "Error details: " + noMatchErrorDetails 
                            : "(Set the noMatchErrorDetails property of the factory bean to give a more specific error "
                                    + "message. Set allowNoMatch to true if this shouldn't be an error.)"));
        }
        return null;
    }


    public boolean getAllowNoMatch() {
        return allowNoMatch;
    }

    public void setAllowNoMatch(boolean allowNoMatch) {
        this.allowNoMatch = allowNoMatch;
    }

    public String getNoMatchErrorDetails() {
        return noMatchErrorDetails;
    }

    
    public void setNoMatchErrorDetails(String noMatchErrorDetails) {
        this.noMatchErrorDetails = noMatchErrorDetails;
    }
    
    public FirstMatchTemplateConfigurationFactory allowNoMatch(boolean allow) {
        setAllowNoMatch(allow);
        return this;
    }

    public FirstMatchTemplateConfigurationFactory noMatchErrorDetails(String message) {
        setNoMatchErrorDetails(message);
        return this;
    }

    @Override
    protected void setConfigurationOfChildren(Configuration cfg) {
        for (TemplateConfigurationFactory templateConfigurationFactory : templateConfigurationFactories) {
            templateConfigurationFactory.setConfiguration(cfg);
        }
    }
    
}

作用:返回子工厂中第一个非空的结果,忽略之后的子工厂。按照子工厂被添加的顺序调用。

4.MergingTemplateConfigurationFactory

代码:

public class MergingTemplateConfigurationFactory extends TemplateConfigurationFactory {
    
    private final TemplateConfigurationFactory[] templateConfigurationFactories;
    
    public MergingTemplateConfigurationFactory(TemplateConfigurationFactory... templateConfigurationFactories) {
        this.templateConfigurationFactories = templateConfigurationFactories;
    }

    @Override
    public TemplateConfiguration get(String sourceName, Object templateSource)
            throws IOException, TemplateConfigurationFactoryException {
        TemplateConfiguration mergedTC = null;
        TemplateConfiguration resultTC = null;
        for (TemplateConfigurationFactory tcf : templateConfigurationFactories) {
            TemplateConfiguration tc = tcf.get(sourceName, templateSource);
            if (tc != null) {
                if (resultTC == null) {
                    resultTC = tc;
                } else {
                    if (mergedTC == null) {
                        Configuration cfg = getConfiguration();
                        if (cfg == null) {
                            throw new IllegalStateException(
                                    "The TemplateConfigurationFactory wasn't associated to a Configuration yet.");
                        }
                        
                        mergedTC = new TemplateConfiguration();
                        mergedTC.setParentConfiguration(cfg);
                        mergedTC.merge(resultTC);
                        resultTC = mergedTC;
                    }
                    mergedTC.merge(tc);
                }
            }
        }
        return resultTC;
    }
    
    @Override
    protected void setConfigurationOfChildren(Configuration cfg) {
        for (TemplateConfigurationFactory templateConfigurationFactory : templateConfigurationFactories) {
            templateConfigurationFactory.setConfiguration(cfg);
        }
    }

}

作用:返回所有子工厂的合并结果。工厂按照添加的顺序合并。值为null的子工厂的结果将被忽略

5.TemplateConfigurationFactoryException类

代码:

public class TemplateConfigurationFactoryException extends Exception {

    public TemplateConfigurationFactoryException(String message) {
        super(message);
    }

    public TemplateConfigurationFactoryException(String message, Throwable cause) {
        super(message, cause);
    }

}

作用:并不是TemplateConfigurationFactory的子类,而是继承自Exception的一个异常类,定义了由TemplateConfigurationFactory抛出的IO Exception

注:Freemarker代码来自FreeMarker 中文官方参考手册

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值