Polish Mustache code

This commit is contained in:
Phillip Webb 2015-01-12 13:16:07 -08:00
parent bec5e96b94
commit 3fc1e44302
13 changed files with 148 additions and 48 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.mustache;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
@ -38,9 +39,10 @@ import com.samskivert.mustache.Mustache.Compiler;
import com.samskivert.mustache.Mustache.TemplateLoader;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Mustache.
*
* @author Dave Syer
* @since 1.2.2
*
*/
@Configuration
@ConditionalOnClass(Mustache.class)

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,23 +26,26 @@ import com.samskivert.mustache.Mustache.Formatter;
import com.samskivert.mustache.Mustache.TemplateLoader;
/**
* Factory for a Mustache compiler with custom strategies. For building a
* <code>@Bean</code> definition in Java it probably doesn't help to use this factory
* since the underlying fluent API is actually richer.
*
* @see MustacheResourceTemplateLoader
* Factory for a Mustache compiler with custom strategies. For building a {@code @Bean}
* definition in Java it probably doesn't help to use this factory since the underlying
* fluent API is actually richer.
*
* @author Dave Syer
* @since 1.2.2
*
* @see MustacheResourceTemplateLoader
*/
public class MustacheCompilerFactoryBean implements FactoryBean<Mustache.Compiler> {
private String delims;
private TemplateLoader templateLoader;
private Formatter formatter;
private Escaper escaper;
private Collector collector;
private Compiler compiler;
public void setDelims(String delims) {

View File

@ -1,3 +1,19 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed 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.springframework.boot.autoconfigure.mustache;
import java.util.HashMap;
@ -11,9 +27,12 @@ import org.springframework.core.env.Environment;
import com.samskivert.mustache.DefaultCollector;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Mustache.Collector;
import com.samskivert.mustache.Mustache.VariableFetcher;
/**
* Mustache {@link Collector} to expose properties from the Spring {@link Environment}.
*
* @author Dave Syer
* @since 1.2.2
*/
@ -21,6 +40,7 @@ public class MustacheEnvironmentCollector extends DefaultCollector implements
EnvironmentAware {
private ConfigurableEnvironment environment;
private Map<String, Object> target;
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,9 +20,10 @@ import org.springframework.boot.autoconfigure.template.AbstractViewResolverPrope
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* {@link ConfigurationProperties} for Mustache.
*
* @author Dave Syer
* @since 1.2.2
*
*/
@ConfigurationProperties(prefix = "spring.mustache")
public class MustacheProperties extends AbstractViewResolverProperties {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,24 +32,23 @@ import com.samskivert.mustache.Mustache.TemplateLoader;
* Resource abstraction to load a template from a file, classpath, URL etc. A
* TemplateLoader is needed in the Compiler when you want to render partials (i.e.
* tiles-like fetaures).
*
* @see Mustache
* @see Resource
*
*
* @author Dave Syer
* @since 1.2.2
*
* @see Mustache
* @see Resource
*/
public class MustacheResourceTemplateLoader implements TemplateLoader, ResourceLoaderAware {
public class MustacheResourceTemplateLoader implements TemplateLoader,
ResourceLoaderAware {
private String prefix = "";
private String suffix = "";
private String charSet = "UTF-8";
private ResourceLoader resourceLoader = new DefaultResourceLoader();
public MustacheResourceTemplateLoader() {
}
@ -58,7 +57,7 @@ public class MustacheResourceTemplateLoader implements TemplateLoader, ResourceL
this.prefix = prefix;
this.suffix = suffix;
}
/**
* @param charSet the charSet to set
*/
@ -76,8 +75,8 @@ public class MustacheResourceTemplateLoader implements TemplateLoader, ResourceL
@Override
public Reader getTemplate(String name) throws Exception {
return new InputStreamReader(resourceLoader.getResource(prefix + name + suffix)
.getInputStream(), charSet);
return new InputStreamReader(this.resourceLoader.getResource(
this.prefix + name + this.suffix).getInputStream(), this.charSet);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,18 +21,21 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractTemplateView;
import com.samskivert.mustache.Template;
/**
* @author Dave Syer
* Spring MVC {@link View} using the Mustache template engine.
*
* @author Dave Syer
* @since 1.2.2
*/
public class MustacheView extends AbstractTemplateView {
private final Template template;
public MustacheView(Template template) {
this.template = template;
}
@ -40,7 +43,7 @@ public class MustacheView extends AbstractTemplateView {
@Override
protected void renderMergedTemplateModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response) throws Exception {
template.execute(model, response.getWriter());
this.template.execute(model, response.getWriter());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.Locale;
import org.springframework.beans.propertyeditors.LocaleEditor;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import com.samskivert.mustache.Mustache;
@ -30,8 +31,10 @@ import com.samskivert.mustache.Mustache.Compiler;
import com.samskivert.mustache.Template;
/**
* @author Dave Syer
* Spring MVC {@link ViewResolver} for Mustache.
*
* @author Dave Syer
* @since 1.2.2
*/
public class MustacheViewResolver extends UrlBasedViewResolver {
@ -61,17 +64,20 @@ public class MustacheViewResolver extends UrlBasedViewResolver {
}
private Template createTemplate(Resource resource) throws IOException {
return compiler.compile(new InputStreamReader(resource.getInputStream()));
return this.compiler.compile(new InputStreamReader(resource.getInputStream()));
}
private Resource resolveResource(String viewName, Locale locale) {
String l10n = "";
if (locale != null) {
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setValue(locale);
l10n = "_" + localeEditor.getAsText();
return resolveFromLocale(viewName, getLocale(locale));
}
private String getLocale(Locale locale) {
if (locale == null) {
return "";
}
return resolveFromLocale(viewName, l10n);
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setValue(locale);
return "_" + localeEditor.getAsText();
}
private Resource resolveFromLocale(String viewName, String locale) {

View File

@ -1,3 +1,19 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed 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.springframework.boot.autoconfigure.mustache;
import java.lang.annotation.Documented;
@ -14,7 +30,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.mustache.AutoApplicationTests.Application;
import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfigurationIntegrationTests.Application;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
@ -31,12 +47,17 @@ import org.springframework.web.bind.annotation.RequestMapping;
import static org.junit.Assert.assertTrue;
/**
* Integration tests for {@link MustacheAutoConfiguration}.
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest({ "server.port:0",
"spring.mustache.prefix:classpath:/mustache-templates/" })
@WebAppConfiguration
public class AutoApplicationTests {
public class MustacheAutoConfigurationIntegrationTests {
@Autowired
private EmbeddedWebApplicationContext context;

View File

@ -1,3 +1,19 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed 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.springframework.boot.autoconfigure.mustache;
import java.util.Collections;
@ -6,7 +22,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.mustache.MustacheTemplateStandaloneTests.Application;
import org.springframework.boot.autoconfigure.mustache.MustacheStandaloneIntegrationTests.Application;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Configuration;
@ -17,10 +33,15 @@ import com.samskivert.mustache.Mustache;
import static org.junit.Assert.assertEquals;
/**
* Integration Tests for {@link MustacheAutoConfiguration} outside of a web application.
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest({ "spring.main.web_environment=false", "env.foo=Heaven", "foo=World" })
public class MustacheTemplateStandaloneTests {
public class MustacheStandaloneIntegrationTests {
@Autowired
private Mustache.Compiler compiler;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,8 +28,9 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Dave Syer
* Tests for {@link MustacheViewResolver}.
*
* @author Dave Syer
*/
public class MustacheViewResolverTests {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,8 +32,9 @@ import com.samskivert.mustache.Mustache;
import static org.junit.Assert.assertEquals;
/**
* @author Dave Syer
* Tests for {@link MustacheView}.
*
* @author Dave Syer
*/
public class MustacheViewTests {

View File

@ -1,3 +1,19 @@
/*
* Copyright 2012-2015 the original author or authors.
*
* Licensed 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.springframework.boot.autoconfigure.mustache;
import java.lang.annotation.Documented;
@ -15,8 +31,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.mustache.MustacheResourceTemplateLoader;
import org.springframework.boot.autoconfigure.mustache.ApplicationTests.Application;
import org.springframework.boot.autoconfigure.mustache.MustacheWebIntegrationTests.Application;
import org.springframework.boot.autoconfigure.mustache.web.MustacheView;
import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
@ -39,11 +55,17 @@ import com.samskivert.mustache.Template;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Integration Tests for {@link MustacheAutoConfiguration}, {@link MustacheViewResolver}
* and {@link MustacheView}.
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@IntegrationTest("server.port:0")
@WebAppConfiguration
public class ApplicationTests {
public class MustacheWebIntegrationTests {
@Autowired
private EmbeddedWebApplicationContext context;