FreeMarker代码分析(11)

2021SC@SDUSC

**内容非常主观,可能出现错漏,慎重参考

AliasTemplateNumberFormatFactory.java

package freemarker.core;

import java.util.Locale;
import java.util.Map;

import freemarker.template.utility.StringUtil;

/**
 * Creates an alias to another format, so that the format can be referred to with a simple name in the template, rather
 * than as a concrete pattern or other kind of format string.
 * 
 * @since 2.3.24
 */
public final class AliasTemplateNumberFormatFactory extends TemplateNumberFormatFactory {

    private final String defaultTargetFormatString;
    private final Map<Locale, String> localizedTargetFormatStrings;

    /**
     * @param targetFormatString
     *            The format string this format will be an alias to
     */
    public AliasTemplateNumberFormatFactory(String targetFormatString) {
        this.defaultTargetFormatString = targetFormatString;
        localizedTargetFormatStrings = null;
    }

    /**
     * @param defaultTargetFormatString
     *            The format string this format will be an alias to if there's no locale-specific format string for the
     *            requested locale in {@code localizedTargetFormatStrings}
     * @param localizedTargetFormatStrings
     *            Maps {@link Locale}-s to format strings. If the desired locale doesn't occur in the map, a less
     *            specific locale is tried, repeatedly until only the language part remains. For example, if locale is
     *            {@code new Locale("en", "US", "Linux")}, then these keys will be attempted untol a match is found, in
     *            this order: {@code new Locale("en", "US", "Linux")}, {@code new Locale("en", "US")},
     *            {@code new Locale("en")}. If there's still no matching key, the value of the
     *            {@code targetFormatString} will be used.
     */
    public AliasTemplateNumberFormatFactory(
            String defaultTargetFormatString, Map<Locale, String> localizedTargetFormatStrings) {
        this.defaultTargetFormatString = defaultTargetFormatString;
        this.localizedTargetFormatStrings = localizedTargetFormatStrings;
    }

    @Override
    public TemplateNumberFormat get(String params, Locale locale, Environment env)
            throws TemplateValueFormatException {
        TemplateFormatUtil.checkHasNoParameters(params);
        try {
            String targetFormatString;
            if (localizedTargetFormatStrings != null) {
                Locale lookupLocale = locale;
                targetFormatString = localizedTargetFormatStrings.get(lookupLocale);
                while (targetFormatString == null
                        && (lookupLocale = _CoreLocaleUtils.getLessSpecificLocale(lookupLocale)) != null) {
                    targetFormatString = localizedTargetFormatStrings.get(lookupLocale);
                }
            } else {
                targetFormatString = null;
            }
            if (targetFormatString == null) {
                targetFormatString = this.defaultTargetFormatString;
            }
            return env.getTemplateNumberFormat(targetFormatString, locale);
        } catch (TemplateValueFormatException e) {
            throw new AliasTargetTemplateValueFormatException("Failed to create format based on target format string,  "
                    + StringUtil.jQuote(params) + ". Reason given: " + e.getMessage(), e);
        }
    }

}

AndExpression.java

package freemarker.core;

import freemarker.template.TemplateException;

final class AndExpression extends BooleanExpression {

    private final Expression lho;
    private final Expression rho;

    AndExpression(Expression lho, Expression rho) {
        this.lho = lho;
        this.rho = rho;
    }

    @Override
    boolean evalToBoolean(Environment env) throws TemplateException {
        return lho.evalToBoolean(env) && rho.evalToBoolean(env);
    }

    @Override
    public String getCanonicalForm() {
        return lho.getCanonicalForm() + " && " + rho.getCanonicalForm();
    }

    @Override
    String getNodeTypeSymbol() {
        return "&&";
    }
    
    @Override
    boolean isLiteral() {
        return constantValue != null || (lho.isLiteral() && rho.isLiteral());
    }

    @Override
    protected Expression deepCloneWithIdentifierReplaced_inner(
            String replacedIdentifier, Expression replacement, ReplacemenetState replacementState) {
    	return new AndExpression(
    	        lho.deepCloneWithIdentifierReplaced(replacedIdentifier, replacement, replacementState),
    	        rho.deepCloneWithIdentifierReplaced(replacedIdentifier, replacement, replacementState));
    }
    
    @Override
    int getParameterCount() {
        return 2;
    }

    @Override
    Object getParameterValue(int idx) {
        switch (idx) {
        case 0: return lho;
        case 1: return rho;
        default: throw new IndexOutOfBoundsException();
        }
    }

    @Override
    ParameterRole getParameterRole(int idx) {
        return ParameterRole.forBinaryOperatorOperand(idx);
    }
    
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值