Repository: incubator-freemarker Updated Branches: refs/heads/3 dc689993a -> 24673a17b
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/24673a17/freemarker-core/src/main/java/org/apache/freemarker/core/Template.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/Template.java b/freemarker-core/src/main/java/org/apache/freemarker/core/Template.java index f165990..feac008 100644 --- a/freemarker-core/src/main/java/org/apache/freemarker/core/Template.java +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/Template.java @@ -111,12 +111,12 @@ public class Template implements ProcessingConfiguration, CustomStateScope { private Map prefixToNamespaceURILookup = new HashMap(); private Map namespaceURIToPrefixLookup = new HashMap(); /** Custom attributes specified inside the template with the #ftl directive. Maybe {@code null}. */ - private Map<String, Serializable> headerCustomAttributes; + private Map<String, Serializable> headerCustomSettings; /** - * In case {@link #headerCustomAttributes} is not {@code null} and the {@link TemplateConfiguration} also specifies + * In case {@link #headerCustomSettings} is not {@code null} and the {@link TemplateConfiguration} also specifies * custom attributes, this is the two set of custom attributes merged. Otherwise it's {@code null}. */ - private transient Map<Serializable, Object> tcAndHeaderCustomAttributes; + private transient Map<Serializable, Object> tcAndHeaderCustomSettings; private AutoEscapingPolicy autoEscapingPolicy; // Values from template content that are detected automatically: @@ -345,8 +345,8 @@ public class Template implements ProcessingConfiguration, CustomStateScope { * protected when construction is done. */ private void finishConstruction() { - headerCustomAttributes = _CollectionUtil.unmodifiableMap(headerCustomAttributes); - tcAndHeaderCustomAttributes = _CollectionUtil.unmodifiableMap(tcAndHeaderCustomAttributes); + headerCustomSettings = _CollectionUtil.unmodifiableMap(headerCustomSettings); + tcAndHeaderCustomSettings = _CollectionUtil.unmodifiableMap(tcAndHeaderCustomSettings); writeProtected = true; } @@ -1068,24 +1068,24 @@ public class Template implements ProcessingConfiguration, CustomStateScope { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override - public Map<Serializable, Object> getCustomAttributesSnapshot(boolean includeInherited) { + public Map<Serializable, Object> getCustomSettingsSnapshot(boolean includeInherited) { boolean nonInheritedAttrsFinal; Map<? extends Serializable, ? extends Object> nonInheritedAttrs; - if (tcAndHeaderCustomAttributes != null) { - nonInheritedAttrs = tcAndHeaderCustomAttributes; + if (tcAndHeaderCustomSettings != null) { + nonInheritedAttrs = tcAndHeaderCustomSettings; nonInheritedAttrsFinal = writeProtected; - } else if (headerCustomAttributes != null) { - nonInheritedAttrs = headerCustomAttributes; + } else if (headerCustomSettings != null) { + nonInheritedAttrs = headerCustomSettings; nonInheritedAttrsFinal = writeProtected; } else if (tCfg != null) { - nonInheritedAttrs = tCfg.getCustomAttributesSnapshot(false); + nonInheritedAttrs = tCfg.getCustomSettingsSnapshot(false); nonInheritedAttrsFinal = true; } else { nonInheritedAttrs = Collections.emptyMap(); nonInheritedAttrsFinal = true; } - Map<Serializable, Object> inheritedAttrs = includeInherited ? cfg.getCustomAttributesSnapshot(true) + Map<Serializable, Object> inheritedAttrs = includeInherited ? cfg.getCustomSettingsSnapshot(true) : Collections.<Serializable, Object>emptyMap(); LinkedHashMap<Serializable, Object> mergedAttrs; @@ -1103,65 +1103,65 @@ public class Template implements ProcessingConfiguration, CustomStateScope { } @Override - public boolean isCustomAttributeSet(Serializable key) { - if (tcAndHeaderCustomAttributes != null) { - return tcAndHeaderCustomAttributes.containsKey(key); + public boolean isCustomSettingSet(Serializable key) { + if (tcAndHeaderCustomSettings != null) { + return tcAndHeaderCustomSettings.containsKey(key); } - return headerCustomAttributes != null && headerCustomAttributes.containsKey(key) - || tCfg != null && tCfg.isCustomAttributeSet(key); + return headerCustomSettings != null && headerCustomSettings.containsKey(key) + || tCfg != null && tCfg.isCustomSettingSet(key); } @Override - public Object getCustomAttribute(Serializable key) { - return getCustomAttribute(key, null, false); + public Object getCustomSetting(Serializable key) { + return getCustomSetting(key, null, false); } @Override - public Object getCustomAttribute(Serializable key, Object defaultValue) { - return getCustomAttribute(key, defaultValue, true); + public Object getCustomSetting(Serializable key, Object defaultValue) { + return getCustomSetting(key, defaultValue, true); } - private Object getCustomAttribute(Serializable key, Object defaultValue, boolean useDefaultValue) { - if (tcAndHeaderCustomAttributes != null) { - Object value = tcAndHeaderCustomAttributes.get(key); - if (value != null || tcAndHeaderCustomAttributes.containsKey(key)) { + private Object getCustomSetting(Serializable key, Object defaultValue, boolean useDefaultValue) { + if (tcAndHeaderCustomSettings != null) { + Object value = tcAndHeaderCustomSettings.get(key); + if (value != null || tcAndHeaderCustomSettings.containsKey(key)) { return value; } } else { - if (headerCustomAttributes != null) { - Object value = headerCustomAttributes.get(key); - if (value != null || headerCustomAttributes.containsKey(key)) { + if (headerCustomSettings != null) { + Object value = headerCustomSettings.get(key); + if (value != null || headerCustomSettings.containsKey(key)) { return value; } } if (tCfg != null) { - Object value = tCfg.getCustomAttribute(key, MISSING_VALUE_MARKER); + Object value = tCfg.getCustomSetting(key, MISSING_VALUE_MARKER); if (value != MISSING_VALUE_MARKER) { return value; } } } - return useDefaultValue ? cfg.getCustomAttribute(key, defaultValue) : cfg.getCustomAttribute(key); + return useDefaultValue ? cfg.getCustomSetting(key, defaultValue) : cfg.getCustomSetting(key); } /** * Should be called by the parser, for example to add the attributes specified in the #ftl header. */ - void setHeaderCustomAttribute(String attName, Serializable attValue) { + void setHeaderCustomSetting(String attName, Serializable attValue) { checkWritable(); - if (headerCustomAttributes == null) { - headerCustomAttributes = new LinkedHashMap<>(); + if (headerCustomSettings == null) { + headerCustomSettings = new LinkedHashMap<>(); } - headerCustomAttributes.put(attName, attValue); + headerCustomSettings.put(attName, attValue); if (tCfg != null) { - Map<Serializable, Object> tcCustAttrs = tCfg.getCustomAttributesSnapshot(false); + Map<Serializable, Object> tcCustAttrs = tCfg.getCustomSettingsSnapshot(false); if (!tcCustAttrs.isEmpty()) { - if (tcAndHeaderCustomAttributes == null) { - tcAndHeaderCustomAttributes = new LinkedHashMap<>(tcCustAttrs); + if (tcAndHeaderCustomSettings == null) { + tcAndHeaderCustomSettings = new LinkedHashMap<>(tcCustAttrs); } - tcAndHeaderCustomAttributes.put(attName, attValue); + tcAndHeaderCustomSettings.put(attName, attValue); } } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/24673a17/freemarker-core/src/main/java/org/apache/freemarker/core/TemplateConfiguration.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/TemplateConfiguration.java b/freemarker-core/src/main/java/org/apache/freemarker/core/TemplateConfiguration.java index f727002..ca37fa6 100644 --- a/freemarker-core/src/main/java/org/apache/freemarker/core/TemplateConfiguration.java +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/TemplateConfiguration.java @@ -83,7 +83,7 @@ public final class TemplateConfiguration implements ParsingAndProcessingConfigur private final Boolean lazyImports; private final Boolean lazyAutoImports; private final boolean lazyAutoImportsSet; - private final Map<Serializable, Object> customAttributes; + private final Map<Serializable, Object> customSettings; private final TemplateLanguage templateLanguage; private final TagSyntax tagSyntax; @@ -124,7 +124,7 @@ public final class TemplateConfiguration implements ParsingAndProcessingConfigur lazyImports = builder.isLazyImportsSet() ? builder.getLazyImports() : null; lazyAutoImportsSet = builder.isLazyAutoImportsSet(); lazyAutoImports = lazyAutoImportsSet ? builder.getLazyAutoImports() : null; - customAttributes = builder.getCustomAttributesSnapshot(false); + customSettings = builder.getCustomSettingsSnapshot(false); templateLanguage = builder.isTemplateLanguageSet() ? builder.getTemplateLanguage() : null; tagSyntax = builder.isTagSyntaxSet() ? builder.getTagSyntax() : null; @@ -634,28 +634,28 @@ public final class TemplateConfiguration implements ParsingAndProcessingConfigur * Note that the {@code includeInherited} has no effect here, as {@link TemplateConfiguration}-s has no parent. */ @Override - public Map<Serializable, Object> getCustomAttributesSnapshot(boolean includeInherited) { - return customAttributes; + public Map<Serializable, Object> getCustomSettingsSnapshot(boolean includeInherited) { + return customSettings; } @Override - public boolean isCustomAttributeSet(Serializable key) { - return customAttributes.containsKey(key); + public boolean isCustomSettingSet(Serializable key) { + return customSettings.containsKey(key); } @Override - public Object getCustomAttribute(Serializable key) { - Object result = getCustomAttribute(key, MISSING_VALUE_MARKER); + public Object getCustomSetting(Serializable key) { + Object result = getCustomSetting(key, MISSING_VALUE_MARKER); if (result == MISSING_VALUE_MARKER) { - throw new CustomAttributeNotSetException(key); + throw new CustomSettingNotSetException(key); } return result; } @Override - public Object getCustomAttribute(Serializable key, Object defaultValue) { - Object attValue = customAttributes.get(key); - if (attValue != null || customAttributes.containsKey(key)) { + public Object getCustomSetting(Serializable key, Object defaultValue) { + Object attValue = customSettings.get(key); + if (attValue != null || customSettings.containsKey(key)) { return attValue; } return defaultValue; @@ -804,16 +804,16 @@ public final class TemplateConfiguration implements ParsingAndProcessingConfigur } @Override - protected Object getDefaultCustomAttribute(Serializable key, Object defaultValue, boolean useDefaultValue) { + protected Object getDefaultCustomSetting(Serializable key, Object defaultValue, boolean useDefaultValue) { // We don't inherit from anything. if (useDefaultValue) { return defaultValue; } - throw new CustomAttributeNotSetException(key); + throw new CustomSettingNotSetException(key); } @Override - protected void collectDefaultCustomAttributesSnapshot(Map<Serializable, Object> target) { + protected void collectDefaultCustomSettingsSnapshot(Map<Serializable, Object> target) { // We don't inherit from anything. } @@ -934,9 +934,9 @@ public final class TemplateConfiguration implements ParsingAndProcessingConfigur tc.isAutoIncludesSet() ? tc.getAutoIncludes() : null)); } - setCustomAttributesMap(mergeMaps( - getCustomAttributesSnapshot(false), - tc.getCustomAttributesSnapshot(false), + setCustomSettingsMap(mergeMaps( + getCustomSettingsSnapshot(false), + tc.getCustomSettingsSnapshot(false), true)); } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/24673a17/freemarker-core/src/main/javacc/FTL.jj ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/javacc/FTL.jj b/freemarker-core/src/main/javacc/FTL.jj index 0be9d81..552f236 100644 --- a/freemarker-core/src/main/javacc/FTL.jj +++ b/freemarker-core/src/main/javacc/FTL.jj @@ -3955,9 +3955,9 @@ void HeaderElement() : } } catch (TemplateModelException tme) { } - } else if (ks.equalsIgnoreCase("attributes")) { + } else if (ks.equalsIgnoreCase("custom_settings") || ks.equals("customSettings")) { if (!(value instanceof TemplateHashModelEx)) { - throw new ParseException("Expecting a hash of attribute names to values.", exp); + throw new ParseException("Expecting a hash value for custom settings.", exp); } TemplateHashModelEx attributeMap = (TemplateHashModelEx) value; try { @@ -3967,11 +3967,11 @@ void HeaderElement() : Object attValue = DeepUnwrap.unwrap(attributeMap.get(attName)); if (attValue != null && !(attValue instanceof Serializable)) { throw new ParseException( - "Value of attribute " + _StringUtil.jQuote(attName) + "Value of custom setting " + _StringUtil.jQuote(attName) + " should implement java.io.Serializable.", exp); } - template.setHeaderCustomAttribute(attName, (Serializable) attValue); + template.setHeaderCustomSetting(attName, (Serializable) attValue); } } catch (TemplateModelException tme) { } @@ -3979,6 +3979,9 @@ void HeaderElement() : String correctName; if (ks.equals("charset") || ks.equals("source_encoding") || ks.equals("sourceEncoding")) { correctName = "encoding"; + } else if (ks.equals("attributes")) { + correctName = token_source.namingConvention == NamingConvention.CAMEL_CASE + ? "customSettings" : "custom_settings"; } else if (ks.equals("xmlns")) { // [2.4] If camel case will be the default, update this correctName http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/24673a17/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java ---------------------------------------------------------------------- diff --git a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java index f4a4895..016e24d 100644 --- a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java +++ b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java @@ -902,7 +902,7 @@ public class FreemarkerServlet extends HttpServlet { } private ContentType getTemplateSpecificContentType(final Template template) { - Object contentTypeAttr = template.getCustomAttribute("content_type", null); + Object contentTypeAttr = template.getCustomSetting("content_type", null); if (contentTypeAttr != null) { // Converted with toString() for backward compatibility. return new ContentType(contentTypeAttr.toString()); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/24673a17/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java ---------------------------------------------------------------------- diff --git a/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java b/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java index e3c05d4..2b25a29 100644 --- a/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java +++ b/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java @@ -44,8 +44,6 @@ import org.apache.freemarker.core.templateresolver.FirstMatchTemplateConfigurati import org.apache.freemarker.core.templateresolver.TemplateConfigurationFactory; import org.apache.freemarker.core.templateresolver.TemplateLoader; import org.apache.freemarker.core.templateresolver.impl.ByteArrayTemplateLoader; -import org.apache.freemarker.servlet.FreemarkerServlet; -import org.apache.freemarker.servlet.FreemarkerServletConfigurationBuilder; import org.junit.Before; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; @@ -314,7 +312,7 @@ public class FreemarkerServletTest { } catch (ServletException e) { assertThat(e.getCause().getCause().getMessage(), containsString(FreemarkerServlet.INIT_PARAM_VALUE_LEGACY)); } - // But the legacy content_type template attribute can still set the output charset: + // But the legacy content_type template custom setting can still set the output charset: assertOutputEncodingEquals( StandardCharsets.UTF_8, // <- expected response.characterEncoding StandardCharsets.UTF_8, // <- expected env.outputEncoding @@ -349,7 +347,7 @@ public class FreemarkerServletTest { } catch (ServletException e) { assertThat(e.getCause().getCause().getMessage(), containsString(FreemarkerServlet.INIT_PARAM_VALUE_LEGACY)); } - // The legacy content_type template attribute can still specify an output charset, though it will be ignored: + // The legacy content_type template custom setting can still specify an output charset, though it will be ignored: assertOutputEncodingEquals( SERVLET_RESPONSE_DEFAULT_CHARSET, // <- expected response.characterEncoding SERVLET_RESPONSE_DEFAULT_CHARSET, // <- expected env.outputEncoding @@ -394,7 +392,7 @@ public class FreemarkerServletTest { } catch (ServletException e) { assertThat(e.getCause().getCause().getMessage(), containsString(FreemarkerServlet.INIT_PARAM_VALUE_LEGACY)); } - // The legacy content_type template attribute can still specify an output charset, though it will be overridden: + // The legacy content_type template custom setting can still specify an output charset, though it will be overridden: assertOutputEncodingEquals( StandardCharsets.UTF_16LE, // <- expected response.characterEncoding StandardCharsets.UTF_16LE, // <- expected env.outputEncoding @@ -583,10 +581,10 @@ public class FreemarkerServletTest { .getBytes(StandardCharsets.UTF_8)); tl.putTemplate(FOO_OUT_UTF8_FTL, "foo" .getBytes(StandardCharsets.UTF_8)); - tl.putTemplate(CONTENT_TYPE_ATTR_FTL, "<#ftl attributes={ 'content_type': 'text/plain' }>foo" + tl.putTemplate(CONTENT_TYPE_ATTR_FTL, "<#ftl customSettings={ 'content_type': 'text/plain' }>foo" .getBytes(StandardCharsets.UTF_8)); tl.putTemplate(CONTENT_TYPE_ATTR_WITH_CHARSET_FTL, - "<#ftl attributes={ 'content_type': 'text/plain; charset=UTF-8' }>foo" + "<#ftl customSettings={ 'content_type': 'text/plain; charset=UTF-8' }>foo" .getBytes(StandardCharsets.UTF_8)); tl.putTemplate(OUTPUT_FORMAT_HEADER_FTL, "<#ftl outputFormat='plainText'>foo" .getBytes(StandardCharsets.UTF_8)); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/24673a17/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/jsp/RealServletContainertTest.java ---------------------------------------------------------------------- diff --git a/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/jsp/RealServletContainertTest.java b/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/jsp/RealServletContainertTest.java index 053ef32..32412a9 100644 --- a/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/jsp/RealServletContainertTest.java +++ b/freemarker-servlet/src/test/java/org/apache/freemarker/servlet/jsp/RealServletContainertTest.java @@ -87,7 +87,7 @@ public class RealServletContainertTest extends WebAppTestCase { } @Test - public void basicCustomAttributes() throws Exception { + public void basicServletScopeAttributes() throws Exception { restartWebAppIfStarted(WEBAPP_BASIC); // To clear the application scope attributes assertExpectedEqualsOutput(WEBAPP_BASIC, "attributes.txt", "tester" + "?action=" + AllKindOfContainersModel2Action.class.getName()