Package org.apache.commons.jelly
Class JellyContext
- java.lang.Object
-
- org.apache.commons.jelly.JellyContext
-
- Direct Known Subclasses:
JellyServletContext
public class JellyContext extends Object
JellyContext
represents the Jelly context.- Version:
- $Revision: 227285 $
- Author:
- James Strachan
-
-
Field Summary
Fields Modifier and Type Field Description protected ClassLoader
classLoader
The class loader to use for instantiating application objects.protected boolean
useContextClassLoader
Do we want to use the Context ClassLoader when loading classes for instantiating new objects? Default isfalse
.
-
Constructor Summary
Constructors Constructor Description JellyContext()
Create 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears variables set by Tags.protected void
clearVariables()
Clears variables set by Tags (variables set while running a Jelly script)Script
compileScript(String uri)
Attempts to parse the script from the given uri using thegetResource(java.lang.String)
method then returns the compiled script.Script
compileScript(URL url)
Attempts to parse the script from the given URL using thegetResource(java.lang.String)
method then returns the compiled script.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
createChildContext()
Factory method to create a new child of this contextprotected URL
createRelativeURL(URL rootURL, String relativeURI)
protected 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.Object
findVariable(String name)
Finds the variable value of the given name in this context or in any other parent context.ClassLoader
getClassLoader()
Return the class loader to be used for instantiating application objects when required.URL
getCurrentURL()
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 URLJellyContext
getParent()
URL
getResource(String uri)
Returns a URL for the given resource from the specified path.InputStream
getResourceAsStream(String uri)
Attempts to open an InputStream to the given resource at the specified path.URL
getRootURL()
JellyContext
getScope(String name)
TagLibrary
getTagLibrary(String namespaceURI)
boolean
getUseContextClassLoader()
Return the boolean as to whether the context classloader should be used.Object
getVariable(String name)
Object
getVariable(String name, String scopeName)
Iterator
getVariableNames()
Map
getVariables()
Object
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
getXMLParser()
boolean
isAllowDtdToCallExternalEntities()
boolean
isCacheTags()
Returns whether caching of Tag instances, per thread, is enabled.boolean
isExport()
boolean
isExportLibraries()
Returns whether we export tag libraries to our parents contextboolean
isInherit()
boolean
isTagLibraryRegistered(String namespaceURI)
JellyContext
newJellyContext()
A factory method to create a new child context of the current context.JellyContext
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.JellyContext
runScript(File file, XMLOutput output)
Parses the script from the given File then compiles it and runs it.JellyContext
runScript(File file, XMLOutput output, boolean export, boolean inherit)
Parses the script from the given file then compiles it and runs it.JellyContext
runScript(String uri, XMLOutput output)
Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.JellyContext
runScript(String uri, XMLOutput output, boolean export, boolean inherit)
Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.JellyContext
runScript(URL url, XMLOutput output)
Parses the script from the given URL then compiles it and runs it.JellyContext
runScript(URL url, XMLOutput output, boolean export, boolean inherit)
Parses the script from the given URL then compiles it and runs it.JellyContext
runScript(InputSource source, XMLOutput output)
Parses the script from the given InputSource then compiles it and runs it.JellyContext
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 Detail
-
classLoader
protected ClassLoader 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 useContextClassLoader
Do we want to use the Context ClassLoader when loading classes for instantiating new objects? Default isfalse
.
-
-
Constructor Detail
-
JellyContext
public JellyContext()
Create a new context with the currentURL set to the rootURL
-
JellyContext
public JellyContext(URL rootURL)
Create a new context with the given rootURL- Parameters:
rootURL
- the root URL used in resolving absolute resources i.e. those starting with '/'
-
JellyContext
public JellyContext(URL rootURL, URL currentURL)
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
public JellyContext(JellyContext parent)
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
public JellyContext(JellyContext parentJellyContext, URL currentURL)
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
public JellyContext(JellyContext parentJellyContext, URL rootURL, URL currentURL)
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 Detail
-
getParent
public JellyContext getParent()
- Returns:
- the parent context for this context
-
getScope
public JellyContext getScope(String name)
- 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
public Object findVariable(String name)
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.
-
getVariableWithDefaultValue
public Object getVariableWithDefaultValue(String name, Object defaultValue)
Gets the value of the variable, or if it's not defined, return the given default value object.
-
getVariable
public Object getVariable(String name, String scopeName)
- 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
public void setVariable(String name, Object value)
Sets the value of the named variable
-
setVariable
public void setVariable(String name, String scopeName, Object value)
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
public void removeVariable(String name)
Removes the given variable
-
removeVariable
public void removeVariable(String name, String scopeName)
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
public Iterator getVariableNames()
- Returns:
- an Iterator over the current variable names in this context
-
getVariables
public Map getVariables()
- Returns:
- the Map of variables in this scope
-
setVariables
public void setVariables(Map variables)
Sets the Map of variables to use
-
newJellyContext
public JellyContext newJellyContext(Map newVariables)
A factory method to create a new child context of the current context.
-
newJellyContext
public JellyContext 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()
-
clearVariables
protected void clearVariables()
Clears variables set by Tags (variables set while running a Jelly script)- See Also:
clear()
-
registerTagLibrary
public void registerTagLibrary(String namespaceURI, TagLibrary taglib)
Registers the given tag library against the given namespace URI. This should be called before the parser is used.
-
registerTagLibrary
public void registerTagLibrary(String namespaceURI, String className)
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
public boolean isTagLibraryRegistered(String namespaceURI)
-
getTagLibrary
public TagLibrary getTagLibrary(String namespaceURI)
- Returns:
- the TagLibrary for the given namespace URI or null if one could not be found
-
compileScript
public Script compileScript(String uri) throws JellyException
Attempts to parse the script from the given uri using thegetResource(java.lang.String)
method then returns the compiled script.- Throws:
JellyException
-
compileScript
public Script compileScript(URL url) throws JellyException
Attempts to parse the script from the given URL using thegetResource(java.lang.String)
method then returns the compiled script.- Throws:
JellyException
-
compileScript
public Script compileScript(InputSource source) throws JellyException
Attempts to parse the script from the given InputSource using thegetResource(java.lang.String)
method then returns the compiled script.- Throws:
JellyException
-
getXMLParser
protected XMLParser getXMLParser()
- Returns:
- a thread pooled XMLParser to avoid the startup overhead of the XMLParser
-
createXMLParser
protected 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
public JellyContext runScript(File file, XMLOutput output) 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) 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) 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
-
runScript
public JellyContext runScript(String uri, XMLOutput output) 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(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
public URL getResource(String uri) throws MalformedURLException
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
public InputStream getResourceAsStream(String uri)
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
public URL 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
public void setRootURL(URL rootURL)
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
public URL 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
public void setCurrentURL(URL currentURL)
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
public ClassLoader 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
public void setClassLoader(ClassLoader classLoader)
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
protected URL createRelativeURL(URL rootURL, String relativeURI) throws MalformedURLException
- 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
protected URL getJellyContextURL(URL url) throws MalformedURLException
Strips off the name of a script to create a new context URL- Throws:
MalformedURLException
-
getJellyContextURL
protected URL getJellyContextURL(InputSource source) throws MalformedURLException
Strips off the name of a script to create a new context URL- Throws:
MalformedURLException
-
createChildContext
protected JellyContext createChildContext()
Factory method to create a new child of this context
-
setParent
protected void setParent(JellyContext context)
Change the parent context to the one provided- Parameters:
context
- the new parent context
-
-