git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273936 13f79535-47bb-0310-9956-ffa450edef68master
@@ -9,6 +9,7 @@ | |||||
<property name="jsp.dir" location="jsp"/> | <property name="jsp.dir" location="jsp"/> | ||||
<property name="jsp.output.dir" location="${jsp.dir}/java"/> | <property name="jsp.output.dir" location="${jsp.dir}/java"/> | ||||
<property name="jsp.verbosity" value="3"/> | <property name="jsp.verbosity" value="3"/> | ||||
<property name="jsp.compiler" value="jasper41"/> | |||||
<mkdir dir="${jsp.output.dir}"/> | <mkdir dir="${jsp.output.dir}"/> | ||||
</target> | </target> | ||||
@@ -23,6 +24,7 @@ | |||||
<jspc | <jspc | ||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
srcdir="${jsp.dir}" | srcdir="${jsp.dir}" | ||||
compiler="${jsp.compiler}" | |||||
verbose="${jsp.verbosity}"> | verbose="${jsp.verbosity}"> | ||||
<include | <include | ||||
name="missing_tld.jsp"/> | name="missing_tld.jsp"/> | ||||
@@ -30,6 +32,7 @@ | |||||
</target> | </target> | ||||
<!-- this should compile to simple.java --> | <!-- this should compile to simple.java --> | ||||
<!-- also, stick to the default compiler here to ensure it still works--> | |||||
<target name="testSimple" depends="init"> | <target name="testSimple" depends="init"> | ||||
<jspc | <jspc | ||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
@@ -46,6 +49,7 @@ | |||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
uriroot="${jsp.dir}" | uriroot="${jsp.dir}" | ||||
srcdir="${jsp.dir}" | srcdir="${jsp.dir}" | ||||
compiler="${jsp.compiler}" | |||||
verbose="${jsp.verbosity}"> | verbose="${jsp.verbosity}"> | ||||
<include | <include | ||||
name="uriroot.jsp"/> | name="uriroot.jsp"/> | ||||
@@ -58,6 +62,7 @@ | |||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
uriroot="${jsp.dir}" | uriroot="${jsp.dir}" | ||||
srcdir="${jsp.dir}" | srcdir="${jsp.dir}" | ||||
compiler="${jsp.compiler}" | |||||
verbose="${jsp.verbosity}"> | verbose="${jsp.verbosity}"> | ||||
<include name="xml.jsp"/> | <include name="xml.jsp"/> | ||||
</jspc> | </jspc> | ||||
@@ -68,6 +73,7 @@ | |||||
<jspc | <jspc | ||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
srcdir="${jsp.dir}" | srcdir="${jsp.dir}" | ||||
compiler="${jsp.compiler}" | |||||
verbose="${jsp.verbosity}"> | verbose="${jsp.verbosity}"> | ||||
<include | <include | ||||
name="default.jsp"/> | name="default.jsp"/> | ||||
@@ -79,6 +85,7 @@ | |||||
<jspc | <jspc | ||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
srcdir="${jsp.dir}" | srcdir="${jsp.dir}" | ||||
compiler="${jsp.compiler}" | |||||
verbose="${jsp.verbosity}"> | verbose="${jsp.verbosity}"> | ||||
<include | <include | ||||
name="1nvalid-classname.jsp"/> | name="1nvalid-classname.jsp"/> | ||||
@@ -90,6 +97,7 @@ | |||||
<jspc | <jspc | ||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
srcdir="${jsp.dir}" | srcdir="${jsp.dir}" | ||||
compiler="${jsp.compiler}" | |||||
verbose="${jsp.verbosity}"> | verbose="${jsp.verbosity}"> | ||||
<include | <include | ||||
name="wrong_type.txt"/> | name="wrong_type.txt"/> | ||||
@@ -100,6 +108,7 @@ | |||||
<target name="testWebapp" depends="init"> | <target name="testWebapp" depends="init"> | ||||
<jspc | <jspc | ||||
destdir="${jsp.output.dir}" | destdir="${jsp.output.dir}" | ||||
compiler="${jsp.compiler}" | |||||
verbose="${jsp.verbosity}"> | verbose="${jsp.verbosity}"> | ||||
<webapp basedir="${jsp.dir}" /> | <webapp basedir="${jsp.dir}" /> | ||||
</jspc> | </jspc> | ||||
@@ -106,7 +106,6 @@ import org.apache.tools.ant.types.Reference; | |||||
* @since 1.5 | * @since 1.5 | ||||
*/ | */ | ||||
public class JspC extends MatchingTask { | public class JspC extends MatchingTask { | ||||
/* ------------------------------------------------------------ */ | |||||
private Path classpath; | private Path classpath; | ||||
private Path compilerClasspath; | private Path compilerClasspath; | ||||
private Path src; | private Path src; | ||||
@@ -155,11 +154,11 @@ public class JspC extends MatchingTask { | |||||
private static final String FAIL_MSG | private static final String FAIL_MSG | ||||
= "Compile failed, messages should have been provided."; | = "Compile failed, messages should have been provided."; | ||||
/* ------------------------------------------------------------ */ | |||||
/** | /** | ||||
* Path for source JSP files. | |||||
* Set the path for source JSP files. | |||||
*/ | */ | ||||
public void setSrcdir(Path srcDir) { | |||||
public void setSrcDir(Path srcDir) { | |||||
if (src == null) { | if (src == null) { | ||||
src = srcDir; | src = srcDir; | ||||
} else { | } else { | ||||
@@ -169,7 +168,7 @@ public class JspC extends MatchingTask { | |||||
public Path getSrcDir(){ | public Path getSrcDir(){ | ||||
return src; | return src; | ||||
} | } | ||||
/* ------------------------------------------------------------ */ | |||||
/** | /** | ||||
* Set the destination directory into which the JSP source | * Set the destination directory into which the JSP source | ||||
* files should be compiled. | * files should be compiled. | ||||
@@ -180,7 +179,6 @@ public class JspC extends MatchingTask { | |||||
public File getDestdir(){ | public File getDestdir(){ | ||||
return destDir; | return destDir; | ||||
} | } | ||||
/* ------------------------------------------------------------ */ | |||||
/** | /** | ||||
* Set the name of the package the compiled jsp files should be in. | * Set the name of the package the compiled jsp files should be in. | ||||
@@ -193,7 +191,6 @@ public class JspC extends MatchingTask { | |||||
return packageName; | return packageName; | ||||
} | } | ||||
/* ------------------------------------------------------------ */ | |||||
/** | /** | ||||
* Set the verbose level of the compiler | * Set the verbose level of the compiler | ||||
*/ | */ | ||||
@@ -204,7 +201,6 @@ public class JspC extends MatchingTask { | |||||
return verbose; | return verbose; | ||||
} | } | ||||
/* ------------------------------------------------------------ */ | |||||
/** | /** | ||||
* Whether or not the build should halt if compilation fails. | * Whether or not the build should halt if compilation fails. | ||||
* Defaults to <code>true</code>. | * Defaults to <code>true</code>. | ||||
@@ -218,15 +214,15 @@ public class JspC extends MatchingTask { | |||||
public boolean getFailonerror() { | public boolean getFailonerror() { | ||||
return failOnError; | return failOnError; | ||||
} | } | ||||
/* ------------------------------------------------------------ */ | |||||
public String getIeplugin() { | public String getIeplugin() { | ||||
return iepluginid; | return iepluginid; | ||||
} | } | ||||
/** | /** | ||||
* Java Plugin CLASSID for Internet Explorer | * Java Plugin CLASSID for Internet Explorer | ||||
*/ | */ | ||||
public void setIeplugin(String iepluginid_) { | |||||
iepluginid = iepluginid_; | |||||
public void setIeplugin(String iepluginid) { | |||||
this.iepluginid = iepluginid; | |||||
} | } | ||||
/** | /** | ||||
@@ -237,12 +233,13 @@ public class JspC extends MatchingTask { | |||||
public boolean isMapped() { | public boolean isMapped() { | ||||
return mapped; | return mapped; | ||||
} | } | ||||
/** | /** | ||||
* If true, generate separate write() calls for each HTML line | * If true, generate separate write() calls for each HTML line | ||||
* in the JSP. | * in the JSP. | ||||
*/ | */ | ||||
public void setMapped(boolean mapped_) { | |||||
mapped = mapped_; | |||||
public void setMapped(boolean mapped) { | |||||
this.mapped = mapped; | |||||
} | } | ||||
/** | /** | ||||
@@ -426,12 +423,12 @@ public class JspC extends MatchingTask { | |||||
// make sure that we've got a srcdir | // make sure that we've got a srcdir | ||||
if (src == null) { | if (src == null) { | ||||
throw new BuildException("srcdir attribute must be set!", | throw new BuildException("srcdir attribute must be set!", | ||||
location); | |||||
} | |||||
getLocation()); | |||||
} | |||||
String [] list = src.list(); | String [] list = src.list(); | ||||
if (list.length == 0) { | if (list.length == 0) { | ||||
throw new BuildException("srcdir attribute must be set!", | throw new BuildException("srcdir attribute must be set!", | ||||
location); | |||||
getLocation()); | |||||
} | } | ||||
@@ -449,7 +446,7 @@ public class JspC extends MatchingTask { | |||||
resetFileLists(); | resetFileLists(); | ||||
int filecount = 0; | int filecount = 0; | ||||
for (int i = 0; i < list.length; i++) { | for (int i = 0; i < list.length; i++) { | ||||
File srcDir = (File) getProject().resolveFile(list[i]); | |||||
File srcDir = getProject().resolveFile(list[i]); | |||||
if (!srcDir.exists()) { | if (!srcDir.exists()) { | ||||
throw new BuildException("srcdir \"" + srcDir.getPath() + | throw new BuildException("srcdir \"" + srcDir.getPath() + | ||||
"\" does not exist!", getLocation()); | "\" does not exist!", getLocation()); | ||||
@@ -55,7 +55,7 @@ package org.apache.tools.ant.taskdefs.optional.jsp; | |||||
import java.io.File; | import java.io.File; | ||||
/** | /** | ||||
* This is a class derived from the Jasper code | |||||
* This is a class derived from the Jasper code | |||||
* (org.apache.jasper.compiler.CommandLineCompiler) to map from a JSP filename | * (org.apache.jasper.compiler.CommandLineCompiler) to map from a JSP filename | ||||
* to a valid Java classname. | * to a valid Java classname. | ||||
* | * | ||||
@@ -159,7 +159,7 @@ public class JspNameMangler implements JspMangler { | |||||
* definition of the char escaping algorithm | * definition of the char escaping algorithm | ||||
* | * | ||||
* @param ch char to mangle | * @param ch char to mangle | ||||
* @return mangled string; 5 digit hex value | |||||
* @return mangled string; 5 digit hex value | |||||
*/ | */ | ||||
private static final String mangleChar(char ch) { | private static final String mangleChar(char ch) { | ||||
@@ -183,7 +183,7 @@ public class JspNameMangler implements JspMangler { | |||||
/** | /** | ||||
* taking in the substring representing the path relative to the source dir | * taking in the substring representing the path relative to the source dir | ||||
* return a new string representing the destination path | * return a new string representing the destination path | ||||
* @todo | |||||
* not supported, as jasper in tomcat4.0 doesnt either | |||||
*/ | */ | ||||
public String mapPath(String path) { | public String mapPath(String path) { | ||||
return null; | return null; | ||||
@@ -73,6 +73,17 @@ import org.apache.tools.ant.types.Path; | |||||
* @since ant1.5 | * @since ant1.5 | ||||
*/ | */ | ||||
public class JasperC extends DefaultJspCompilerAdapter { | public class JasperC extends DefaultJspCompilerAdapter { | ||||
/** | |||||
* what produces java classes from .jsp files | |||||
*/ | |||||
JspMangler mangler; | |||||
public JasperC(JspMangler mangler) { | |||||
this.mangler = mangler; | |||||
} | |||||
/** | /** | ||||
* our execute method | * our execute method | ||||
*/ | */ | ||||
@@ -80,28 +91,11 @@ public class JasperC extends DefaultJspCompilerAdapter { | |||||
throws BuildException { | throws BuildException { | ||||
getJspc().log("Using jasper compiler", Project.MSG_VERBOSE); | getJspc().log("Using jasper compiler", Project.MSG_VERBOSE); | ||||
CommandlineJava cmd = setupJasperCommand(); | CommandlineJava cmd = setupJasperCommand(); | ||||
/* | |||||
Path classpath=cmd.createClasspath(getProject()); | |||||
if (getJspc().getClasspath() != null) { | |||||
classpath=getJspc().getClasspath(); | |||||
} else { | |||||
classpath.concatSystemClasspath(); | |||||
} | |||||
ExecuteJava exec=new ExecuteJava(); | |||||
exec.execute(getProject()); | |||||
if ((err = executeJava()) != 0) { | |||||
if (failOnError) { | |||||
throw new BuildException("Java returned: " + err, location); | |||||
} else { | |||||
log("Java Result: " + err, Project.MSG_ERR); | |||||
} | |||||
*/ | |||||
try { | try { | ||||
// Create an instance of the compiler, redirecting output to | // Create an instance of the compiler, redirecting output to | ||||
// the project log | // the project log | ||||
// REVISIT. ugly. | |||||
Java java = (Java) (getProject().createTask("java")); | Java java = (Java) (getProject().createTask("java")); | ||||
if (getJspc().getClasspath() != null) { | if (getJspc().getClasspath() != null) { | ||||
getProject().log("using user supplied classpath: "+getJspc().getClasspath(), | getProject().log("using user supplied classpath: "+getJspc().getClasspath(), | ||||
@@ -175,6 +169,6 @@ public class JasperC extends DefaultJspCompilerAdapter { | |||||
*/ | */ | ||||
public JspMangler createMangler() { | public JspMangler createMangler() { | ||||
return new JspNameMangler(); | |||||
return mangler; | |||||
} | } | ||||
} | } |
@@ -56,6 +56,8 @@ package org.apache.tools.ant.taskdefs.optional.jsp.compilers; | |||||
import org.apache.tools.ant.AntClassLoader; | import org.apache.tools.ant.AntClassLoader; | ||||
import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
import org.apache.tools.ant.Task; | import org.apache.tools.ant.Task; | ||||
import org.apache.tools.ant.taskdefs.optional.jsp.JspNameMangler; | |||||
import org.apache.tools.ant.taskdefs.optional.jsp.Jasper41Mangler; | |||||
/** | /** | ||||
@@ -63,6 +65,7 @@ import org.apache.tools.ant.Task; | |||||
* | * | ||||
* @author <a href="mailto:jayglanville@home.com">J D Glanville</a> | * @author <a href="mailto:jayglanville@home.com">J D Glanville</a> | ||||
* @author Matthew Watson <a href="mailto:mattw@i3sp.com">mattw@i3sp.com</a> | * @author Matthew Watson <a href="mailto:mattw@i3sp.com">mattw@i3sp.com</a> | ||||
* @author Steve Loughran | |||||
*/ | */ | ||||
public class JspCompilerAdapterFactory { | public class JspCompilerAdapterFactory { | ||||
@@ -112,11 +115,14 @@ public class JspCompilerAdapterFactory { | |||||
public static JspCompilerAdapter getCompiler(String compilerType, Task task, | public static JspCompilerAdapter getCompiler(String compilerType, Task task, | ||||
AntClassLoader loader) | AntClassLoader loader) | ||||
throws BuildException { | throws BuildException { | ||||
/* If I've done things right, this should be the extent of the | |||||
* conditional statements required. | |||||
*/ | |||||
if (compilerType.equalsIgnoreCase("jasper")) { | if (compilerType.equalsIgnoreCase("jasper")) { | ||||
return new JasperC(); | |||||
//tomcat4.0 gets the old mangler | |||||
return new JasperC(new JspNameMangler()); | |||||
} | |||||
if (compilerType.equalsIgnoreCase("jasper41")) { | |||||
//tomcat4.1 gets the new one | |||||
return new JasperC(new Jasper41Mangler()); | |||||
} | } | ||||
return resolveClassName(compilerType, loader); | return resolveClassName(compilerType, loader); | ||||
} | } | ||||
@@ -57,6 +57,12 @@ import java.io.*; | |||||
import java.util.Properties; | import java.util.Properties; | ||||
import org.apache.tools.ant.BuildFileTest; | import org.apache.tools.ant.BuildFileTest; | ||||
import org.apache.tools.ant.taskdefs.optional.jsp.JspMangler; | |||||
import org.apache.tools.ant.taskdefs.optional.jsp.Jasper41Mangler; | |||||
import org.apache.tools.ant.taskdefs.optional.jsp.JspC; | |||||
import org.apache.tools.ant.taskdefs.optional.jsp.JspNameMangler; | |||||
import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapterFactory; | |||||
import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapter; | |||||
/** | /** | ||||
* Tests the Jspc task. | * Tests the Jspc task. | ||||
@@ -163,7 +169,22 @@ public class JspcTest extends BuildFileTest { | |||||
} | } | ||||
/** | |||||
* A unit test for JUnit | |||||
*/ | |||||
public void testNotAJspFile() throws Exception { | |||||
executeTarget("testNotAJspFile"); | |||||
} | |||||
/** | |||||
* webapp test is currently broken, because it picks up | |||||
* on the missing_tld file, and bails. | |||||
*/ | |||||
/* | |||||
public void testWebapp() throws Exception { | |||||
executeTarget("testWebapp"); | |||||
} | |||||
*/ | |||||
/** | /** | ||||
* run a target then verify the named file gets created | * run a target then verify the named file gets created | ||||
* | * | ||||
@@ -191,7 +212,6 @@ public class JspcTest extends BuildFileTest { | |||||
assertTrue("file " + filename + " is empty", file.length() > 0); | assertTrue("file " + filename + " is empty", file.length() > 0); | ||||
} | } | ||||
/** | /** | ||||
* Gets the OutputFile attribute of the JspcTest object | * Gets the OutputFile attribute of the JspcTest object | ||||
* | * | ||||
@@ -201,5 +221,48 @@ public class JspcTest extends BuildFileTest { | |||||
protected File getOutputFile(String subpath) { | protected File getOutputFile(String subpath) { | ||||
return new File(outDir, subpath); | return new File(outDir, subpath); | ||||
} | } | ||||
/** | |||||
* verify that we select the appropriate mangler | |||||
*/ | |||||
public void testJasperNameManglerSelection() { | |||||
JspCompilerAdapter adapter= | |||||
JspCompilerAdapterFactory.getCompiler("jasper", null,null); | |||||
JspMangler mangler=adapter.createMangler(); | |||||
assertTrue(mangler instanceof JspNameMangler); | |||||
adapter= JspCompilerAdapterFactory.getCompiler("jasper41", null, null); | |||||
mangler = adapter.createMangler(); | |||||
assertTrue(mangler instanceof Jasper41Mangler); | |||||
} | |||||
public void testJasper41() { | |||||
JspMangler mangler = new Jasper41Mangler(); | |||||
//java keywords are not special | |||||
assertMapped(mangler, "for.jsp", "for_jsp"); | |||||
//underscores go in front of invalid start chars | |||||
assertMapped(mangler, "0.jsp", "_0_jsp"); | |||||
//underscores at the front get an underscore too | |||||
assertMapped(mangler, "_.jsp", "___jsp"); | |||||
//non java char at start => underscore then the the _hex value | |||||
assertMapped(mangler, "-.jsp", "__0002d_jsp"); | |||||
//and paths are stripped | |||||
char s = File.separatorChar; | |||||
assertMapped(mangler, "" + s + s + "somewhere" + s + "file" + s + "index.jsp", "index_jsp"); | |||||
} | |||||
/** | |||||
* assert our mapping rules | |||||
* @param mangler | |||||
* @param filename | |||||
* @param classname | |||||
*/ | |||||
protected void assertMapped(JspMangler mangler, String filename, String classname) { | |||||
String mappedname = mangler.mapJspToJavaName(new File(filename)); | |||||
assertTrue(filename+" should have mapped to "+classname | |||||
+" but instead mapped to "+mappedname, | |||||
classname.equals(mappedname)); | |||||
} | |||||
} | } | ||||