This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.scripting.thymeleaf-1.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-thymeleaf.git
commit e6340bbebf2535d60a57257d38b168f08f46fcd5 Author: Oliver Lietz <[email protected]> AuthorDate: Mon Dec 7 22:43:24 2015 +0000 SLING-5075 Upgrade Thymeleaf to 3.0 * adjust to new Context API * adjust to new Message Resolution API * cleanup git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/thymeleaf@1718487 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/scripting/thymeleaf/SlingContext.java | 30 ---------------- .../thymeleaf/internal/DefaultSlingContext.java | 42 ---------------------- .../internal/ResourceBundleMessageResolver.java | 30 ++++++++-------- .../thymeleaf/internal/ThymeleafScriptEngine.java | 7 ++-- 4 files changed, 17 insertions(+), 92 deletions(-) diff --git a/src/main/java/org/apache/sling/scripting/thymeleaf/SlingContext.java b/src/main/java/org/apache/sling/scripting/thymeleaf/SlingContext.java deleted file mode 100644 index fa434a9..0000000 --- a/src/main/java/org/apache/sling/scripting/thymeleaf/SlingContext.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.sling.scripting.thymeleaf; - -import aQute.bnd.annotation.ProviderType; -import org.apache.sling.api.resource.ResourceResolver; -import org.thymeleaf.context.IContext; - -@ProviderType -public interface SlingContext extends IContext { - - ResourceResolver getResourceResolver(); - -} diff --git a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/DefaultSlingContext.java b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/DefaultSlingContext.java deleted file mode 100644 index a8b6ac4..0000000 --- a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/DefaultSlingContext.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.sling.scripting.thymeleaf.internal; - -import java.util.Locale; -import java.util.Map; - -import org.apache.sling.api.resource.ResourceResolver; -import org.apache.sling.scripting.thymeleaf.SlingContext; -import org.thymeleaf.context.AbstractContext; - -public final class DefaultSlingContext extends AbstractContext implements SlingContext { - - private final ResourceResolver resourceResolver; - - public DefaultSlingContext(final ResourceResolver resourceResolver, final Locale locale, final Map<String, Object> variables) { - super(locale, variables); - this.resourceResolver = resourceResolver; - } - - @Override - public ResourceResolver getResourceResolver() { - return resourceResolver; - } - -} diff --git a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ResourceBundleMessageResolver.java b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ResourceBundleMessageResolver.java index bfeb2db..03ee718 100644 --- a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ResourceBundleMessageResolver.java +++ b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ResourceBundleMessageResolver.java @@ -18,7 +18,10 @@ */ package org.apache.sling.scripting.thymeleaf.internal; +import java.text.MessageFormat; import java.util.Dictionary; +import java.util.Locale; +import java.util.ResourceBundle; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; @@ -100,26 +103,21 @@ public class ResourceBundleMessageResolver implements IMessageResolver { } @Override - public String resolveMessage(ITemplateContext context, Class<?> origin, String key, Object[] messageParameters) { - return null; - } - - @Override - public String createAbsentMessageRepresentation(ITemplateContext context, Class<?> origin, String key, Object[] messageParameters) { - return null; - } - - /* - @Override - public MessageResolution resolveMessage(final ITemplateProcessingContext processingContext, final String key, final Object[] messageParameters) { - logger.debug("processingContext: {}, key: {}, message parameters: {}", processingContext, key, messageParameters); - final Locale locale = processingContext.getLocale(); + public String resolveMessage(final ITemplateContext templateContext, final Class<?> origin, final String key, final Object[] messageParameters) { + logger.debug("template context: {}, origin: {}, key: {}, message parameters: {}", templateContext, origin, key, messageParameters); + // TODO can origin be useful with Sling i18n? + final Locale locale = templateContext.getLocale(); final ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale); final String string = resourceBundle.getString(key); final MessageFormat messageFormat = new MessageFormat(string, locale); final String message = messageFormat.format((messageParameters != null ? messageParameters : EMPTY_MESSAGE_PARAMETERS)); - return new MessageResolution(message); + logger.debug("message: '{}'", message); + return message; + } + + @Override + public String createAbsentMessageRepresentation(final ITemplateContext templateContext, final Class<?> origin, final String key, final Object[] messageParameters) { + return key; // TODO make configurable } - */ } diff --git a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ThymeleafScriptEngine.java b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ThymeleafScriptEngine.java index 736bbb4..f0fd01c 100644 --- a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ThymeleafScriptEngine.java +++ b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/ThymeleafScriptEngine.java @@ -33,6 +33,7 @@ import org.apache.sling.api.scripting.SlingScriptHelper; import org.apache.sling.scripting.api.AbstractSlingScriptEngine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.thymeleaf.context.Context; import org.thymeleaf.context.IContext; public final class ThymeleafScriptEngine extends AbstractSlingScriptEngine { @@ -64,12 +65,10 @@ public final class ThymeleafScriptEngine extends AbstractSlingScriptEngine { final String scriptName = helper.getScript().getScriptResource().getPath(); final Writer writer = scriptContext.getWriter(); - bindings.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, resourceResolver); // TODO #388 + bindings.put(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, resourceResolver); // TODO SlingBindings.RESOLVER try { - final IContext context = new DefaultSlingContext(resourceResolver, locale, bindings); - // TODO optimize, process() calls TemplateManager which does resolving, parsing and processing - // resolving is already done by Sling, so we need a parsing and processing only call into TemplateEngine + final IContext context = new Context(locale, bindings); thymeleafScriptEngineFactory.getTemplateEngine().process(scriptName, context, writer); } catch (Exception e) { logger.error("Failure rendering Thymeleaf template '{}': {}", scriptName, e.getMessage()); -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
