# Class JspCompilationContext

java.lang.Object
org.apache.jasper.JspCompilationContext

public class JspCompilationContext extends Object
A placeholder for various things that are used throughout the JSP engine. This is a per-request/per-context data structure. Some of the instance variables are set at different points. Most of the path-related stuff is here - mangling names, versions, dirs, loading resources and dealing with uris.

## Constructor Summary

Constructors
Constructor
Description
`JspCompilationContext(String tagfile, TagInfo tagInfo, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt, Jar tagJar)`
Creates a compilation context for a tag file.
`JspCompilationContext(String jspUri, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt)`
Creates a compilation context for a JSP page.

## Method Summary

Modifier and Type
Method
Description
`protected static String`
`canonicalURI(String s)`
Canonicalizes a URI string by resolving relative path components like '.' and '..' and collapsing multiple separators.
`void`
`checkOutputDir()`
Checks and creates the output directory if it does not exist.
`void`
`clearJspLoader()`
Clears the JSP class loader so it will be recreated on next access.
`void`
`compile()`
Compiles the JSP if it is out of date with respect to the generated class file.
`Compiler`
`createCompiler()`
Create a "Compiler" object based on some init param data.
`protected Compiler`
`createCompiler(String className)`
Creates a compiler instance by class name using reflection.
`protected void`
`createOutputDir()`
Creates the output directory path based on the package name and scratch directory.
`String`
`getBasePackageName()`
Returns the base package name into which all servlet and associated code is generated.
`String`
`getClassFileName()`
Returns the full path name of the compiled class file.
`ClassLoader`
`getClassLoader()`
What class loader to use for loading classes while compiling this JSP?
`String`
`getClassPath()`
Returns the classpath used by the Java compiler for this JSP.
`Compiler`
`getCompiler()`
Returns the compiler instance for this compilation context.
`protected String`
`getDerivedPackageName()`
Returns the derived package name based on the JSP URI path hierarchy.
`String`
`getFQCN()`
Returns the fully qualified class name of the generated servlet or tag handler.
`String`
`getJavaPath()`
Returns the path of the Java file relative to the work directory.
`String`
`getJspFile()`
Path of the JSP URI.
`ClassLoader`
`getJspLoader()`
Returns the JSP-specific class loader, creating it if necessary.
`Long`
`getLastModified(String resource)`
Returns the last modified time of the given resource using the default tag JAR.
`Long`
`getLastModified(String resource, Jar tagJar)`
Returns the last modified time of the given resource.
`Options`
`getOptions()`
Returns the options object for this compilation context.
`String`
`getOutputDir()`
The output directory to generate code into.
`String`
`getRealPath(String path)`
Gets the actual path of a URI relative to the context of the compilation.
`URL`
`getResource(String res)`
Gets a resource as a URL, relative to the context of this compilation.
`InputStream`
`getResourceAsStream(String res)`
Gets a resource as a stream, relative to the meanings of this context's implementation.
`Set<String>`
`getResourcePaths(String path)`
Gets the resource paths beneath the given path, relative to the context.
`JspRuntimeContext`
`getRuntimeContext()`
Returns the JSP runtime context for this compilation context.
`String`
`getServletClassName()`
Just the class name (does not include package name) of the generated class.
`ServletContext`
`getServletContext()`
Returns the servlet context for this compilation context.
`String`
`getServletJavaFileName()`
Returns the full path name of the Java file into which the servlet is being generated.
`String`
`getServletPackageName()`
Package name for the generated class is made up of the base package name, which is user settable, and the derived package name.
`Jar`
`getTagFileJar()`
Returns the JAR file in which the tag file for which this JspCompilationContext was created is packaged, or null if this JspCompilationContext does not correspond to a tag file, or if the corresponding tag file is not packaged in a JAR.
`TagInfo`
`getTagInfo()`
Returns the tag information for this compilation context.
`TldResourcePath`
`getTldResourcePath(String uri)`
Gets the 'location' of the TLD associated with the given taglib 'uri'.
`ServletWriter`
`getWriter()`
Returns the writer used to write the generated Servlet source code.
`void`
`incrementRemoved()`
Increments the removed counter and removes the wrapper from the runtime context.
`protected static boolean`
`isPathSeparator(char c)`
Checks whether the given character is a path separator.
`boolean`
`isPrototypeMode()`
Returns whether we are compiling a tag file in prototype mode, i.e., generating code with class for the tag handler with empty method bodies.
`boolean`
`isRemoved()`
Returns whether this JSP has been removed.
`boolean`
`isTagFile()`
Returns whether this compilation context corresponds to a tag file.
`boolean`
`keepGenerated()`
Returns whether the generated code is kept after compilation.
`Class<?>`
`load()`
Loads the compiled servlet class using the JSP class loader.
`protected boolean`
`makeOutputDir()`
Creates the output directory for generated files.
`String`
`resolveRelativeUri(String uri)`
Get the full value of a URI relative to this compilations context uses current file as the base.
`void`
`setBasePackageName(String basePackageName)`
The package name into which the servlet class is generated.
`void`
`setClassLoader(ClassLoader loader)`
Sets the class loader to use for loading classes while compiling this JSP.
`void`
`setClassPath(String classPath)`
The classpath that is passed off to the Java compiler.
`void`
`setPrototypeMode(boolean pm)`
Sets the prototype mode for tag file compilation.
`void`
`setServletClassName(String className)`
Sets the class name for the generated servlet.
`void`
`setTagFileJar(Jar tagJar)`
Sets the JAR file in which the tag file is packaged.
`void`
`setTagInfo(TagInfo tagi)`
Sets the tag information for this compilation context.
`void`
`setWriter(ServletWriter writer)`
Sets the writer used to write the generated Servlet source code.

### Methods inherited from class Object

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

## Constructor Details

### JspCompilationContext

public JspCompilationContext(String jspUri, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt)
Creates a compilation context for a JSP page.

Parameters:
  `jspUri` - The URI of the JSP page relative to the context
  `options` - The compilation options
  `context` - The servlet context
  `jsw` - The JSP servlet wrapper
  `rctxt` - The JSP runtime context

### JspCompilationContext

public JspCompilationContext(String tagfile, TagInfo tagInfo, Options options, ServletContext context, JspServletWrapper jsw, JspRuntimeContext rctxt, Jar tagJar)
Creates a compilation context for a tag file.

Parameters:
  `tagfile` - The URI of the tag file
  `tagInfo` - The tag information
  `options` - The compilation options
  `context` - The servlet context
  `jsw` - The JSP servlet wrapper
  `rctxt` - The JSP runtime context
  `tagJar` - The JAR containing the tag file

## Method Details

### getClassPath

public String getClassPath()
Returns the classpath used by the Java compiler for this JSP.

Returns:
  the classpath that is passed off to the Java compiler

### setClassPath

public void setClassPath(String classPath)
The classpath that is passed off to the Java compiler.

Parameters:
  `classPath` - The class path to use

### getClassLoader

public ClassLoader getClassLoader()
What class loader to use for loading classes while compiling this JSP?

Returns:
  the class loader used to load all compiled classes

### setClassLoader

public void setClassLoader(ClassLoader loader)
Sets the class loader to use for loading classes while compiling this JSP.

Parameters:
  `loader` - The class loader

### getJspLoader

public ClassLoader getJspLoader()
Returns the JSP-specific class loader, creating it if necessary.

Returns:
  the JSP class loader

### clearJspLoader

public void clearJspLoader()
Clears the JSP class loader so it will be recreated on next access.

### getOutputDir

public String getOutputDir()
The output directory to generate code into. The output directory is make up of the scratch directory, which is provided in Options, plus the directory derived from the package name.

Returns:
  the output directory in which the generated sources are placed

### createCompiler

public Compiler createCompiler()
Create a "Compiler" object based on some init param data. This is not done yet. Right now we're just hardcoding the actual compilers that are created.

Returns:
  the Java compiler wrapper

### createCompiler

protected Compiler createCompiler(String className)
Creates a compiler instance by class name using reflection.

Parameters:
  `className` - The fully qualified compiler class name
Returns:
  the compiler instance, or `null` if the class cannot be loaded

### getCompiler

public Compiler getCompiler()
Returns the compiler instance for this compilation context.

Returns:
  the compiler instance

### resolveRelativeUri

public String resolveRelativeUri(String uri)
Get the full value of a URI relative to this compilations context uses current file as the base.

Parameters:
  `uri` - The relative URI
Returns:
  absolute URI

### getResourceAsStream

public InputStream getResourceAsStream(String res)
Gets a resource as a stream, relative to the meanings of this context's implementation.

Parameters:
  `res` - the resource to look for
Returns:
  a null if the resource cannot be found or represented as an InputStream.

### getResource

public URL getResource(String res) throws MalformedURLException
Gets a resource as a URL, relative to the context of this compilation.

Parameters:
  `res` - The resource path
Returns:
  the resource URL
Throws:
  `MalformedURLException` - If the resource URL is malformed

### getResourcePaths

public Set<String> getResourcePaths(String path)
Gets the resource paths beneath the given path, relative to the context.

Parameters:
  `path` - The path to list resources for
Returns:
  the set of resource paths

### getRealPath

public String getRealPath(String path)
Gets the actual path of a URI relative to the context of the compilation.

Parameters:
  `path` - The webapp path
Returns:
  the corresponding path in the filesystem

### getTagFileJar

public Jar getTagFileJar()
Returns the JAR file in which the tag file for which this JspCompilationContext was created is packaged, or null if this JspCompilationContext does not correspond to a tag file, or if the corresponding tag file is not packaged in a JAR.

Returns:
  a JAR file

### setTagFileJar

public void setTagFileJar(Jar tagJar)
Sets the JAR file in which the tag file is packaged.

Parameters:
  `tagJar` - The JAR file

### getServletClassName

public String getServletClassName()
Just the class name (does not include package name) of the generated class.

Returns:
  the class name

### setServletClassName

public void setServletClassName(String className)
Sets the class name for the generated servlet.

Parameters:
  `className` - The class name

### getJspFile

public String getJspFile()
Path of the JSP URI. Note that this is not a file name. This is the context rooted URI of the JSP file.

Returns:
  the path to the JSP

### getLastModified

public Long getLastModified(String resource)
Returns the last modified time of the given resource using the default tag JAR.

Parameters:
  `resource` - The resource path
Returns:
  the last modified time in milliseconds, or -1 if not available

### getLastModified

public Long getLastModified(String resource, Jar tagJar)
Returns the last modified time of the given resource.

Parameters:
  `resource` - The resource path
  `tagJar` - The JAR file containing the resource, or `null` for webapp resources
Returns:
  the last modified time in milliseconds, or -1 if not available

### isTagFile

public boolean isTagFile()
Returns whether this compilation context corresponds to a tag file.

Returns:
  `true` if this is a tag file

### getTagInfo

public TagInfo getTagInfo()
Returns the tag information for this compilation context.

Returns:
  the tag info, or `null` if not a tag file

### setTagInfo

public void setTagInfo(TagInfo tagi)
Sets the tag information for this compilation context.

Parameters:
  `tagi` - The tag info

### isPrototypeMode

public boolean isPrototypeMode()
Returns whether we are compiling a tag file in prototype mode, i.e., generating code with class for the tag handler with empty method bodies.

Returns:
  `true` if we are compiling a tag file in prototype mode

### setPrototypeMode

public void setPrototypeMode(boolean pm)
Sets the prototype mode for tag file compilation.

Parameters:
  `pm` - The prototype mode flag

### getServletPackageName

public String getServletPackageName()
Package name for the generated class is made up of the base package name, which is user settable, and the derived package name. The derived package name directly mirrors the file hierarchy of the JSP page.

Returns:
  the package name

### getDerivedPackageName

protected String getDerivedPackageName()
Returns the derived package name based on the JSP URI path hierarchy.

Returns:
  the derived package name

### getBasePackageName

public String getBasePackageName()
Returns the base package name into which all servlet and associated code is generated.

Returns:
  the base package name

### setBasePackageName

public void setBasePackageName(String basePackageName)
The package name into which the servlet class is generated.

Parameters:
  `basePackageName` - The package name to use

### getServletJavaFileName

public String getServletJavaFileName()
Returns the full path name of the Java file into which the servlet is being generated.

Returns:
  the full path of the generated Java file

### getOptions

public Options getOptions()
Returns the options object for this compilation context.

Returns:
  the options object

### getServletContext

public ServletContext getServletContext()
Returns the servlet context for this compilation context.

Returns:
  the servlet context

### getRuntimeContext

public JspRuntimeContext getRuntimeContext()
Returns the JSP runtime context for this compilation context.

Returns:
  the runtime context

### getJavaPath

public String getJavaPath()
Returns the path of the Java file relative to the work directory.

Returns:
  the relative path of the Java file

### getClassFileName

public String getClassFileName()
Returns the full path name of the compiled class file.

Returns:
  the class file path

### getWriter

public ServletWriter getWriter()
Returns the writer used to write the generated Servlet source code.

Returns:
  the servlet writer

### setWriter

public void setWriter(ServletWriter writer)
Sets the writer used to write the generated Servlet source code.

Parameters:
  `writer` - The servlet writer

### getTldResourcePath

public TldResourcePath getTldResourcePath(String uri)
Gets the 'location' of the TLD associated with the given taglib 'uri'.

Parameters:
  `uri` - The taglib URI
Returns:
  An array of two Strings: The first element denotes the real path to the TLD. If the path to the TLD points to a jar file, then the second element denotes the name of the TLD entry in the jar file. Returns null if the given uri is not associated with any tag library 'exposed' in the web application.

### keepGenerated

public boolean keepGenerated()
Returns whether the generated code is kept after compilation.

Returns:
  `true` if generated code is kept

### incrementRemoved

public void incrementRemoved()
Increments the removed counter and removes the wrapper from the runtime context.

### isRemoved

public boolean isRemoved()
Returns whether this JSP has been removed.

Returns:
  `true` if this JSP has been removed

### compile

public void compile() throws JasperException, FileNotFoundException
Compiles the JSP if it is out of date with respect to the generated class file.

Throws:
  `JasperException` - If a compilation error occurs
  `FileNotFoundException` - If the JSP file has been removed

### load

public Class<?> load() throws JasperException
Loads the compiled servlet class using the JSP class loader.

Returns:
  the loaded servlet class
Throws:
  `JasperException` - If the class cannot be loaded

### getFQCN

public String getFQCN()
Returns the fully qualified class name of the generated servlet or tag handler.

Returns:
  the fully qualified class name

### checkOutputDir

public void checkOutputDir()
Checks and creates the output directory if it does not exist.

### makeOutputDir

protected boolean makeOutputDir()
Creates the output directory for generated files.

Returns:
  `true` if the directory was created or already exists

### createOutputDir

protected void createOutputDir()
Creates the output directory path based on the package name and scratch directory.

### isPathSeparator

protected static boolean isPathSeparator(char c)
Checks whether the given character is a path separator.

Parameters:
  `c` - The character to check
Returns:
  `true` if the character is '/' or '\\'

### canonicalURI

protected static String canonicalURI(String s)
Canonicalizes a URI string by resolving relative path components like '.' and '..' and collapsing multiple separators.

Parameters:
  `s` - The URI string to canonicalize
Returns:
  the canonical URI, or `null` if the input is `null`