EchoPoint
1.0

echopoint.template
Class SwitchedDataSource

java.lang.Object
  extended byechopoint.template.SwitchedDataSource
All Implemented Interfaces:
DataSource

public class SwitchedDataSource
extends java.lang.Object
implements DataSource

SwitchedDataSource allows you to use a 3 step strategy for finding HTML template data.

Step 1:

This class will first look for the existence of a file and if it exists then it will use it as the template data source. The file name is defined as fileDirectoryPrefix + fileOrResourceName

Step 2:

Faling that, if you provide an EchoInstance, it will retrieve the javax.servlet.ServletContext from the instance and attempt to load it using javax.servlet.ServletContext.getResource(java.lang.String) and the fileOrResourceName. This step allows you to access resources in your web application that are not under a class path directory.

Step 3:

If ServletContext.getResource() fails or the EchoIntance is null, it will then attempt to load the template data as a class resource via Class.getResource(java.lang.String).

This class is useful when moving between development and production. For example, in development your templates may reside in your web project source directory (probably under source control).

Then when you move to production, they will reside as class resources under the web application servlet context root directory or WEB-INF/classes directory of your web application.

This class will allow for easier template changes during development and testing and then use class loading in production.

You would use this class something like this :

 ds = new SwitchedDataSource(
 			"/java/myproject/finance/src",
 			"/com/bankwest/templates/login.html",
 			getEchoInstance());
 
 template.setTemplate(ds);
 
In the above example the SwitchedDataSource will first look for the template data as a File in :
 "/java/myproject/finance/src/com/bankwest/templates/login.html"
 
Then if this file cant be found it will load the template data as a ServletContext resource using the name :
 "/com/bankwest/templates/login.html"
 
Finally if this files it will load the template data as a Class resource using the name :
 "/com/bankwest/templates/login.html"
 

See Also:
URLKit.locate(String, String, EchoInstance, Class)

Constructor Summary
SwitchedDataSource(java.lang.String fileDirectoryPrefix, java.lang.String fileOrResourceName)
          Constructs a SwitchedDataSource that locates the template data using the following strategy : Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName' Step 2 : failing that, it will a Class.getResource(fileOrResourceName)
SwitchedDataSource(java.lang.String fileDirectoryPrefix, java.lang.String fileOrResourceName, nextapp.echo.EchoInstance echoInstance)
          Constructs a SwitchedDataSource that locates the template data using the following strategy : Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName' Step 2 : failing that, it uses the EchoInstance to locate the URL via ServletContext.getResource(fileOrResourceName), if the EchoInstance is not null.
SwitchedDataSource(java.lang.String fileDirectoryPrefix, java.lang.String fileOrResourceName, nextapp.echo.EchoInstance echoInstance, java.lang.String encoding, java.lang.Class resourceClass)
          Constructs a SwitchedDataSource that locates the template data using the following strategy : Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName' Step 2 : failing that, it uses the EchoInstance to locate the URL via ServletContext.getResource(fileOrResourceName), if the EchoInstance is not null.
SwitchedDataSource(java.lang.String fileDirectoryPrefix, java.lang.String fileOrResourceName, java.lang.String encoding)
          Constructs a SwitchedDataSource that locates the template data using the following strategy : Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName' Step 2 : failing that, it uses the EchoInstance to locate the URL via ServletContext.getResource(fileOrResourceName), if the EchoInstance is not null.
 
Method Summary
 java.lang.String getCanonicalName()
          Returns a canonical name of this DataSource.
 java.lang.String getCharacterEncoding()
          This returns the encoding of the DataSource.
 java.io.Reader getInputReader()
          Gets an Reader of this DataSource.
 long lastModified()
          Returns the time the content of this DataSource was last modified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SwitchedDataSource

public SwitchedDataSource(java.lang.String fileDirectoryPrefix,
                          java.lang.String fileOrResourceName)
                   throws java.io.IOException
Constructs a SwitchedDataSource that locates the template data using the following strategy :

Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName'

Step 2 : failing that, it will a Class.getResource(fileOrResourceName)

Parameters:
fileDirectoryPrefix - - the directory prefix to use when looking initially for the File. May be null.
fileOrResourceName - - the file or class resource name to use.
Throws:
java.io.IOException
java.io.UnsupportedEncodingException
See Also:
Class.getResource(java.lang.String), URLKit.locate(String, String, EchoInstance, Class)

SwitchedDataSource

public SwitchedDataSource(java.lang.String fileDirectoryPrefix,
                          java.lang.String fileOrResourceName,
                          nextapp.echo.EchoInstance echoInstance)
                   throws java.io.IOException
Constructs a SwitchedDataSource that locates the template data using the following strategy :

Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName'

Step 2 : failing that, it uses the EchoInstance to locate the URL via ServletContext.getResource(fileOrResourceName), if the EchoInstance is not null.

Step 3 : failing that, it will a Class.getResource(fileOrResourceName)

Parameters:
fileDirectoryPrefix - - the directory prefix to use when looking initially for the File. May be null.
fileOrResourceName - - the file or class resource name to use.
echoInstance - - the EchoInstance to retreive the ServletContext from. This can be null, in which case ServletContext resource loading will not be used.
Throws:
java.io.IOException
java.io.UnsupportedEncodingException
See Also:
Class.getResource(java.lang.String), URLKit.locate(String, String, EchoInstance, Class)

SwitchedDataSource

public SwitchedDataSource(java.lang.String fileDirectoryPrefix,
                          java.lang.String fileOrResourceName,
                          java.lang.String encoding)
                   throws java.io.IOException,
                          java.io.UnsupportedEncodingException
Constructs a SwitchedDataSource that locates the template data using the following strategy :

Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName'

Step 2 : failing that, it uses the EchoInstance to locate the URL via ServletContext.getResource(fileOrResourceName), if the EchoInstance is not null.

Step 3 : failing that, it will a Class.getResource(fileOrResourceName)

Parameters:
fileDirectoryPrefix - - the directory prefix to use when looking initially for the File. May be null.
fileOrResourceName - - the file or class resource name to use.
encoding - - the character encoding to use when reading the data
Throws:
java.io.IOException
java.io.UnsupportedEncodingException
See Also:
Class.getResource(java.lang.String), URLKit.locate(String, String, EchoInstance, Class)

SwitchedDataSource

public SwitchedDataSource(java.lang.String fileDirectoryPrefix,
                          java.lang.String fileOrResourceName,
                          nextapp.echo.EchoInstance echoInstance,
                          java.lang.String encoding,
                          java.lang.Class resourceClass)
                   throws java.io.IOException,
                          java.io.UnsupportedEncodingException
Constructs a SwitchedDataSource that locates the template data using the following strategy :

Step 1 : Looks for a file called 'fileDirectoryPrefix + fileOrResourceName'

Step 2 : failing that, it uses the EchoInstance to locate the URL via ServletContext.getResource(fileOrResourceName), if the EchoInstance is not null.

Step 3 : failing that, it will a Class.getResource(fileOrResourceName)

Parameters:
fileDirectoryPrefix - - the directory prefix to use when looking initially for the File. May be null.
fileOrResourceName - - the file or class resource name to use.
echoInstance - - the EchoInstance to retreive the ServletContext from. This can be null, in which case ServletContext resource loading will not be used.
encoding - - the character encoding to use when reading the data
resourceClass - - the class to use when loading the resource data via Class.getResource(java.lang.String)
Throws:
java.io.IOException
java.io.UnsupportedEncodingException
See Also:
Class.getResource(java.lang.String), URLKit.locate(String, String, EchoInstance, Class)
Method Detail

getCanonicalName

public java.lang.String getCanonicalName()
Description copied from interface: DataSource
Returns a canonical name of this DataSource.

The name returned here is used to look up the parsing result of the internal caching, so it should differ for all different Sources :-) May return null if this Source is supposed to be parsed each time. The canonical name would be something like a filename or an URL.

Specified by:
getCanonicalName in interface DataSource
See Also:
DataSource.getCanonicalName()

getCharacterEncoding

public java.lang.String getCharacterEncoding()
Description copied from interface: DataSource
This returns the encoding of the DataSource. This should be used by the DataSource when returning a localised Reader within getInputReader().

Specified by:
getCharacterEncoding in interface DataSource
See Also:
DataSource.getCharacterEncoding()

getInputReader

public java.io.Reader getInputReader()
                              throws java.io.IOException
Description copied from interface: DataSource
Gets an Reader of this DataSource.

Note that this method may be called twice if the page has to be parsed first. So you probably have to implement a buffer if your underlying source is transient ..

Specified by:
getInputReader in interface DataSource
Throws:
java.io.IOException
See Also:
DataSource.getInputReader()

lastModified

public long lastModified()
Description copied from interface: DataSource
Returns the time the content of this DataSource was last modified.

The return value is used to decide whether to reparse a Source or not. Reparsing is done if the value returned here differs from the value returned at the last processing time. This may not return a 'real' time, it needs just to be comparable to itself; so some sort of version counter would be perfect as well.

Specified by:
lastModified in interface DataSource
Returns:
long a modification time
See Also:
DataSource.lastModified()

EchoPoint
1.0