Package org.apache.commons.jelly
Class JellyContext
java.lang.Object
org.apache.commons.jelly.JellyContext
- Direct Known Subclasses:
JellyServletContext
JellyContext
represents the Jelly context.
- Version:
- $Revision: 227285 $
- Author:
- James Strachan
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ClassLoader
The class loader to use for instantiating application objects.protected boolean
Do we want to use the Context ClassLoader when loading classes for instantiating new objects? Default isfalse
. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new context with the currentURL set to the rootURLJellyContext
(URL rootURL) Create a new context with the given rootURLJellyContext
(URL rootURL, URL currentURL) Create a new context with the given rootURL and currentURLJellyContext
(JellyContext parent) Create a new context with the given parent context.JellyContext
(JellyContext parentJellyContext, URL currentURL) Create a new context with the given parent context.JellyContext
(JellyContext parentJellyContext, URL rootURL, URL currentURL) Create a new context with the given parent context. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears variables set by Tags.protected void
Clears variables set by Tags (variables set while running a Jelly script)compileScript
(String uri) Attempts to parse the script from the given uri using thegetResource(java.lang.String)
method then returns the compiled script.compileScript
(URL url) Attempts to parse the script from the given URL using thegetResource(java.lang.String)
method then returns the compiled script.compileScript
(InputSource source) Attempts to parse the script from the given InputSource using thegetResource(java.lang.String)
method then returns the compiled script.protected JellyContext
Factory method to create a new child of this contextprotected URL
createRelativeURL
(URL rootURL, String relativeURI) protected XMLParser
Factory method to allow JellyContext implementations to overload how an XMLParser is created - such as to overload what the default ExpressionFactory should be.findVariable
(String name) Finds the variable value of the given name in this context or in any other parent context.Return the class loader to be used for instantiating application objects when required.protected URL
getJellyContextURL
(URL url) Strips off the name of a script to create a new context URLprotected URL
getJellyContextURL
(InputSource source) Strips off the name of a script to create a new context URLgetResource
(String uri) Returns a URL for the given resource from the specified path.Attempts to open an InputStream to the given resource at the specified path.getTagLibrary
(String namespaceURI) boolean
Return the boolean as to whether the context classloader should be used.getVariable
(String name) getVariable
(String name, String scopeName) getVariableWithDefaultValue
(String name, Object defaultValue) Gets the value of the variable, or if it's not defined, return the given default value object.protected XMLParser
boolean
boolean
Returns whether caching of Tag instances, per thread, is enabled.boolean
isExport()
boolean
Returns whether we export tag libraries to our parents contextboolean
boolean
isTagLibraryRegistered
(String namespaceURI) A factory method to create a new child context of the current context.newJellyContext
(Map newVariables) A factory method to create a new child context of the current context.void
registerTagLibrary
(String namespaceURI, String className) Registers the given tag library class name against the given namespace URI.void
registerTagLibrary
(String namespaceURI, TagLibrary taglib) Registers the given tag library against the given namespace URI.void
removeVariable
(String name) Removes the given variablevoid
removeVariable
(String name, String scopeName) Removes the given variable in the specified scope.Parses the script from the given File then compiles it and runs it.Parses the script from the given file then compiles it and runs it.Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.Parses the script from the given URL then compiles it and runs it.Parses the script from the given URL then compiles it and runs it.runScript
(InputSource source, XMLOutput output) Parses the script from the given InputSource then compiles it and runs it.runScript
(InputSource source, XMLOutput output, boolean export, boolean inherit) Parses the script from the given InputSource then compiles it and runs it.void
setAllowDtdToCallExternalEntities
(boolean allowDtdToCallExternalEntities) Sets whether we should allow our doctype definitions to call out to external entities.void
setCacheTags
(boolean cacheTags) Sets whether caching of Tag instances, per thread, is enabled.void
setClassLoader
(ClassLoader classLoader) Set the class loader to be used for instantiating application objects when required.void
setCurrentURL
(URL currentURL) Sets the current URL context of the current script that is executing.void
setExport
(boolean export) Sets whether we should export variable definitions to our parent contextvoid
setExportLibraries
(boolean exportLibraries) Sets whether we export tag libraries to our parents contextvoid
setInherit
(boolean inherit) Sets whether we should inherit variables from our parent contextprotected void
setParent
(JellyContext context) Change the parent context to the one providedvoid
setRootURL
(URL rootURL) Sets the current root context URL from which all absolute resource URIs will be relative to.void
setUseContextClassLoader
(boolean use) Determine whether to use the Context ClassLoader (the one found by callingThread.currentThread().getContextClassLoader()
) to resolve/load classes.void
setVariable
(String name, Object value) Sets the value of the named variablevoid
setVariable
(String name, String scopeName, Object value) Sets the value of the given variable name in the given variable scopevoid
setVariables
(Map variables) Sets the Map of variables to use
-
Field Details
-
classLoader
The class loader to use for instantiating application objects. If not specified, the context class loader, or the class loader used to load this class itself, is used, based on the value of theuseContextClassLoader
variable. -
useContextClassLoader
protected boolean useContextClassLoaderDo we want to use the Context ClassLoader when loading classes for instantiating new objects? Default isfalse
.
-
-
Constructor Details
-
JellyContext
public JellyContext()Create a new context with the currentURL set to the rootURL -
JellyContext
Create a new context with the given rootURL- Parameters:
rootURL
- the root URL used in resolving absolute resources i.e. those starting with '/'
-
JellyContext
Create a new context with the given rootURL and currentURL- Parameters:
rootURL
- the root URL used in resolving absolute resources i.e. those starting with '/'currentURL
- the root URL used in resolving relative resources
-
JellyContext
Create a new context with the given parent context. The parent's rootURL and currentURL are set on the child, and the parent's variables are available in the child context under the nameparentScope
.- Parameters:
parent
- the parent context for the newly created context.
-
JellyContext
Create a new context with the given parent context. The parent's rootURL are set on the child, and the parent's variables are available in the child context under the nameparentScope
.- Parameters:
parentJellyContext
- the parent context for the newly created context.currentURL
- the root URL used in resolving relative resources
-
JellyContext
Create a new context with the given parent context. The parent's variables are available in the child context under the nameparentScope
.- Parameters:
parentJellyContext
- the parent context for the newly created context.rootURL
- the root URL used in resolving absolute resources i.e. those starting with '/'currentURL
- the root URL used in resolving relative resources
-
-
Method Details
-
getParent
- Returns:
- the parent context for this context
-
getScope
- Returns:
- the scope of the given name, such as the 'parent' scope. If Jelly is used in a Servlet situation then 'request', 'session' and 'application' are other names for scopes
-
findVariable
Finds the variable value of the given name in this context or in any other parent context. If this context does not contain the variable, then its parent is used and then its parent and so forth until the context with no parent is found.- Returns:
- the value of the variable in this or one of its descendant contexts or null if the variable could not be found.
-
getVariable
- Returns:
- the value of the given variable name
-
getVariableWithDefaultValue
Gets the value of the variable, or if it's not defined, return the given default value object. -
getVariable
- Parameters:
name
- is the name of the variablescopeName
- is the optional scope name such as 'parent'. For servlet environments this could be 'application', 'session' or 'request'.- Returns:
- the value of the given variable name in the given variable scope
-
setVariable
Sets the value of the named variable -
setVariable
Sets the value of the given variable name in the given variable scope- Parameters:
name
- is the name of the variablescopeName
- is the optional scope name such as 'parent'. For servlet environments this could be 'application', 'session' or 'request'.value
- is the value of the attribute
-
removeVariable
Removes the given variable -
removeVariable
Removes the given variable in the specified scope.- Parameters:
name
- is the name of the variablescopeName
- is the optional scope name such as 'parent'. For servlet environments this could be 'application', 'session' or 'request'.
-
getVariableNames
- Returns:
- an Iterator over the current variable names in this context
-
getVariables
- Returns:
- the Map of variables in this scope
-
setVariables
Sets the Map of variables to use -
newJellyContext
A factory method to create a new child context of the current context. -
newJellyContext
A factory method to create a new child context of the current context. -
clear
public void clear()Clears variables set by Tags.- See Also:
-
clearVariables
protected void clearVariables()Clears variables set by Tags (variables set while running a Jelly script)- See Also:
-
registerTagLibrary
Registers the given tag library against the given namespace URI. This should be called before the parser is used. -
registerTagLibrary
Registers the given tag library class name against the given namespace URI. The class will be loaded via the given ClassLoader This should be called before the parser is used. -
isTagLibraryRegistered
-
getTagLibrary
- Returns:
- the TagLibrary for the given namespace URI or null if one could not be found
-
compileScript
Attempts to parse the script from the given uri using thegetResource(java.lang.String)
method then returns the compiled script.- Throws:
JellyException
-
compileScript
Attempts to parse the script from the given URL using thegetResource(java.lang.String)
method then returns the compiled script.- Throws:
JellyException
-
compileScript
Attempts to parse the script from the given InputSource using thegetResource(java.lang.String)
method then returns the compiled script.- Throws:
JellyException
-
getXMLParser
- Returns:
- a thread pooled XMLParser to avoid the startup overhead of the XMLParser
-
createXMLParser
Factory method to allow JellyContext implementations to overload how an XMLParser is created - such as to overload what the default ExpressionFactory should be. -
runScript
Parses the script from the given File then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
runScript
Parses the script from the given URL then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
runScript
Parses the script from the given InputSource then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
runScript
Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
runScript
public JellyContext runScript(String uri, XMLOutput output, boolean export, boolean inherit) throws JellyException Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
runScript
public JellyContext runScript(File file, XMLOutput output, boolean export, boolean inherit) throws JellyException Parses the script from the given file then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
runScript
public JellyContext runScript(URL url, XMLOutput output, boolean export, boolean inherit) throws JellyException Parses the script from the given URL then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
runScript
public JellyContext runScript(InputSource source, XMLOutput output, boolean export, boolean inherit) throws JellyException Parses the script from the given InputSource then compiles it and runs it.- Returns:
- the new child context that was used to run the script
- Throws:
JellyException
-
getResource
Returns a URL for the given resource from the specified path. If the uri starts with "/" then the path is taken as relative to the current context root. If the uri is a well formed URL then it is used. If the uri is a file that exists and can be read then it is used. Otherwise the uri is interpreted as relative to the current context (the location of the current script).- Throws:
MalformedURLException
-
getResourceAsStream
Attempts to open an InputStream to the given resource at the specified path. If the uri starts with "/" then the path is taken as relative to the current context root. If the uri is a well formed URL then it is used. Otherwise the uri is interpreted as relative to the current context (the location of the current script).- Returns:
- null if this resource could not be loaded, otherwise the resources input stream is returned.
-
getRootURL
- Returns:
- the current root context URL from which all absolute resource URIs will be relative to. For example in a web application the root URL will map to the web directory which contains the WEB-INF directory.
-
setRootURL
Sets the current root context URL from which all absolute resource URIs will be relative to. For example in a web application the root URL will map to the web directory which contains the WEB-INF directory. -
getCurrentURL
- Returns:
- the current URL context of the current script that is executing.
This URL context is used to deduce relative scripts when relative URIs are
used in calls to
getResource(java.lang.String)
to process relative scripts.
-
setCurrentURL
Sets the current URL context of the current script that is executing. This URL context is used to deduce relative scripts when relative URIs are used in calls togetResource(java.lang.String)
to process relative scripts. -
isCacheTags
public boolean isCacheTags()Returns whether caching of Tag instances, per thread, is enabled. Caching Tags can boost performance, on some JVMs, by reducing the cost of object construction when running Jelly inside a multi-threaded application server such as a Servlet engine.- Returns:
- whether caching of Tag instances is enabled.
-
setCacheTags
public void setCacheTags(boolean cacheTags) Sets whether caching of Tag instances, per thread, is enabled. Caching Tags can boost performance, on some JVMs, by reducing the cost of object construction when running Jelly inside a multi-threaded application server such as a Servlet engine.- Parameters:
cacheTags
- Whether caching should be enabled or disabled.
-
isExportLibraries
public boolean isExportLibraries()Returns whether we export tag libraries to our parents context- Returns:
- boolean
-
setExportLibraries
public void setExportLibraries(boolean exportLibraries) Sets whether we export tag libraries to our parents context- Parameters:
exportLibraries
- The exportLibraries to set
-
setExport
public void setExport(boolean export) Sets whether we should export variable definitions to our parent context -
isExport
public boolean isExport()- Returns:
- whether we should export variable definitions to our parent context
-
setInherit
public void setInherit(boolean inherit) Sets whether we should inherit variables from our parent context -
isInherit
public boolean isInherit()- Returns:
- whether we should inherit variables from our parent context
-
setAllowDtdToCallExternalEntities
public void setAllowDtdToCallExternalEntities(boolean allowDtdToCallExternalEntities) Sets whether we should allow our doctype definitions to call out to external entities. -
isAllowDtdToCallExternalEntities
public boolean isAllowDtdToCallExternalEntities()- Returns:
- whether we should allow our doctype definitions to call out to external entities.
-
getClassLoader
Return the class loader to be used for instantiating application objects when required. This is determined based upon the following rules:- The class loader set by
setClassLoader()
, if any - The thread context class loader, if it exists and the
useContextClassLoader
property is set to true - The class loader used to load the XMLParser class itself.
- The class loader set by
-
setClassLoader
Set the class loader to be used for instantiating application objects when required.- Parameters:
classLoader
- The new class loader to use, ornull
to revert to the standard rules
-
getUseContextClassLoader
public boolean getUseContextClassLoader()Return the boolean as to whether the context classloader should be used. -
setUseContextClassLoader
public void setUseContextClassLoader(boolean use) Determine whether to use the Context ClassLoader (the one found by callingThread.currentThread().getContextClassLoader()
) to resolve/load classes. If not using Context ClassLoader, then the class-loading defaults to using the calling-class' ClassLoader.- Parameters:
use
- determines whether to use JellyContext ClassLoader.
-
createRelativeURL
- Parameters:
rootURL
- is the root context from which the relative URI will be appliedrelativeURI
- is the relative URI (without a leading "/")- Returns:
- a new relative URL from the given root and with the addition of the extra relative URI
- Throws:
MalformedURLException
- if the URL is invalid.
-
getJellyContextURL
Strips off the name of a script to create a new context URL- Throws:
MalformedURLException
-
getJellyContextURL
Strips off the name of a script to create a new context URL- Throws:
MalformedURLException
-
createChildContext
Factory method to create a new child of this context -
setParent
Change the parent context to the one provided- Parameters:
context
- the new parent context
-