/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.jasper;
/** * A place holder for various things that are used through out 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. * * @author Anil K. Vijendran * @author Harish Prabandham * @author Pierre Delisle * @author Costin Manolache * @author Kin-man Chung
*/ publicclass JspCompilationContext {
privatefinal Log log = LogFactory.getLog(JspCompilationContext.class); // must not be static
// volatile so changes are visible when multiple threads request a JSP file // that has been modified privatevolatile URLClassLoader jspLoader; private URL baseUrl; privateClass<?> servletClass;
privatefinalboolean isTagFile; privateboolean protoTypeMode; private TagInfo tagInfo; private Jar tagJar;
// jspURI _must_ be relative to the context public JspCompilationContext(String jspUri, Options options,
ServletContext context, JspServletWrapper jsw,
JspRuntimeContext rctxt) { this(jspUri, null, options, context, jsw, rctxt, null, false);
}
String baseURI = jspUri.substring(0, jspUri.lastIndexOf('/') + 1); // hack fix for resolveRelativeURI if (baseURI.isEmpty()) {
baseURI = "/";
} elseif (baseURI.charAt(0) != '/') { // strip the base slash since it will be combined with the // uriBase to generate a file
baseURI = "/" + baseURI;
} if (baseURI.charAt(baseURI.length() - 1) != '/') {
baseURI += '/';
} this.baseURI = baseURI;
/* ==================== Methods to override ==================== */
// ---------- Class path and loader ----------
/** * @return the classpath that is passed off to the Java compiler.
*/ public String getClassPath() { if( classPath != null ) { return classPath;
} return rctxt.getClassPath();
}
/** * The classpath that is passed off to the Java compiler. * @param classPath The class path to use
*/ publicvoid setClassPath(String classPath) { this.classPath = classPath;
}
/** * What class loader to use for loading classes while compiling * this JSP? * @return the class loader used to load all compiled classes
*/ public ClassLoader getClassLoader() { if( loader != null ) { return loader;
} return rctxt.getParentClassLoader();
}
/** * The output directory to generate code into. The output directory * is make up of the scratch directory, which is provide in Options, * plus the directory derived from the package name. * @return the output directory in which the generated sources are placed
*/ public String getOutputDir() { if (outputDir == null) {
createOutputDir();
}
return outputDir;
}
/** * 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. * @return the Java compiler wrapper
*/ public Compiler createCompiler() { if (jspCompiler != null ) { return jspCompiler;
}
jspCompiler = null; if (options.getCompilerClassName() != null) {
jspCompiler = createCompiler(options.getCompilerClassName());
} else { if (options.getCompiler() == null) {
jspCompiler = createCompiler("org.apache.jasper.compiler.JDTCompiler"); if (jspCompiler == null) {
jspCompiler = createCompiler("org.apache.jasper.compiler.AntCompiler");
}
} else {
jspCompiler = createCompiler("org.apache.jasper.compiler.AntCompiler"); if (jspCompiler == null) {
jspCompiler = createCompiler("org.apache.jasper.compiler.JDTCompiler");
}
}
} if (jspCompiler == null) { thrownew IllegalStateException(Localizer.getMessage("jsp.error.compiler.config",
options.getCompilerClassName(), options.getCompiler()));
}
jspCompiler.init(this, jsw); return jspCompiler;
}
public Compiler getCompiler() { return jspCompiler;
}
// ---------- Access resources in the webapp ----------
/** * Get the full value of a URI relative to this compilations context * uses current file as the base. * @param uri The relative URI * @return absolute URI
*/ public String resolveRelativeUri(String uri) { // sometimes we get uri's massaged from File(String), so check for // a root directory separator char if (uri.startsWith("/") || uri.startsWith(File.separator)) { return uri;
} else { return baseURI + uri;
}
}
/** * Gets a resource as a stream, relative to the meanings of this * context's implementation. * @param res the resource to look for * @return a null if the resource cannot be found or represented * as an InputStream.
*/ public java.io.InputStream getResourceAsStream(String res) { return context.getResourceAsStream(canonicalURI(res));
}
public URL getResource(String res) throws MalformedURLException { return context.getResource(canonicalURI(res));
}
public Set<String> getResourcePaths(String path) { return context.getResourcePaths(canonicalURI(path));
}
/** * Gets the actual path of a URI relative to the context of * the compilation. * @param path The webapp path * @return the corresponding path in the filesystem
*/ public String getRealPath(String path) { if (context != null) { return context.getRealPath(path);
} return path;
}
/** * 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. * @return a JAR file
*/ public Jar getTagFileJar() { returnthis.tagJar;
}
/** * Path of the JSP URI. Note that this is not a file name. This is * the context rooted URI of the JSP file. * @return the path to the JSP
*/ public String getJspFile() { return jspUri;
}
/** * @return <code>true</code> if we are compiling a tag file * in prototype mode. * ie we only generate codes with class for the tag handler with empty * method bodies.
*/ publicboolean isPrototypeMode() { return protoTypeMode;
}
/** * 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. * @return the package name
*/ public String getServletPackageName() { if (isTagFile()) {
String className = tagInfo.getTagClassName(); int lastIndex = className.lastIndexOf('.');
String packageName = ""; if (lastIndex != -1) {
packageName = className.substring(0, lastIndex);
} return packageName;
} else {
String dPackageName = getDerivedPackageName(); if (dPackageName.length() == 0) { return basePackageName;
} return basePackageName + '.' + getDerivedPackageName();
}
}
/** * @return The base package name into which all servlet and associated code * is generated
*/ public String getBasePackageName() { return basePackageName;
}
/** * The package name into which the servlet class is generated. * @param basePackageName The package name to use
*/ publicvoid setBasePackageName(String basePackageName) { this.basePackageName = basePackageName;
}
/** * @return Full path name of the Java file into which the servlet is being * generated.
*/ public String getServletJavaFileName() { if (servletJavaFileName == null) {
servletJavaFileName = getOutputDir() + getServletClassName() + ".java";
} return servletJavaFileName;
}
/** * @return the Options object for this context.
*/ public Options getOptions() { return options;
}
public ServletContext getServletContext() { return context;
}
public JspRuntimeContext getRuntimeContext() { return rctxt;
}
/** * @return the path of the Java file relative to the work directory.
*/ public String getJavaPath() {
/** * Gets the 'location' of the TLD associated with the given taglib 'uri'. * @param uri The taglib URI * @return 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.
*/ public TldResourcePath getTldResourcePath(String uri) { return getOptions().getTldCache().getTldResourcePath(uri);
}
/** * @return <code>true</code> if generated code is kept.
*/ publicboolean keepGenerated() { return getOptions().getKeepGenerated();
}
/* * two dots in a path: go back one hierarchy. * foo/bar/../baz -> foo/baz
*/ case'.': // only if we have exactly _two_ dots. if (pos+3 < len && isPathSeparator(s.charAt(pos+3))) {
pos += 3; int separatorPos = result.length()-1; while (separatorPos >= 0 &&
! isPathSeparator(result
.charAt(separatorPos))) {
--separatorPos;
} if (separatorPos >= 0) {
result.setLength(separatorPos);
} continue;
}
}
}
}
result.append(c);
++pos;
} return result.toString();
}
}
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.