@ -16,50 +16,46 @@
package org.springframework.boot.autoconfigure.mustache.servlet ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.io.Reader ;
import java.util.Locale ;
import com.samskivert.mustache.Mustache ;
import com.samskivert.mustache.Mustache.Compiler ;
import com.samskivert.mustache.Template ;
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.AbstractTemplateViewResolver ;
import org.springframework.web.servlet.view.AbstractUrlBasedView ;
/ * *
* Spring MVC { @link ViewResolver } for Mustache .
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Phillip Webb
* @since 1.2 .2
* @author Brian Clozel
* @since 2.0 .0
* /
public class MustacheViewResolver extends AbstractTemplateViewResolver {
private Compiler compiler = Mustache . compiler ( ) ;
private final Mustache . Compiler compiler ;
private String charset ;
/ * *
* Create a { @code MustacheViewResolver } backed by a default
* instance of a { @link Mustache . Compiler } .
* /
public MustacheViewResolver ( ) {
this . compiler = Mustache . compiler ( ) ;
setViewClass ( requiredViewClass ( ) ) ;
}
@Override
protected Class < ? > requiredViewClass ( ) {
return MustacheView . class ;
}
/ * *
* Set the compiler .
* @param compiler the compiler
* Create a { @code MustacheViewResolver } backed by a custom
* instance of a { @link Mustache . Compiler } .
* @param compiler the Mustache compiler used to compile templates
* /
public void setCompiler ( Compiler compiler ) {
public MustacheViewResolver ( Mustache . Compiler compiler ) {
this . compiler = compiler ;
setViewClass ( requiredViewClass ( ) ) ;
}
@Override
protected Class < ? > requiredViewClass ( ) {
return MustacheView . class ;
}
/ * *
@ -71,57 +67,11 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver {
}
@Override
protected View loadView ( String viewName , Locale locale ) throws Exception {
Resource resource = resolveResource ( viewName , locale ) ;
if ( resource = = null ) {
return null ;
}
MustacheView mustacheView = ( MustacheView ) super . loadView ( viewName , locale ) ;
mustacheView . setTemplate ( createTemplate ( resource ) ) ;
return mustacheView ;
}
private Resource resolveResource ( String viewName , Locale locale ) {
return resolveFromLocale ( viewName , getLocale ( locale ) ) ;
}
private Resource resolveFromLocale ( String viewName , String locale ) {
Resource resource = getApplicationContext ( )
. getResource ( getPrefix ( ) + viewName + locale + getSuffix ( ) ) ;
if ( resource = = null | | ! resource . exists ( ) ) {
if ( locale . isEmpty ( ) ) {
return null ;
}
int index = locale . lastIndexOf ( "_" ) ;
return resolveFromLocale ( viewName , locale . substring ( 0 , index ) ) ;
}
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 {
Reader reader = getReader ( resource ) ;
try {
return this . compiler . compile ( reader ) ;
}
finally {
reader . close ( ) ;
}
}
private Reader getReader ( Resource resource ) throws IOException {
if ( this . charset ! = null ) {
return new InputStreamReader ( resource . getInputStream ( ) , this . charset ) ;
}
return new InputStreamReader ( resource . getInputStream ( ) ) ;
protected AbstractUrlBasedView buildView ( String viewName ) throws Exception {
MustacheView view = ( MustacheView ) super . buildView ( viewName ) ;
view . setCompiler ( this . compiler ) ;
view . setCharset ( this . charset ) ;
return view ;
}
}