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);
}
}