类 UiDependencyTool
java.lang.Object
org.apache.velocity.tools.view.UiDependencyTool
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>
- 版本:
- $Revision: 16660 $
- 作者:
- Nathan Bubna
-
嵌套类概要
嵌套类修饰符和类型类说明protected static class
NOTE: 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 class
NOTE: 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 class
NOTE: 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 class
NOTE: 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.private static final class
protected static class
NOTE: 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. -
字段概要
字段修饰符和类型字段说明private String
static final String
private static final List<UiDependencyTool.Type>
private Map<String,
UiDependencyTool.Group> static final String
private org.apache.velocity.runtime.log.Log
static final String
private List<UiDependencyTool.Type>
static final String
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected void
addDependencies
(Map<String, List<String>> fbt) Adds the specified files organized by type to this instance's current dependencies.protected void
Adds a file to this instance's dependencies under the specified type.void
Sets a custom {context} variable for the formats to use.protected Digester
Creates theDigester
used byread(java.lang.String, boolean)
to create the group info for this instance out of the specified XML file.private void
protected String
Applies the format string to the given value.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
(String type) Returns theList
of files for the specified file type, if any.Retrieves the configured format string for the specified file type.protected UiDependencyTool.Group
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.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.private UiDependencyTool.Type
For internal use only.protected UiDependencyTool.Group
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.Adds all the files required for the specified group, then returns this instance.Adds the specified file to this instance's list of dependencies of the specified type, then returns this instance.print()
Formats and prints all the current dependencies of this tool, using a new line in between the printed/formatted files.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.Formats and prints all of this instance's current dependencies of the specified type, using the specified delimiter in between the printed/formatted files.Formats and prints all the current dependencies of this tool, using the specified delimiter in between the printed/formatted files.protected void
Reads group info out of the specified file and into this instance.void
Sets the format string for the specified file type.toString()
Returns an empty String to avoid polluting the template output after a successful call toon(String)
oron(String,String)
.private URL
protected static final void
-
字段详细资料
-
构造器详细资料
-
UiDependencyTool
public UiDependencyTool()
-
-
方法详细资料
-
debug
-
trace
-
configure
-
on
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
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
Formats and prints all the current dependencies of this tool, using a new line in between the printed/formatted files. -
print
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. -
print
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
Formats and prints all the current dependencies of this tool, using the specified delimiter in between the printed/formatted files. -
context
Sets a custom {context} variable for the formats to use. -
getFormat
Retrieves the configured format string for the specified file type. -
setFormat
Sets the format string for the specified file type. -
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
Returns theList
of files for the specified file type, if any. -
getGroupDependencies
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
Returns an empty String to avoid polluting the template output after a successful call toon(String)
oron(String,String)
. -
read
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 asRuntimeException
s. -
createDigester
protected Digester createDigester()Creates theDigester
used byread(java.lang.String, boolean)
to create the group info for this instance out of the specified XML file. -
format
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
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
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
Adds the specified files organized by type to this instance's current dependencies. -
addFile
Adds a file to this instance's dependencies under the specified type. -
getType
For internal use only. Use/override get/setFormat instead. -
toURL
-