@ -16,50 +16,46 @@
package org.springframework.boot.autoconfigure.mustache.servlet ;
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 ;
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.ViewResolver ;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver ;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver ;
import org.springframework.web.servlet.view.AbstractUrlBasedView ;
/ * *
/ * *
* Spring MVC { @link ViewResolver } for Mustache .
* Spring MVC { @link ViewResolver } for Mustache .
*
*
* @author Dave Syer
* @author Brian Clozel
* @author Andy Wilkinson
* @since 2.0 .0
* @author Phillip Webb
* @since 1.2 .2
* /
* /
public class MustacheViewResolver extends AbstractTemplateViewResolver {
public class MustacheViewResolver extends AbstractTemplateViewResolver {
private Compiler compiler = Mustache . compiler ( ) ;
private final Mustache . Compiler compiler ;
private String charset ;
private String charset ;
/ * *
* Create a { @code MustacheViewResolver } backed by a default
* instance of a { @link Mustache . Compiler } .
* /
public MustacheViewResolver ( ) {
public MustacheViewResolver ( ) {
this . compiler = Mustache . compiler ( ) ;
setViewClass ( requiredViewClass ( ) ) ;
setViewClass ( requiredViewClass ( ) ) ;
}
}
@Override
protected Class < ? > requiredViewClass ( ) {
return MustacheView . class ;
}
/ * *
/ * *
* Set the compiler .
* Create a { @code MustacheViewResolver } backed by a custom
* @param compiler the compiler
* 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 ;
this . compiler = compiler ;
setViewClass ( requiredViewClass ( ) ) ;
}
@Override
protected Class < ? > requiredViewClass ( ) {
return MustacheView . class ;
}
}
/ * *
/ * *
@ -71,57 +67,11 @@ public class MustacheViewResolver extends AbstractTemplateViewResolver {
}
}
@Override
@Override
protected View loadView ( String viewName , Locale locale ) throws Exception {
protected AbstractUrlBasedView buildView ( String viewName ) throws Exception {
Resource resource = resolveResource ( viewName , locale ) ;
MustacheView view = ( MustacheView ) super . buildView ( viewName ) ;
if ( resource = = null ) {
view . setCompiler ( this . compiler ) ;
return null ;
view . setCharset ( this . charset ) ;
}
return view ;
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 ( ) ) ;
}
}
}
}