org.apache.commons.jelly

## Class JellyContext

- java.lang.Object
- - org.apache.commons.jelly.JellyContext

Direct Known Subclasses:
  JellyServletContext

    public class JellyContext
    extends java.lang.Object

`JellyContext` represents the Jelly context.

Version:
  $Revision: 227285 $
Author:
  James Strachan

### Field Summary

Fields
`protected java.lang.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 is `false`.

### Constructor Summary

Constructors
Constructor and Description
`JellyContext()` Create a new context with the currentURL set to the rootURL
`JellyContext(JellyContext parent)` Create a new context with the given parent context.
`JellyContext(JellyContext parentJellyContext, java.net.URL currentURL)` Create a new context with the given parent context.
`JellyContext(JellyContext parentJellyContext, java.net.URL rootURL, java.net.URL currentURL)` Create a new context with the given parent context.
`JellyContext(java.net.URL rootURL)` Create a new context with the given rootURL
`JellyContext(java.net.URL rootURL, java.net.URL currentURL)` Create a new context with the given rootURL and currentURL

### Method Summary

Methods
`void` `clear()` Clears variables set by Tags.
`protected void` `clearVariables()` Clears variables set by Tags (variables set while running a Jelly script)
`Script` `compileScript(org.xml.sax.InputSource source)` Attempts to parse the script from the given InputSource using the `getResource(java.lang.String)` method then returns the compiled script.
`Script` `compileScript(java.lang.String uri)` Attempts to parse the script from the given uri using the `getResource(java.lang.String)` method then returns the compiled script.
`Script` `compileScript(java.net.URL url)` Attempts to parse the script from the given URL using the `getResource(java.lang.String)` method then returns the compiled script.
`protected JellyContext` `createChildContext()` Factory method to create a new child of this context
`protected java.net.URL` `createRelativeURL(java.net.URL rootURL, java.lang.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.
`java.lang.Object` `findVariable(java.lang.String name)` Finds the variable value of the given name in this context or in any other parent context.
`java.lang.ClassLoader` `getClassLoader()` Return the class loader to be used for instantiating application objects when required.
`java.net.URL` `getCurrentURL()`
`protected java.net.URL` `getJellyContextURL(org.xml.sax.InputSource source)` Strips off the name of a script to create a new context URL
`protected java.net.URL` `getJellyContextURL(java.net.URL url)` Strips off the name of a script to create a new context URL
`JellyContext` `getParent()`
`java.net.URL` `getResource(java.lang.String uri)` Returns a URL for the given resource from the specified path.
`java.io.InputStream` `getResourceAsStream(java.lang.String uri)` Attempts to open an InputStream to the given resource at the specified path.
`java.net.URL` `getRootURL()`
`JellyContext` `getScope(java.lang.String name)`
`TagLibrary` `getTagLibrary(java.lang.String namespaceURI)`
`boolean` `getUseContextClassLoader()` Return the boolean as to whether the context classloader should be used.
`java.lang.Object` `getVariable(java.lang.String name)`
`java.lang.Object` `getVariable(java.lang.String name, java.lang.String scopeName)`
`java.util.Iterator` `getVariableNames()`
`java.util.Map` `getVariables()`
`java.lang.Object` `getVariableWithDefaultValue(java.lang.String name, java.lang.Object defaultValue)` Gets the value of the variable, or if it's not defined, return the given default value object.
`protected XMLParser` `getXMLParser()`
`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 context
`boolean` `isInherit()`
`boolean` `isTagLibraryRegistered(java.lang.String namespaceURI)`
`JellyContext` `newJellyContext()` A factory method to create a new child context of the current context.
`JellyContext` `newJellyContext(java.util.Map newVariables)` A factory method to create a new child context of the current context.
`void` `registerTagLibrary(java.lang.String namespaceURI, java.lang.String className)` Registers the given tag library class name against the given namespace URI.
`void` `registerTagLibrary(java.lang.String namespaceURI, TagLibrary taglib)` Registers the given tag library against the given namespace URI.
`void` `removeVariable(java.lang.String name)` Removes the given variable
`void` `removeVariable(java.lang.String name, java.lang.String scopeName)` Removes the given variable in the specified scope.
`JellyContext` `runScript(java.io.File file, XMLOutput output)` Parses the script from the given File then compiles it and runs it.
`JellyContext` `runScript(java.io.File file, XMLOutput output, boolean export, boolean inherit)` Parses the script from the given file then compiles it and runs it.
`JellyContext` `runScript(org.xml.sax.InputSource source, XMLOutput output)` Parses the script from the given InputSource then compiles it and runs it.
`JellyContext` `runScript(org.xml.sax.InputSource source, XMLOutput output, boolean export, boolean inherit)` Parses the script from the given InputSource then compiles it and runs it.
`JellyContext` `runScript(java.lang.String uri, XMLOutput output)` Parses the script from the given uri using the JellyContext.getResource() API then compiles it and runs it.
`JellyContext` `runScript(java.lang.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(java.net.URL url, XMLOutput output)` Parses the script from the given URL then compiles it and runs it.
`JellyContext` `runScript(java.net.URL url, XMLOutput output, boolean export, boolean inherit)` Parses the script from the given URL then compiles it and runs it.
`void` `setCacheTags(boolean cacheTags)` Sets whether caching of Tag instances, per thread, is enabled.
`void` `setClassLoader(java.lang.ClassLoader classLoader)` Set the class loader to be used for instantiating application objects when required.
`void` `setCurrentURL(java.net.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 context
`void` `setExportLibraries(boolean exportLibraries)` Sets whether we export tag libraries to our parents context
`void` `setInherit(boolean inherit)` Sets whether we should inherit variables from our parent context
`protected void` `setParent(JellyContext context)` Change the parent context to the one provided
`void` `setRootURL(java.net.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 calling `Thread.currentThread().getContextClassLoader()`) to resolve/load classes.
`void` `setVariable(java.lang.String name, java.lang.Object value)` Sets the value of the named variable
`void` `setVariable(java.lang.String name, java.lang.String scopeName, java.lang.Object value)` Sets the value of the given variable name in the given variable scope
`void` `setVariables(java.util.Map variables)` Sets the Map of variables to use

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### classLoader

    protected java.lang.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 the `useContextClassLoader` variable.

#### useContextClassLoader

    protected boolean useContextClassLoader

Do we want to use the Context ClassLoader when loading classes for instantiating new objects? Default is `false`.

### Constructor Detail

#### JellyContext

    public JellyContext()

Create a new context with the currentURL set to the rootURL

#### JellyContext

    public JellyContext(java.net.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(java.net.URL rootURL,
                java.net.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 name `parentScope`.

Parameters:
  `parent` - the parent context for the newly created context.

#### JellyContext

    public JellyContext(JellyContext parentJellyContext,
                java.net.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 name `parentScope`.

Parameters:
  `parentJellyContext` - the parent context for the newly created context.
  `currentURL` - the root URL used in resolving relative resources

#### JellyContext

    public JellyContext(JellyContext parentJellyContext,
                java.net.URL rootURL,
                java.net.URL currentURL)

Create a new context with the given parent context. The parent's variables are available in the child context under the name `parentScope`.

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(java.lang.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 java.lang.Object findVariable(java.lang.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.

#### getVariable

    public java.lang.Object getVariable(java.lang.String name)

Returns:
  the value of the given variable name

#### getVariableWithDefaultValue

    public java.lang.Object getVariableWithDefaultValue(java.lang.String name,
                                               java.lang.Object defaultValue)

Gets the value of the variable, or if it's not defined, return the given default value object.

#### getVariable

    public java.lang.Object getVariable(java.lang.String name,
                               java.lang.String scopeName)

Parameters:
  `name` - is the name of the variable
  `scopeName` - 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(java.lang.String name,
                   java.lang.Object value)

Sets the value of the named variable

#### setVariable

    public void setVariable(java.lang.String name,
                   java.lang.String scopeName,
                   java.lang.Object value)

Sets the value of the given variable name in the given variable scope

Parameters:
  `name` - is the name of the variable
  `scopeName` - 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(java.lang.String name)

Removes the given variable

#### removeVariable

    public void removeVariable(java.lang.String name,
                      java.lang.String scopeName)

Removes the given variable in the specified scope.

Parameters:
  `name` - is the name of the variable
  `scopeName` - is the optional scope name such as 'parent'. For servlet environments this could be 'application', 'session' or 'request'.

#### getVariableNames

    public java.util.Iterator getVariableNames()

Returns:
  an Iterator over the current variable names in this context

#### getVariables

    public java.util.Map getVariables()

Returns:
  the Map of variables in this scope

#### setVariables

    public void setVariables(java.util.Map variables)

Sets the Map of variables to use

#### newJellyContext

    public JellyContext newJellyContext(java.util.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(java.lang.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(java.lang.String namespaceURI,
                          java.lang.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(java.lang.String namespaceURI)

#### getTagLibrary

    public TagLibrary getTagLibrary(java.lang.String namespaceURI)

Returns:
  the TagLibrary for the given namespace URI or null if one could not be found

#### compileScript

    public Script compileScript(java.lang.String uri)
                         throws JellyException

Attempts to parse the script from the given uri using the `getResource(java.lang.String)` method then returns the compiled script.

Throws:
  `JellyException`

#### compileScript

    public Script compileScript(java.net.URL url)
                         throws JellyException

Attempts to parse the script from the given URL using the `getResource(java.lang.String)` method then returns the compiled script.

Throws:
  `JellyException`

#### compileScript

    public Script compileScript(org.xml.sax.InputSource source)
                         throws JellyException

Attempts to parse the script from the given InputSource using the `getResource(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(java.io.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(java.net.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(org.xml.sax.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(java.lang.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(java.lang.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(java.io.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(java.net.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(org.xml.sax.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 java.net.URL getResource(java.lang.String uri)
                             throws java.net.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:
  `java.net.MalformedURLException`

#### getResourceAsStream

    public java.io.InputStream getResourceAsStream(java.lang.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 java.net.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(java.net.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 java.net.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(java.net.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 to `getResource(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

#### getClassLoader

    public java.lang.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.

#### setClassLoader

    public void setClassLoader(java.lang.ClassLoader classLoader)

Set the class loader to be used for instantiating application objects when required.

Parameters:
  `classLoader` - The new class loader to use, or `null` 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 calling `Thread.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 java.net.URL createRelativeURL(java.net.URL rootURL,
                                 java.lang.String relativeURI)
                                      throws java.net.MalformedURLException

Parameters:
  `rootURL` - is the root context from which the relative URI will be applied
  `relativeURI` - 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:
  `java.net.MalformedURLException` - if the URL is invalid.

#### getJellyContextURL

    protected java.net.URL getJellyContextURL(java.net.URL url)
                                       throws java.net.MalformedURLException

Strips off the name of a script to create a new context URL

Throws:
  `java.net.MalformedURLException`

#### getJellyContextURL

    protected java.net.URL getJellyContextURL(org.xml.sax.InputSource source)
                                       throws java.net.MalformedURLException

Strips off the name of a script to create a new context URL

Throws:
  `java.net.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