|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--freemarker.template.cache.LocalizedFileRetriever
Retrieves cacheable objects through the file system. This version performs locale-based searching for files: first it looks for the most-localized file, then works back to the default (base) filename. Filenames are of the format:
(filename)_(language)_(country).(file extension)
For instance, an HTML file encoded in Australian English would have the filename: helloworld_en_AU.html
Based on code donated to the FreeMarker project by Jonathan Revusky as part of the Niggle web application framework.
Field Summary | |
protected java.io.File |
directoryRoot
The root directory where the retriever will get files. |
protected java.lang.String |
encoding
The text encoding of the template files. |
protected java.lang.String |
filenameSuffix
The filename suffix required for a file to be retrieved. |
protected java.util.Locale |
locale
The localization. |
protected java.util.List |
localeExtensions
|
protected TemplateRegistry |
registry
The template registry to use to instantiate objects. |
Constructor Summary | |
LocalizedFileRetriever()
Creates new FileRetriever. |
|
LocalizedFileRetriever(java.io.File rootDir)
Creates a new FileRetriever, with a directory root. |
|
LocalizedFileRetriever(java.lang.String path)
Constructs a FileRetriever with a directory in which it will look for template files. |
Method Summary | |
boolean |
connectionOk()
Corresponds to checkCacheDir for file-system implementations. |
boolean |
exists(java.lang.String location)
Tests whether the object still exists in the template repository. |
static java.lang.String |
getCharset(java.util.Locale loc)
Gets the preferred charset for the given locale, or null if the locale is not recognized. |
java.lang.String |
getConnection()
Gets the connection for this retriever. |
java.lang.String |
getEncoding()
Returns the character encoding to be used when reading template files. |
protected java.lang.String |
getFilenameFromLocale(java.lang.String aFilename,
freemarker.template.cache.LocalizedFileRetriever.LocaleMap cLocale)
Given a base filename, and a LocaleMap entry, work out what the filename should be. |
java.lang.String |
getFilenameSuffix()
Returns the file suffix. |
java.util.Locale |
getLocale()
Retrieves the locale used when retrieving files. |
protected java.util.List |
getLocaleExtensions(java.util.Locale locale)
Creates a list of locales and associated filenames to use when searching for localized files. |
protected freemarker.template.cache.LocalizedFileRetriever.FileLocale |
getLocalizedFile(java.lang.String aFilename)
Given a base filename, get a localized version, if one is available. |
java.io.File |
getPath()
Returns the root directory for this retriever. |
java.util.List |
getPreloadData()
Returns a list of objects ( String s) to pre-load the cache with. |
protected java.lang.String |
getRootFile(java.lang.String aFilename)
Performs a reverse lookup of locale information: given a filename, determine whether a locale has been used, and if so, strips it back to the root filename. |
TemplateRegistry |
getTemplateRegistry()
Retrieves the current TemplateRegistry in use. |
protected boolean |
isSuffixValid(java.lang.String name)
Determine whether the filename ends with the appropriate filename suffix. |
long |
lastModified(java.lang.String location)
Determines when the object in the template repository was last modified. |
Cacheable |
loadData(java.lang.String location,
java.lang.String type)
Retrieves the appropriate data to be stored in the cache. |
protected java.io.File |
nameToFile(java.lang.String name)
Converts a cache element name to a File . |
protected void |
readDirectory(java.io.File dir,
java.lang.String relativeDirPath,
java.util.List visitedFiles)
Recursively updates the cache from the files in a (sub)directory and its subdirectories. |
void |
setConnection(java.lang.String path)
Sets the root directory for this retriever. |
void |
setEncoding(java.lang.String encoding)
Sets the character encoding to be used when reading template files. |
void |
setFilenameSuffix(java.lang.String filenameSuffix)
Sets the file suffix. |
void |
setLocale(java.util.Locale locale)
Sets the locale to use when retrieving files. |
void |
setPath(java.io.File dir)
Sets the root directory for this retriever. |
void |
setTemplateRegistry(TemplateRegistry cRegistry)
Sets a template registry implementation to use when creating new templates. |
java.lang.String |
toString()
Returns a string representation of the object. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected java.io.File directoryRoot
protected java.lang.String filenameSuffix
protected java.lang.String encoding
protected TemplateRegistry registry
protected java.util.Locale locale
protected java.util.List localeExtensions
Constructor Detail |
public LocalizedFileRetriever()
public LocalizedFileRetriever(java.lang.String path)
path
- the absolute path of the directory containing
templates for this retrieverpublic LocalizedFileRetriever(java.io.File rootDir)
rootDir
- the root directory for the file systemMethod Detail |
public boolean connectionOk() throws TemplateException
connectionOk
in interface CacheRetriever
TemplateException
- the directory no longer exists, or is not
a directorypublic void setConnection(java.lang.String path)
setConnection
in interface CacheRetriever
path
- the absolute path of the directory containing files
for this retriever.public java.lang.String getConnection()
getConnection
in interface CacheRetriever
freemarker.template.cache.CacheRetriever
public void setPath(java.io.File dir)
dir
- the root directory containing files for this retrieverpublic java.io.File getPath()
public void setFilenameSuffix(java.lang.String filenameSuffix)
filenameSuffix
- the optional filename suffix of
files to be read for this retriever.public java.lang.String getFilenameSuffix()
public boolean exists(java.lang.String location)
exists
in interface CacheRetriever
location
- the location of the object to be testedtrue
if the object still exists in the repository,
otherwise false
lastModified(java.lang.String)
public java.util.List getPreloadData() throws TemplateException
String
s) to pre-load the cache with.getPreloadData
in interface CacheRetriever
List
of String
s to preload the
cache withprotected void readDirectory(java.io.File dir, java.lang.String relativeDirPath, java.util.List visitedFiles) throws java.io.IOException
dir
- the directory to be read.relativeDirPath
- a string representing the directory's path
relative to the root cache directory.visitedFiles
- a List of files that have been visited so far.public long lastModified(java.lang.String location) throws TemplateException
Determines when the object in the template repository was last modified.
lastModified
in interface CacheRetriever
location
- the location of the object to be testedTemplateException
- is thrown whenever the item:
protected boolean isSuffixValid(java.lang.String name) throws TemplateException
name
- the filename to be checkedTemplateException
- the suffix is invalidprotected java.io.File nameToFile(java.lang.String name) throws TemplateException
File
.name
- the filename relative to the directory root of the retrieverpublic Cacheable loadData(java.lang.String location, java.lang.String type) throws TemplateException
loadData
in interface CacheRetriever
location
- the filename, relative to the root directory, of the
template data to loadtype
- the type of item to be loadedpublic void setEncoding(java.lang.String encoding)
setEncoding
in interface TextEncoding
encoding
- the name of the encoding to be used; this will be
passed to the constructor of InputStreamReader.public java.lang.String getEncoding()
getEncoding
in interface TextEncoding
InputStreamReader
.public void setTemplateRegistry(TemplateRegistry cRegistry)
setTemplateRegistry
in interface RegistryAccepter
cRegistry
- the registry to be used for creating new objectspublic TemplateRegistry getTemplateRegistry()
getTemplateRegistry
in interface RegistryAccepter
public void setLocale(java.util.Locale locale)
public java.util.Locale getLocale()
protected java.util.List getLocaleExtensions(java.util.Locale locale)
protected java.lang.String getRootFile(java.lang.String aFilename)
protected freemarker.template.cache.LocalizedFileRetriever.FileLocale getLocalizedFile(java.lang.String aFilename) throws TemplateException
protected java.lang.String getFilenameFromLocale(java.lang.String aFilename, freemarker.template.cache.LocalizedFileRetriever.LocaleMap cLocale)
public static java.lang.String getCharset(java.util.Locale loc)
loc
- the localepublic java.lang.String toString()
toString
in class java.lang.Object
String
representation of the object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |