|
|
|
@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.mustache.web; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
|
|
|
|
import java.io.Reader; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Locale; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.propertyeditors.LocaleEditor; |
|
|
|
import org.springframework.beans.propertyeditors.LocaleEditor; |
|
|
|
@ -35,6 +36,7 @@ import com.samskivert.mustache.Template; |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Dave Syer |
|
|
|
* @author Dave Syer |
|
|
|
* @author Andy Wilkinson |
|
|
|
* @author Andy Wilkinson |
|
|
|
|
|
|
|
* @author Phillip Webb |
|
|
|
* @since 1.2.2 |
|
|
|
* @since 1.2.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MustacheViewResolver extends UrlBasedViewResolver { |
|
|
|
public class MustacheViewResolver extends UrlBasedViewResolver { |
|
|
|
@ -44,7 +46,12 @@ public class MustacheViewResolver extends UrlBasedViewResolver { |
|
|
|
private String charset; |
|
|
|
private String charset; |
|
|
|
|
|
|
|
|
|
|
|
public MustacheViewResolver() { |
|
|
|
public MustacheViewResolver() { |
|
|
|
setViewClass(MustacheView.class); |
|
|
|
setViewClass(requiredViewClass()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected Class<?> requiredViewClass() { |
|
|
|
|
|
|
|
return MustacheView.class; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -67,31 +74,15 @@ public class MustacheViewResolver extends UrlBasedViewResolver { |
|
|
|
if (resource == null) { |
|
|
|
if (resource == null) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
MustacheView view = new MustacheView(createTemplate(resource)); |
|
|
|
MustacheView mustacheView = (MustacheView) super.loadView(viewName, locale); |
|
|
|
view.setApplicationContext(getApplicationContext()); |
|
|
|
mustacheView.setTemplate(createTemplate(resource)); |
|
|
|
view.setServletContext(getServletContext()); |
|
|
|
return mustacheView; |
|
|
|
return view; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Template createTemplate(Resource resource) throws IOException { |
|
|
|
|
|
|
|
return this.charset == null ? this.compiler.compile(new InputStreamReader( |
|
|
|
|
|
|
|
resource.getInputStream())) : this.compiler |
|
|
|
|
|
|
|
.compile(new InputStreamReader(resource.getInputStream(), this.charset)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Resource resolveResource(String viewName, Locale locale) { |
|
|
|
private Resource resolveResource(String viewName, Locale locale) { |
|
|
|
return resolveFromLocale(viewName, getLocale(locale)); |
|
|
|
return resolveFromLocale(viewName, getLocale(locale)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String getLocale(Locale locale) { |
|
|
|
|
|
|
|
if (locale == null) { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LocaleEditor localeEditor = new LocaleEditor(); |
|
|
|
|
|
|
|
localeEditor.setValue(locale); |
|
|
|
|
|
|
|
return "_" + localeEditor.getAsText(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Resource resolveFromLocale(String viewName, String locale) { |
|
|
|
private Resource resolveFromLocale(String viewName, String locale) { |
|
|
|
Resource resource = getApplicationContext().getResource( |
|
|
|
Resource resource = getApplicationContext().getResource( |
|
|
|
getPrefix() + viewName + locale + getSuffix()); |
|
|
|
getPrefix() + viewName + locale + getSuffix()); |
|
|
|
@ -105,4 +96,24 @@ public class MustacheViewResolver extends UrlBasedViewResolver { |
|
|
|
return resource; |
|
|
|
return resource; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String getLocale(Locale locale) { |
|
|
|
|
|
|
|
if (locale == null) { |
|
|
|
|
|
|
|
return ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
LocaleEditor localeEditor = new LocaleEditor(); |
|
|
|
|
|
|
|
localeEditor.setValue(locale); |
|
|
|
|
|
|
|
return "_" + localeEditor.getAsText(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Template createTemplate(Resource resource) throws IOException { |
|
|
|
|
|
|
|
return this.compiler.compile(getReader(resource)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Reader getReader(Resource resource) throws IOException { |
|
|
|
|
|
|
|
if (this.charset != null) { |
|
|
|
|
|
|
|
return new InputStreamReader(resource.getInputStream(), this.charset); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return new InputStreamReader(resource.getInputStream()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|