Package org.apache.velocity.tools.view
Class UiDependencyTool
- java.lang.Object
-
- org.apache.velocity.tools.view.UiDependencyTool
-
@DefaultKey("depends") @ValidScope("request") public class UiDependencyTool extends Object
NOTE: This tool is considered "beta" quality due to lack of public testing and is not automatically provided via the default tools.xml file. Tool to make it easier to manage usage of client-side dependencies. This is essentially a simple dependency system for javascript and css. This could be cleaned up to use fewer maps, use more classes, and cache formatted values, but this is good enough for now. To use it, create a ui.xml file at the root of the classpath. Follow the example below. By default, it prepends the request context path and then "css/" to every stylesheet file and the request context path and "js/" to every javascript file path. You can alter those defaults by changing the type definition. In the example below, the file path for the style type is changed to "/styles/", leaving out the {context}. This is safe in request scope, but the group info (from ui.xml) should only be read once. It is not re-parsed on every request.Example of use:
Template --- <html> <head> $depends.on('profile').print(' ') </head> ... Output ------ <html> <head> <style rel="stylesheet" type="text/css" href="css/globals.css"/> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/profile.js"></script> </head> ... Example tools.xml: <tools> <toolbox scope="request"> <tool class="org.apache.velocity.tools.view.beta.UiDependencyTool"/> </toolbox> </tools> Example ui.xml: <ui> <type name="style"><![CDATA[<link rel="stylesheet" type="text/css" href="/styles/{file}">]]></type> <group name="globals"> <file type="style">css/globals.css<file/> </group> <group name="jquery"> <file type="script">js/jquery.js<file/> </group> <group name="profile"> <needs>globals</needs> <needs>jquery</needs> <file type="script">js/profile.js<file/> </group> </ui>- Version:
- $Revision: 16660 $
- Author:
- Nathan Bubna
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classUiDependencyTool.FileRuleNOTE: This class may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.protected static classUiDependencyTool.GroupNOTE: This class may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.protected static classUiDependencyTool.GroupRuleNOTE: This class may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.protected static classUiDependencyTool.NeedsRuleNOTE: This class may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.protected static classUiDependencyTool.TypeRuleNOTE: This class may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_SOURCE_FILEstatic StringGROUPS_KEY_SPACEstatic StringSOURCE_FILE_KEYstatic StringTYPES_KEY_SPACE
-
Constructor Summary
Constructors Constructor Description UiDependencyTool()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddDependencies(Map<String,List<String>> fbt)Adds the specified files organized by type to this instance's current dependencies.protected voidaddFile(String type, String file)Adds a file to this instance's dependencies under the specified type.voidconfigure(Map params)UiDependencyToolcontext(String path)Sets a custom {context} variable for the formats to use.protected org.apache.commons.digester.DigestercreateDigester()Creates theDigesterused byread(java.lang.String, boolean)to create the group info for this instance out of the specified XML file.protected Stringformat(String format, String value)Applies the format string to the given value.Map<String,List<String>>getDependencies()Returns the current dependencies of this instance, organized as an ordered map of file types to lists of the required files of that type.List<String>getDependencies(String type)Returns theListof files for the specified file type, if any.StringgetFormat(String type)Retrieves the configured format string for the specified file type.protected UiDependencyTool.GroupgetGroup(String name)NOTE: This method may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.Map<String,List<String>>getGroupDependencies(String name)Returns the dependencies of the specified group, organized as an ordered map of file types to lists of the required files of that type.protected UiDependencyTool.GroupmakeGroup(String name)NOTE: This method may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.UiDependencyToolon(String name)Adds all the files required for the specified group, then returns this instance.UiDependencyToolon(String type, String file)Adds the specified file to this instance's list of dependencies of the specified type, then returns this instance.Stringprint()Formats and prints all the current dependencies of this tool, using a new line in between the printed/formatted files.Stringprint(String typeOrDelim)If the parameter value is a known type, then this will format and print all of this instance's current dependencies of the specified type, using a new line in between the printed/formatted files.Stringprint(String type, String delim)Formats and prints all of this instance's current dependencies of the specified type, using the specified delimiter in between the printed/formatted files.StringprintAll(String delim)Formats and prints all the current dependencies of this tool, using the specified delimiter in between the printed/formatted files.protected voidread(String file, boolean required)Reads group info out of the specified file and into this instance.voidsetFormat(String type, String format)Sets the format string for the specified file type.StringtoString()Returns an empty String to avoid polluting the template output after a successful call toon(String)oron(String,String).protected static voidtrace(org.apache.velocity.runtime.log.Log log, String msg, Object... args)
-
-
-
Field Detail
-
GROUPS_KEY_SPACE
public static final String GROUPS_KEY_SPACE
-
TYPES_KEY_SPACE
public static final String TYPES_KEY_SPACE
-
SOURCE_FILE_KEY
public static final String SOURCE_FILE_KEY
- See Also:
- Constant Field Values
-
DEFAULT_SOURCE_FILE
public static final String DEFAULT_SOURCE_FILE
- See Also:
- Constant Field Values
-
-
Method Detail
-
trace
protected static final void trace(org.apache.velocity.runtime.log.Log log, String msg, Object... args)
-
configure
public void configure(Map params)
-
on
public UiDependencyTool on(String name)
Adds all the files required for the specified group, then returns this instance. If the group name is null or no such group exists, this will return null to indicate the error.
-
on
public UiDependencyTool on(String type, String file)
Adds the specified file to this instance's list of dependencies of the specified type, then returns this instance. If either the type or file are null, this will return null to indicate the error.
-
print
public String print()
Formats and prints all the current dependencies of this tool, using a new line in between the printed/formatted files.
-
print
public String print(String typeOrDelim)
If the parameter value is a known type, then this will format and print all of this instance's current dependencies of the specified type, using a new line in between the printed/formatted files. If the parameter value is NOT a known type, then this will treat it as a delimiter and print all of this instance's dependencies of all types, using the specified value as the delimiter in between the printed/formatted files.- See Also:
print(String,String),printAll(String)
-
print
public String print(String type, String delim)
Formats and prints all of this instance's current dependencies of the specified type, using the specified delimiter in between the printed/formatted files.
-
printAll
public String printAll(String delim)
Formats and prints all the current dependencies of this tool, using the specified delimiter in between the printed/formatted files.
-
context
public UiDependencyTool context(String path)
Sets a custom {context} variable for the formats to use.
-
getFormat
public String getFormat(String type)
Retrieves the configured format string for the specified file type.
-
setFormat
public void setFormat(String type, String format)
Sets the format string for the specified file type.
-
getDependencies
public Map<String,List<String>> getDependencies()
Returns the current dependencies of this instance, organized as an ordered map of file types to lists of the required files of that type.
-
getDependencies
public List<String> getDependencies(String type)
Returns theListof files for the specified file type, if any.
-
getGroupDependencies
public Map<String,List<String>> getGroupDependencies(String name)
Returns the dependencies of the specified group, organized as an ordered map of file types to lists of the required files of that type.
-
toString
public String toString()
Returns an empty String to avoid polluting the template output after a successful call toon(String)oron(String,String).
-
read
protected void read(String file, boolean required)
Reads group info out of the specified file and into this instance. If the file cannot be found and required is true, then this will throw an IllegalArgumentException. Otherwise, it will simply do nothing. Any checked exceptions during the actual reading of the file are caught and wrapped asRuntimeExceptions.
-
createDigester
protected org.apache.commons.digester.Digester createDigester()
Creates theDigesterused byread(java.lang.String, boolean)to create the group info for this instance out of the specified XML file.
-
format
protected String format(String format, String value)
Applies the format string to the given value. Currently, this simply replaces '{file}' with the value. If you want to handle more complicated formats, override this method.
-
getGroup
protected UiDependencyTool.Group getGroup(String name)
NOTE: This method may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.
-
makeGroup
protected UiDependencyTool.Group makeGroup(String name)
NOTE: This method may change or disappear w/o warning; don't depend on it unless you're willing to update your code whenever this changes.
-
addDependencies
protected void addDependencies(Map<String,List<String>> fbt)
Adds the specified files organized by type to this instance's current dependencies.
-
-