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.1.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-thymeleaf.git
commit dd8a8a5abcf809122b0202376ce825b5fdebb96b Author: Oliver Lietz <[email protected]> AuthorDate: Thu Jan 26 21:18:48 2017 +0000 SLING-6486 Use single pattern (regular expression) per template mode in PatternTemplateModeProvider git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/scripting/org.apache.sling.scripting.thymeleaf@1780481 13f79535-47bb-0310-9956-ffa450edef68 --- .../internal/PatternTemplateModeProvider.java | 61 ++++++--------- .../PatternTemplateModeProviderConfiguration.java | 46 +++++------ .../it/tests/PatternTemplateModeProviderIT.java | 89 ++++++++++++++++++++++ 3 files changed, 132 insertions(+), 64 deletions(-) diff --git a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProvider.java b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProvider.java index 69d5fd4..b25c0c2 100644 --- a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProvider.java +++ b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProvider.java @@ -18,9 +18,7 @@ */ package org.apache.sling.scripting.thymeleaf.internal; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; +import java.util.regex.Pattern; import org.apache.sling.api.resource.Resource; import org.apache.sling.scripting.thymeleaf.TemplateModeProvider; @@ -33,7 +31,6 @@ import org.osgi.service.metatype.annotations.Designate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.thymeleaf.templatemode.TemplateMode; -import org.thymeleaf.util.PatternSpec; @Component( immediate = true, @@ -47,17 +44,17 @@ import org.thymeleaf.util.PatternSpec; ) public class PatternTemplateModeProvider implements TemplateModeProvider { - private final PatternSpec htmlPatternSpec = new PatternSpec(); + private Pattern htmlPattern; - private final PatternSpec xmlPatternSpec = new PatternSpec(); + private Pattern xmlPattern; - private final PatternSpec textPatternSpec = new PatternSpec(); + private Pattern textPattern; - private final PatternSpec javascriptPatternSpec = new PatternSpec(); + private Pattern javascriptPattern; - private final PatternSpec cssPatternSpec = new PatternSpec(); + private Pattern cssPattern; - private final PatternSpec rawPatternSpec = new PatternSpec(); + private Pattern rawPattern; private final Logger logger = LoggerFactory.getLogger(PatternTemplateModeProvider.class); @@ -83,52 +80,44 @@ public class PatternTemplateModeProvider implements TemplateModeProvider { private void configure(final PatternTemplateModeProviderConfiguration configuration) { // HTML - setPatterns(configuration.htmlPatterns(), htmlPatternSpec); - logger.debug("configured HTML patterns: {}", htmlPatternSpec.getPatterns()); + htmlPattern = Pattern.compile(configuration.htmlPattern()); + logger.debug("configured HTML pattern: {}", htmlPattern.pattern()); // XML - setPatterns(configuration.xmlPatterns(), xmlPatternSpec); - logger.debug("configured XML patterns: {}", xmlPatternSpec.getPatterns()); + xmlPattern = Pattern.compile(configuration.xmlPattern()); + logger.debug("configured XML pattern: {}", xmlPattern.pattern()); // TEXT - setPatterns(configuration.textPatterns(), textPatternSpec); - logger.debug("configured TEXT patterns: {}", textPatternSpec.getPatterns()); + textPattern = Pattern.compile(configuration.textPattern()); + logger.debug("configured TEXT pattern: {}", textPattern.pattern()); // JAVASCRIPT - setPatterns(configuration.javascriptPatterns(), javascriptPatternSpec); - logger.debug("configured JAVASCRIPT patterns: {}", javascriptPatternSpec.getPatterns()); + javascriptPattern = Pattern.compile(configuration.javascriptPattern()); + logger.debug("configured JAVASCRIPT pattern: {}", javascriptPattern.pattern()); // CSS - setPatterns(configuration.cssPatterns(), cssPatternSpec); - logger.debug("configured CSS patterns: {}", cssPatternSpec.getPatterns()); + cssPattern = Pattern.compile(configuration.cssPattern()); + logger.debug("configured CSS pattern: {}", cssPattern.pattern()); // RAW - setPatterns(configuration.rawPatterns(), rawPatternSpec); - logger.debug("configured RAW patterns: {}", rawPatternSpec.getPatterns()); - } - - private void setPatterns(final String[] strings, final PatternSpec patternSpec) { - final Set<String> set = new HashSet<String>(); - if (strings != null) { - Collections.addAll(set, strings); - } - patternSpec.setPatterns(set); + rawPattern = Pattern.compile(configuration.rawPattern()); + logger.debug("configured RAW pattern: {}", rawPattern.pattern()); } @Override public TemplateMode provideTemplateMode(final Resource resource) { final String path = resource.getPath(); - if (htmlPatternSpec.matches(path)) { + if (htmlPattern.matcher(path).matches()) { return TemplateMode.HTML; } - if (xmlPatternSpec.matches(path)) { + if (xmlPattern.matcher(path).matches()) { return TemplateMode.XML; } - if (textPatternSpec.matches(path)) { + if (textPattern.matcher(path).matches()) { return TemplateMode.TEXT; } - if (javascriptPatternSpec.matches(path)) { + if (javascriptPattern.matcher(path).matches()) { return TemplateMode.JAVASCRIPT; } - if (cssPatternSpec.matches(path)) { + if (cssPattern.matcher(path).matches()) { return TemplateMode.CSS; } - if (rawPatternSpec.matches(path)) { + if (rawPattern.matcher(path).matches()) { return TemplateMode.RAW; } return null; diff --git a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProviderConfiguration.java b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProviderConfiguration.java index 3aec55c..8b692ef 100644 --- a/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProviderConfiguration.java +++ b/src/main/java/org/apache/sling/scripting/thymeleaf/internal/PatternTemplateModeProviderConfiguration.java @@ -28,49 +28,39 @@ import org.osgi.service.metatype.annotations.ObjectClassDefinition; @interface PatternTemplateModeProviderConfiguration { @AttributeDefinition( - name = "patterns for template mode HTML", - description = "The template patterns (regular expressions) for templates which should be processed with template mode HTML (e.g. *.html - NOTE: extension needs to be enabled for this script engine)." + name = "pattern for template mode HTML", + description = "The template patterns (regular expression) for templates which should be processed with template mode HTML (e.g. *.html - NOTE: extension needs to be enabled for this script engine)." ) - String[] htmlPatterns() default { - "*.html" - }; + String htmlPattern() default "^.+\\.html$"; @AttributeDefinition( - name = "patterns for template mode XML", - description = "The template patterns (regular expressions) for templates which should be processed with template mode XML (e.g. *.xml - NOTE: extension needs to be enabled for this script engine)." + name = "pattern for template mode XML", + description = "The template pattern (regular expression) for templates which should be processed with template mode XML (e.g. *.xml - NOTE: extension needs to be enabled for this script engine)." ) - String[] xmlPatterns() default { - "*.xml" - }; + String xmlPattern() default "^.+\\.xml$"; @AttributeDefinition( - name = "patterns for template mode TEXT", - description = "The template patterns (regular expressions) for templates which should be processed with template mode TEXT (e.g. *.txt - NOTE: extension needs to be enabled for this script engine)." + name = "pattern for template mode TEXT", + description = "The template pattern (regular expression) for templates which should be processed with template mode TEXT (e.g. *.txt - NOTE: extension needs to be enabled for this script engine)." ) - String[] textPatterns() default { - "*.txt" - }; + String textPattern() default "^.+\\.txt$"; @AttributeDefinition( - name = "patterns for template mode JAVASCRIPT", - description = "The template patterns (regular expressions) for templates which should be processed with template mode JAVASCRIPT (e.g. *.js - NOTE: extension needs to be enabled for this script engine)." + name = "pattern for template mode JAVASCRIPT", + description = "The template pattern (regular expression) for templates which should be processed with template mode JAVASCRIPT (e.g. *.js - NOTE: extension needs to be enabled for this script engine)." ) - String[] javascriptPatterns() default { - "*.js" - }; + String javascriptPattern() default "^.+\\.js$"; @AttributeDefinition( - name = "patterns for template mode CSS", - description = "The template patterns (regular expressions) for templates which should be processed with template mode CSS (e.g. *.css - NOTE: extension needs to be enabled for this script engine)." + name = "pattern for template mode CSS", + description = "The template pattern (regular expression) for templates which should be processed with template mode CSS (e.g. *.css - NOTE: extension needs to be enabled for this script engine)." ) - String[] cssPatterns() default { - "*.css" - }; + String cssPattern() default "^.+\\.css$"; @AttributeDefinition( - name = "patterns for template mode RAW", - description = "The template patterns (regular expressions) for templates which should be processed with template mode RAW (e.g. *.raw - NOTE: extension needs to be enabled for this script engine)." + name = "pattern for template mode RAW", + description = "The template pattern (regular expression) for templates which should be processed with template mode RAW (e.g. *.raw - NOTE: extension needs to be enabled for this script engine)." ) - String[] rawPatterns(); + String rawPattern() default "^.+$"; } diff --git a/src/test/java/org/apache/sling/scripting/thymeleaf/it/tests/PatternTemplateModeProviderIT.java b/src/test/java/org/apache/sling/scripting/thymeleaf/it/tests/PatternTemplateModeProviderIT.java new file mode 100644 index 0000000..b401055 --- /dev/null +++ b/src/test/java/org/apache/sling/scripting/thymeleaf/it/tests/PatternTemplateModeProviderIT.java @@ -0,0 +1,89 @@ +/* + * 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.it.tests; + +import javax.inject.Inject; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.SyntheticResource; +import org.apache.sling.scripting.thymeleaf.TemplateModeProvider; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.junit.PaxExam; +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; +import org.ops4j.pax.exam.spi.reactors.PerClass; +import org.thymeleaf.templatemode.TemplateMode; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@RunWith(PaxExam.class) +@ExamReactorStrategy(PerClass.class) +public class PatternTemplateModeProviderIT extends ThymeleafTestSupport { + + @Inject + protected TemplateModeProvider templateModeProvider; + + private static Resource mockResource(final String path) { + return new SyntheticResource(null, path, null); + } + + @Test + public void provideTemplateMode_HTML() throws Exception { + final Resource resource = mockResource("/apps/thymeleaf/page/foo.html"); + final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource); + assertThat(templateMode, is(TemplateMode.HTML)); + } + + @Test + public void provideTemplateMode_XML() throws Exception { + final Resource resource = mockResource("/apps/thymeleaf/page/foo.xml"); + final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource); + assertThat(templateMode, is(TemplateMode.XML)); + } + + @Test + public void provideTemplateMode_TEXT() throws Exception { + final Resource resource = mockResource("/apps/thymeleaf/text/foo.txt"); + final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource); + assertThat(templateMode, is(TemplateMode.TEXT)); + } + + @Test + public void provideTemplateMode_JAVASCRIPT() throws Exception { + final Resource resource = mockResource("/apps/thymeleaf/assets/foo.js"); + final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource); + assertThat(templateMode, is(TemplateMode.JAVASCRIPT)); + } + + @Test + public void provideTemplateMode_CSS() throws Exception { + final Resource resource = mockResource("/apps/thymeleaf/assets/foo.css"); + final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource); + assertThat(templateMode, is(TemplateMode.CSS)); + } + + @Test + public void provideTemplateMode_fall_through() throws Exception { + final Resource resource = mockResource("foohtml"); + final TemplateMode templateMode = templateModeProvider.provideTemplateMode(resource); + assertThat(templateMode, is(TemplateMode.RAW)); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
