use of org.apache.jasper.JasperException in project Payara by payara.
the class AllJSPsMustBeCompilable method compile.
protected List<JasperException> compile(WebBundleDescriptor descriptor) {
String archiveUri = getAbstractArchiveUri(descriptor);
File outDir = getVerifierContext().getOutDir();
logger.log(Level.INFO, "Compiling JSPs in [ " + new File(archiveUri).getName() + " ]");
JspC jspc = new JspC();
jspc.setUriroot(new File(URI.create(archiveUri)).getAbsolutePath());
jspc.setCompile(true);
jspc.setOutputDir(outDir.getAbsolutePath());
jspc.setFailOnError(false);
setClassPath(jspc);
jspc.setSchemaResourcePrefix("/schemas/");
jspc.setDtdResourcePrefix("/dtds/");
if (logger.isLoggable(Level.FINEST))
jspc.setVerbose(1);
try {
jspc.execute();
} catch (JasperException je) {
List<JasperException> errors = jspc.getJSPCompilationErrors();
errors.add(je);
return errors;
}
return jspc.getJSPCompilationErrors();
}
use of org.apache.jasper.JasperException in project tomcat70 by apache.
the class JspDocumentParser method parse.
/*
* Parses a JSP document by responding to SAX events.
*
* @throws JasperException
*/
public static Node.Nodes parse(ParserController pc, String path, JarFile jarFile, Node parent, boolean isTagFile, boolean directivesOnly, String pageEnc, String jspConfigPageEnc, boolean isEncodingSpecifiedInProlog, boolean isBomPresent) throws JasperException {
JspDocumentParser jspDocParser = new JspDocumentParser(pc, path, isTagFile, directivesOnly);
Node.Nodes pageNodes = null;
try {
// Create dummy root and initialize it with given page encodings
Node.Root dummyRoot = new Node.Root(null, parent, true);
dummyRoot.setPageEncoding(pageEnc);
dummyRoot.setJspConfigPageEncoding(jspConfigPageEnc);
dummyRoot.setIsEncodingSpecifiedInProlog(isEncodingSpecifiedInProlog);
dummyRoot.setIsBomPresent(isBomPresent);
jspDocParser.current = dummyRoot;
if (parent == null) {
jspDocParser.addInclude(dummyRoot, jspDocParser.pageInfo.getIncludePrelude());
} else {
jspDocParser.isTop = false;
}
jspDocParser.isValidating = false;
// Parse the input
SAXParser saxParser = getSAXParser(false, jspDocParser);
InputStream inStream = null;
try {
inStream = JspUtil.getInputStream(path, jarFile, jspDocParser.ctxt, jspDocParser.err);
saxParser.parse(new InputSource(inStream), jspDocParser);
} catch (EnableDTDValidationException e) {
saxParser = getSAXParser(true, jspDocParser);
jspDocParser.isValidating = true;
try {
inStream.close();
} catch (Exception any) {
}
inStream = JspUtil.getInputStream(path, jarFile, jspDocParser.ctxt, jspDocParser.err);
saxParser.parse(new InputSource(inStream), jspDocParser);
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (Exception any) {
}
}
}
if (parent == null) {
jspDocParser.addInclude(dummyRoot, jspDocParser.pageInfo.getIncludeCoda());
}
// Create Node.Nodes from dummy root
pageNodes = new Node.Nodes(dummyRoot);
} catch (IOException ioe) {
jspDocParser.err.jspError(ioe, "jsp.error.data.file.read", path);
} catch (SAXParseException e) {
jspDocParser.err.jspError(new Mark(jspDocParser.ctxt, path, e.getLineNumber(), e.getColumnNumber()), e, e.getMessage());
} catch (Exception e) {
jspDocParser.err.jspError(e, "jsp.error.data.file.processing", path);
}
return pageNodes;
}
use of org.apache.jasper.JasperException in project tomcat70 by apache.
the class JDTCompiler method generateClass.
/**
* Compile the servlet from .java file to .class file
*/
@Override
protected void generateClass(String[] smap) throws FileNotFoundException, JasperException, Exception {
long t1 = 0;
if (log.isDebugEnabled()) {
t1 = System.currentTimeMillis();
}
final String sourceFile = ctxt.getServletJavaFileName();
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();
String packageName = ctxt.getServletPackageName();
final String targetClassName = ((packageName.length() != 0) ? (packageName + ".") : "") + ctxt.getServletClassName();
final ClassLoader classLoader = ctxt.getJspLoader();
String[] fileNames = new String[] { sourceFile };
String[] classNames = new String[] { targetClassName };
final ArrayList<JavacErrorDetail> problemList = new ArrayList<JavacErrorDetail>();
class CompilationUnit implements ICompilationUnit {
private final String className;
private final String sourceFile;
CompilationUnit(String sourceFile, String className) {
this.className = className;
this.sourceFile = sourceFile;
}
@Override
public char[] getFileName() {
return sourceFile.toCharArray();
}
@Override
public char[] getContents() {
char[] result = null;
FileInputStream is = null;
InputStreamReader isr = null;
Reader reader = null;
try {
is = new FileInputStream(sourceFile);
isr = new InputStreamReader(is, ctxt.getOptions().getJavaEncoding());
reader = new BufferedReader(isr);
char[] chars = new char[8192];
StringBuilder buf = new StringBuilder();
int count;
while ((count = reader.read(chars, 0, chars.length)) > 0) {
buf.append(chars, 0, count);
}
result = new char[buf.length()];
buf.getChars(0, result.length, result, 0);
} catch (IOException e) {
log.error("Compilation error", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
/*Ignore*/
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException ioe) {
/*Ignore*/
}
}
if (is != null) {
try {
is.close();
} catch (IOException exc) {
/*Ignore*/
}
}
}
return result;
}
@Override
public char[] getMainTypeName() {
int dot = className.lastIndexOf('.');
if (dot > 0) {
return className.substring(dot + 1).toCharArray();
}
return className.toCharArray();
}
@Override
public char[][] getPackageName() {
StringTokenizer izer = new StringTokenizer(className, ".");
char[][] result = new char[izer.countTokens() - 1][];
for (int i = 0; i < result.length; i++) {
String tok = izer.nextToken();
result[i] = tok.toCharArray();
}
return result;
}
@Override
public boolean ignoreOptionalProblems() {
return false;
}
}
final INameEnvironment env = new INameEnvironment() {
@Override
public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
StringBuilder result = new StringBuilder();
String sep = "";
for (int i = 0; i < compoundTypeName.length; i++) {
result.append(sep);
result.append(compoundTypeName[i]);
sep = ".";
}
return findType(result.toString());
}
@Override
public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
StringBuilder result = new StringBuilder();
String sep = "";
for (int i = 0; i < packageName.length; i++) {
result.append(sep);
result.append(packageName[i]);
sep = ".";
}
result.append(sep);
result.append(typeName);
return findType(result.toString());
}
private NameEnvironmentAnswer findType(String className) {
InputStream is = null;
try {
if (className.equals(targetClassName)) {
ICompilationUnit compilationUnit = new CompilationUnit(sourceFile, className);
return new NameEnvironmentAnswer(compilationUnit, null);
}
String resourceName = className.replace('.', '/') + ".class";
is = classLoader.getResourceAsStream(resourceName);
if (is != null) {
byte[] classBytes;
byte[] buf = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream(buf.length);
int count;
while ((count = is.read(buf, 0, buf.length)) > 0) {
baos.write(buf, 0, count);
}
baos.flush();
classBytes = baos.toByteArray();
char[] fileName = className.toCharArray();
ClassFileReader classFileReader = new ClassFileReader(classBytes, fileName, true);
return new NameEnvironmentAnswer(classFileReader, null);
}
} catch (IOException exc) {
log.error("Compilation error", exc);
} catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) {
log.error("Compilation error", exc);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException exc) {
// Ignore
}
}
}
return null;
}
private boolean isPackage(String result) {
if (result.equals(targetClassName)) {
return false;
}
String resourceName = result.replace('.', '/') + ".class";
InputStream is = null;
try {
is = classLoader.getResourceAsStream(resourceName);
return is == null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}
@Override
public boolean isPackage(char[][] parentPackageName, char[] packageName) {
StringBuilder result = new StringBuilder();
String sep = "";
if (parentPackageName != null) {
for (int i = 0; i < parentPackageName.length; i++) {
result.append(sep);
result.append(parentPackageName[i]);
sep = ".";
}
}
if (Character.isUpperCase(packageName[0])) {
if (!isPackage(result.toString())) {
return false;
}
}
result.append(sep);
result.append(packageName);
return isPackage(result.toString());
}
@Override
public void cleanup() {
}
};
final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
final Map<String, String> settings = new HashMap<String, String>();
settings.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
settings.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
settings.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
if (ctxt.getOptions().getJavaEncoding() != null) {
settings.put(CompilerOptions.OPTION_Encoding, ctxt.getOptions().getJavaEncoding());
}
if (ctxt.getOptions().getClassDebugInfo()) {
settings.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
}
// Source JVM
if (ctxt.getOptions().getCompilerSourceVM() != null) {
String opt = ctxt.getOptions().getCompilerSourceVM();
if (opt.equals("1.1")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_1);
} else if (opt.equals("1.2")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_2);
} else if (opt.equals("1.3")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3);
} else if (opt.equals("1.4")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
} else if (opt.equals("1.5")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
} else if (opt.equals("1.6")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
} else if (opt.equals("1.7")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_7);
} else if (opt.equals("1.8")) {
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_8);
} else if (opt.equals("1.9")) {
settings.put(CompilerOptions.OPTION_Source, // CompilerOptions.VERSION_1_9
"1.9");
} else {
log.warn("Unknown source VM " + opt + " ignored.");
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
}
} else {
// Default to 1.6
settings.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
}
// Target JVM
if (ctxt.getOptions().getCompilerTargetVM() != null) {
String opt = ctxt.getOptions().getCompilerTargetVM();
if (opt.equals("1.1")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1);
} else if (opt.equals("1.2")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_2);
} else if (opt.equals("1.3")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_3);
} else if (opt.equals("1.4")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_4);
} else if (opt.equals("1.5")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
settings.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
} else if (opt.equals("1.6")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
settings.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
} else if (opt.equals("1.7")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_7);
settings.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_7);
} else if (opt.equals("1.8")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_8);
settings.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_8);
} else if (opt.equals("1.9")) {
settings.put(CompilerOptions.OPTION_TargetPlatform, // CompilerOptions.VERSION_1_9
"1.9");
settings.put(CompilerOptions.OPTION_Compliance, // CompilerOptions.VERSION_1_9
"1.9");
} else {
log.warn("Unknown target VM " + opt + " ignored.");
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
}
} else {
// Default to 1.6
settings.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
settings.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
}
final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
final ICompilerRequestor requestor = new ICompilerRequestor() {
@Override
public void acceptResult(CompilationResult result) {
try {
if (result.hasProblems()) {
IProblem[] problems = result.getProblems();
for (int i = 0; i < problems.length; i++) {
IProblem problem = problems[i];
if (problem.isError()) {
String name = new String(problems[i].getOriginatingFileName());
try {
problemList.add(ErrorDispatcher.createJavacError(name, pageNodes, new StringBuilder(problem.getMessage()), problem.getSourceLineNumber(), ctxt));
} catch (JasperException e) {
log.error("Error visiting node", e);
}
}
}
}
if (problemList.isEmpty()) {
ClassFile[] classFiles = result.getClassFiles();
for (int i = 0; i < classFiles.length; i++) {
ClassFile classFile = classFiles[i];
char[][] compoundName = classFile.getCompoundName();
StringBuilder classFileName = new StringBuilder(outputDir).append('/');
for (int j = 0; j < compoundName.length; j++) {
if (j > 0) {
classFileName.append('/');
}
classFileName.append(compoundName[j]);
}
byte[] bytes = classFile.getBytes();
classFileName.append(".class");
FileOutputStream fout = null;
BufferedOutputStream bos = null;
try {
fout = new FileOutputStream(classFileName.toString());
bos = new BufferedOutputStream(fout);
bos.write(bytes);
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
}
}
}
}
}
} catch (IOException exc) {
log.error("Compilation error", exc);
}
}
};
ICompilationUnit[] compilationUnits = new ICompilationUnit[classNames.length];
for (int i = 0; i < compilationUnits.length; i++) {
String className = classNames[i];
compilationUnits[i] = new CompilationUnit(fileNames[i], className);
}
CompilerOptions cOptions = new CompilerOptions(settings);
cOptions.parseLiteralExpressionsAsConstants = true;
Compiler compiler = new Compiler(env, policy, cOptions, requestor, problemFactory);
compiler.compile(compilationUnits);
if (!ctxt.keepGenerated()) {
File javaFile = new File(ctxt.getServletJavaFileName());
javaFile.delete();
}
if (!problemList.isEmpty()) {
JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]);
errDispatcher.javacError(jeds);
}
if (log.isDebugEnabled()) {
long t2 = System.currentTimeMillis();
log.debug("Compiled " + ctxt.getServletJavaFileName() + " " + (t2 - t1) + "ms");
}
if (ctxt.isPrototypeMode()) {
return;
}
// JSR45 Support
if (!options.isSmapSuppressed()) {
SmapUtil.installSmap(smap);
}
}
use of org.apache.jasper.JasperException in project tomcat70 by apache.
the class TagLibraryInfoImpl method createValidator.
private TagLibraryValidator createValidator(TreeNode elem) throws JasperException {
String validatorClass = null;
Map<String, Object> initParams = new Hashtable<String, Object>();
Iterator<TreeNode> list = elem.findChildren();
while (list.hasNext()) {
TreeNode element = list.next();
String tname = element.getName();
if ("validator-class".equals(tname))
validatorClass = element.getBody();
else if ("init-param".equals(tname)) {
String[] initParam = createInitParam(element);
initParams.put(initParam[0], initParam[1]);
} else if (// Ignored elements
"description".equals(tname) || false) {
} else {
if (log.isWarnEnabled()) {
log.warn(Localizer.getMessage("jsp.warning.unknown.element.in.validator", tname));
}
}
}
TagLibraryValidator tlv = null;
if (validatorClass != null && !validatorClass.equals("")) {
try {
Class<?> tlvClass = ctxt.getClassLoader().loadClass(validatorClass);
tlv = (TagLibraryValidator) tlvClass.newInstance();
} catch (Exception e) {
err.jspError(e, "jsp.error.tlvclass.instantiation", validatorClass);
}
}
if (tlv != null) {
tlv.setInitParameters(initParams);
}
return tlv;
}
use of org.apache.jasper.JasperException in project tomcat70 by apache.
the class TldLocationsCache method init.
/*
* Keep processing order in sync with o.a.c.startup.TldConfig
*
* This supports a Tomcat-specific extension to the TLD search
* order defined in the JSP spec. It allows tag libraries packaged as JAR
* files to be shared by web applications by simply dropping them in a
* location that all web applications have access to (e.g.,
* <CATALINA_HOME>/lib). It also supports some of the weird and
* wonderful arrangements present when Tomcat gets embedded.
*
*/
private synchronized void init() throws JasperException {
if (initialized)
return;
try {
tldScanWebXml();
tldScanResourcePaths(WEB_INF);
JarScanner jarScanner = JarScannerFactory.getJarScanner(ctxt);
jarScanner.scan(ctxt, Thread.currentThread().getContextClassLoader(), new TldJarScannerCallback(), noTldJars);
initialized = true;
} catch (Exception ex) {
throw new JasperException(Localizer.getMessage("jsp.error.internal.tldinit", ex.getMessage()), ex);
}
}
Aggregations