org.codehaus.groovy.grails.web.pages
Class GroovyPagesTemplateEngine

java.lang.Object
  extended by groovy.text.TemplateEngine
      extended by org.codehaus.groovy.grails.support.ResourceAwareTemplateEngine
          extended by org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
All Implemented Interfaces:
ApplicationContextAware, ServletContextAware

public class GroovyPagesTemplateEngine
extends ResourceAwareTemplateEngine
implements ApplicationContextAware, ServletContextAware

A GroovyPagesTemplateEngine based on (but not extending) the existing TemplateEngine implementations within Groovy. It allows GSP pages to be re-used in different context using code like the below: Template t = new GroovyPagesTemplateEngine() .createTemplate(context,request,response); t.make() .writeTo(out);

Since:
0.1 Created: 12-Jan-2006
Author:
Graeme Rocher

Field Summary
static String BEAN_ID
           
static String RESOURCE_LOADER_BEAN_ID
           
 
Constructor Summary
GroovyPagesTemplateEngine()
           
GroovyPagesTemplateEngine(ServletContext servletContext)
           
 
Method Summary
protected  GroovyPageMetaInfo buildPageMetaInfo(InputStream inputStream, Resource res, String pageName)
          Constructs a GroovyPageMetaInfo instance which holds the script class, modified date and so on
 int[] calculateLineNumbersForPage(ServletContext context, String url)
          Retrieves a line number matrix for the specified page that can be used to retrieve the actual line number within the GSP page if the line number within the compiled GSP is known
 void clearPageCache()
          Clears the page cache.
 groovy.text.Template createTemplate()
          Creates a Template for the currently executing Request
 groovy.text.Template createTemplate(File file)
          Creates a Template for the given file
 groovy.text.Template createTemplate(InputStream inputStream)
          Create a Template for the given InputStream
protected  groovy.text.Template createTemplate(InputStream inputStream, Resource resource, String pageName)
          Constructs a Groovy Template from the given InputStream and Spring Resource object
 groovy.text.Template createTemplate(Resource resource)
          Creates a Template for the given Spring Resource instance
 groovy.text.Template createTemplate(String uri)
          Creates a Template using the given URI.
 groovy.text.Template createTemplate(String txt, String pageName)
          Creates a Template using the given text for the Template and the given name.
 groovy.text.Template createTemplate(URL url)
          Creates a Template for the given URL
protected  String establishPageName(Resource res, String pageName)
          Establishes the name to use for the given resource
protected  String getCurrentRequestUri(HttpServletRequest request)
          Return the page identifier.
 Resource getResourceForUri(String uri)
          Attempts to retrieve a reference to a GSP as a Spring Resource instance for the given URI.
protected  String getUriWithinGrailsViews(String relativeUri)
          Returns the path to the view of the relative URI within the Grails views directory
 boolean isReloadEnabled()
          Return whether reload is enabled for the GroovyPagesTemplateEngine
 void setApplicationContext(ApplicationContext applicationContext)
          Sets the ResourceLoader from the ApplicationContext
 void setClassLoader(groovy.lang.GroovyClassLoader classLoader)
          Sets the ClassLoader that the TemplateEngine should use to
 void setJspTagLibraryResolver(TagLibraryResolver jspTagLibraryResolver)
           
 void setReloadEnabled(boolean b)
          Sets whether reloading is enabled
 void setResourceLoader(ResourceLoader resourceLoader)
          Sets a custom ResourceLoader that will be used to load GSPs for URIs
 void setServletContext(ServletContext servletContext)
           
 void setTagLibraryLookup(TagLibraryLookup tagLibraryLookup)
           
 
Methods inherited from class org.codehaus.groovy.grails.support.ResourceAwareTemplateEngine
createTemplate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BEAN_ID

public static final String BEAN_ID
See Also:
Constant Field Values

RESOURCE_LOADER_BEAN_ID

public static final String RESOURCE_LOADER_BEAN_ID
See Also:
Constant Field Values
Constructor Detail

GroovyPagesTemplateEngine

public GroovyPagesTemplateEngine()

GroovyPagesTemplateEngine

public GroovyPagesTemplateEngine(ServletContext servletContext)
Method Detail

setTagLibraryLookup

public void setTagLibraryLookup(TagLibraryLookup tagLibraryLookup)

setJspTagLibraryResolver

public void setJspTagLibraryResolver(TagLibraryResolver jspTagLibraryResolver)

setClassLoader

public void setClassLoader(groovy.lang.GroovyClassLoader classLoader)
Sets the ClassLoader that the TemplateEngine should use to

Parameters:
classLoader - The ClassLoader to use when compilation of Groovy Pages occurs

setResourceLoader

public void setResourceLoader(ResourceLoader resourceLoader)
Sets a custom ResourceLoader that will be used to load GSPs for URIs

Parameters:
resourceLoader - The ResourceLoader instance

calculateLineNumbersForPage

public int[] calculateLineNumbersForPage(ServletContext context,
                                         String url)
Retrieves a line number matrix for the specified page that can be used to retrieve the actual line number within the GSP page if the line number within the compiled GSP is known

Parameters:
context - The ServletContext instance
url - The URL of the page
Returns:
An array where the index is the line number witin the compiled GSP and the value is the line number within the source

createTemplate

public groovy.text.Template createTemplate(Resource resource)
Creates a Template for the given Spring Resource instance

Overrides:
createTemplate in class ResourceAwareTemplateEngine
Parameters:
resource - The Resource to create the Template for
Returns:
The Template instance

createTemplate

public groovy.text.Template createTemplate(String uri)
Creates a Template using the given URI.

Overrides:
createTemplate in class groovy.text.TemplateEngine
Parameters:
uri - The URI of the page to create the template for
Returns:
The Template instance
Throws:
org.codehaus.groovy.control.CompilationFailedException

createTemplate

public groovy.text.Template createTemplate(String txt,
                                           String pageName)
                                    throws IOException
Creates a Template using the given text for the Template and the given name. The name of the template is required

Parameters:
txt - The URI of the page to create the template for
pageName - The name of the page being parsed
Returns:
The Template instance
Throws:
org.codehaus.groovy.control.CompilationFailedException
IOException - Thrown if an IO exception occurs creating the Template

createTemplate

public groovy.text.Template createTemplate()
                                    throws IOException,
                                           ClassNotFoundException
Creates a Template for the currently executing Request

Returns:
The Template for the currently executing request
Throws:
IOException - Thrown when an exception occurs Reading the Template
ClassNotFoundException - Thrown when the class of the template was not found

createTemplate

public groovy.text.Template createTemplate(File file)
                                    throws org.codehaus.groovy.control.CompilationFailedException,
                                           ClassNotFoundException,
                                           IOException
Creates a Template for the given file

Overrides:
createTemplate in class groovy.text.TemplateEngine
Parameters:
file - The File to use to construct the template with
Returns:
A Groovy Template instance
Throws:
org.codehaus.groovy.control.CompilationFailedException - When an error occured compiling the Template
ClassNotFoundException - When a Class cannot be found within the given Template
IOException - When a I/O Exception occurs reading the Template

createTemplate

public groovy.text.Template createTemplate(URL url)
                                    throws org.codehaus.groovy.control.CompilationFailedException,
                                           ClassNotFoundException,
                                           IOException
Creates a Template for the given URL

Overrides:
createTemplate in class groovy.text.TemplateEngine
Parameters:
url - The URL to use to construct the template with
Returns:
A Groovy Template instance
Throws:
org.codehaus.groovy.control.CompilationFailedException - When an error occured compiling the Template
ClassNotFoundException - When a Class cannot be found within the given Template
IOException - When a I/O Exception occurs reading the Template

createTemplate

public groovy.text.Template createTemplate(InputStream inputStream)
Create a Template for the given InputStream

Specified by:
createTemplate in class ResourceAwareTemplateEngine
Parameters:
inputStream - The InputStream to create the Template for
Returns:
The Template instance

createTemplate

protected groovy.text.Template createTemplate(InputStream inputStream,
                                              Resource resource,
                                              String pageName)
Constructs a Groovy Template from the given InputStream and Spring Resource object

Parameters:
inputStream - The InputStream to use
resource - The Resource to use
pageName - The name of the page
Returns:
The Groovy Template

isReloadEnabled

public boolean isReloadEnabled()
Return whether reload is enabled for the GroovyPagesTemplateEngine

Returns:
True if it is

setReloadEnabled

public void setReloadEnabled(boolean b)
Sets whether reloading is enabled

Parameters:
b - True if it is enabled

getResourceForUri

public Resource getResourceForUri(String uri)
Attempts to retrieve a reference to a GSP as a Spring Resource instance for the given URI.

Parameters:
uri - The URI to check
Returns:
A Resource instance

buildPageMetaInfo

protected GroovyPageMetaInfo buildPageMetaInfo(InputStream inputStream,
                                               Resource res,
                                               String pageName)
Constructs a GroovyPageMetaInfo instance which holds the script class, modified date and so on

Parameters:
inputStream - The InputStream to construct the GroovyPageMetaInfo instance from
res - The Spring Resource to construct the MetaInfo from
pageName - The name of the page (can be null, in which case method responsible for calculating appropriate alternative)
Returns:
The GroovyPageMetaInfo instance

establishPageName

protected String establishPageName(Resource res,
                                   String pageName)
Establishes the name to use for the given resource

Parameters:
res - The Resource to calculate the name for
pageName - The name of the page, can be null, in which case method responsible for calculation
Returns:
The name as a String

setApplicationContext

public void setApplicationContext(ApplicationContext applicationContext)
                           throws BeansException
Sets the ResourceLoader from the ApplicationContext

Specified by:
setApplicationContext in interface ApplicationContextAware
Parameters:
applicationContext - The ApplicationContext
Throws:
BeansException - Thrown when an error occurs with the ApplicationContext

getCurrentRequestUri

protected String getCurrentRequestUri(HttpServletRequest request)
Return the page identifier.

Parameters:
request - The HttpServletRequest instance
Returns:
The page id

getUriWithinGrailsViews

protected String getUriWithinGrailsViews(String relativeUri)
Returns the path to the view of the relative URI within the Grails views directory

Parameters:
relativeUri - The relative URI
Returns:
The path of the URI within the Grails view directory

setServletContext

public void setServletContext(ServletContext servletContext)
Specified by:
setServletContext in interface ServletContextAware

clearPageCache

public void clearPageCache()
Clears the page cache. Views will be re-compiled.



Copyright (c) 2005-2009 The Grails project