【Java开源代码栏目提醒】:网学会员Java开源代码为您提供Compiler.java参考,解决您在Compiler.java学习中工作中的难题,参考学习。
/*
* $Header: /cvsroot/jetty/Jetty3/src/org/apache/jasper/compiler/Compiler.java,v 1.1 2000/10/09 17:57:40 gregwilkins Exp $
* $Revision: 1.1 $
* $Date: 2000/10/09 17:57:40 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact 3710167@qq.com.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.jasper.compiler;
import java.util.Hashtable;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import org.apache.jasper.JspCompilationContext;
import org.apache.jasper.Constants;
import org.apache.jasper.JasperException;
import org.apache.jasper.compiler.ParseException;
/**
* If you want to customize JSP compilation aspects, this class is
* something you should take a look at.
*
* Hope is that people can just extend Compiler and override things
* like isOutDated() but inherit things like compile(). This might
* change.
*
* @author Anil K. Vijendran
* @author Mandar Raje
*/
public class Compiler {
protected JavaCompiler javac;
protected Mangler mangler;
protected JspCompilationContext ctxt;
public Compiler(JspCompilationContext ctxt) {
this.ctxt = ctxt;
}
/**
* Compile the jsp file from the current engine context
*
* @return true if the class file was outdated the jsp file
* was recompiled.
*/
public boolean compile()
throws FileNotFoundException, JasperException, Exception
{
String pkgName = mangler.getPackageName();
String classFileName = mangler.getClassFileName();
ctxt.setServletPackageName(pkgName);
Constants.debug("jsp.message.package_name_is",
new Object[] { (pkgName==null)?
"[default package]":pkgName });
Constants.debug("jsp.message.class_file_name_is",
new Object[] { classFileName });
if (!isOutDated())
return false;
// Hack to avoid readign the class file every time -
// getClassName() is an _expensive_ operation, and it's needed only
// if isOutDated() return true.
String javaFileName = mangler.getJavaFileName();
ctxt.setServletJavaFileName(javaFileName);
Constants.debug("jsp.message.java_file_name_is",
new Object[] { javaFileName });
String className = mangler.getClassName();
ctxt.setServletClassName(className);
Constants.debug("jsp.message.class_name_is",
new Object[] { className });
// Need the encoding specified in the JSP 'page' directive for
// - reading the JSP page
// - writing the JSP servlet source
// - compiling the generated servlets (pass -encoding to javac).
// XXX - There are really three encodings of interest.
String jspEncoding = "8859_1"; // default per JSP spec
String javaEncoding = "UTF8"; // perhaps debatable?
// This seems to be a reasonable point to scan the JSP file
// for a 'contentType' directive. If it found then the set
// the value of 'jspEncoding to reflect the value specified.
// Note: if (true) is convenience programming. It can be
// taken out once we have a more efficient method.
if (true) {
JspReader tmpReader = JspReader.createJspReader(
ctxt.getJspFile(),
ctxt,
jspEncoding);
String newEncode = changeEncodingIfNecessary(tmpReader);
if (newEncode != null) jspEncoding = newEncode;
}
JspReader reader = JspReader.createJspReader(
ctxt.getJspFile(),
ctxt,
jspEncoding
);
ServletWriter writer =
(new ServletWriter
(new PrintWriter
(new java.io.OutputStreamWriter(
new FileOutputStream(javaFileName),javaEncoding))));
ctxt.setReader(reader);
ctxt.setWriter(writer);
ParseEventListener listener = new JspParseEventListener(ctxt);
Parser p = new Parser(reader, listener);
listener.beginPageProcessing();
p.parse();
listener.endPageProcessing();
writer.close();
String classpath = ctxt.getClassPath();
// I'm nuking
// System.getProperty("jsp.class.path", ".")
// business. If anyone badly needs this we can talk. -akv
// I'm adding tc_path_add because it solves a real problem
// and nobody has yet to come up with a better alternative.
// Note: this is in two places. Search for tc_path_add below.
// If you have one, please let me know. -Sam Ruby
String sep = System.getProperty("path.separator");
String[] argv = new String[]
{
"-encoding",
javaEncoding,
"-classpath",
System.getProperty("java.class.path")+ sep + classpath + sep +
System.getProperty("tc_path_add") + sep + ctxt.getOutputDir(),
"-d", ctxt.getOu
上一篇:
CompileException.java
下一篇:
网络能耗监测系统管理端的研究与实现