KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jsp > compilers > JasperC


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.jsp.compilers;
20
21 import java.io.File JavaDoc;
22 import org.apache.tools.ant.AntClassLoader;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.taskdefs.Java;
26 import org.apache.tools.ant.taskdefs.optional.jsp.JspC;
27 import org.apache.tools.ant.taskdefs.optional.jsp.JspMangler;
28 import org.apache.tools.ant.types.CommandlineJava;
29 import org.apache.tools.ant.types.Path;
30
31 /**
32  * The implementation of the jasper compiler.
33  * This is a cut-and-paste of the original Jspc task.
34  *
35  * @since ant1.5
36  */

37 public class JasperC extends DefaultJspCompilerAdapter {
38
39     // CheckStyle:VisibilityModifier OFF - bc
40

41     /**
42      * what produces java classes from .jsp files
43      */

44     JspMangler mangler;
45
46     // CheckStyle:VisibilityModifier ON
47

48     /**
49      * Constructor for JasperC.
50      * @param mangler a filename converter
51      */

52     public JasperC(JspMangler mangler) {
53         this.mangler = mangler;
54     }
55
56     /**
57      * Our execute method.
58      * @return true if successful
59      * @throws BuildException on error
60      */

61     public boolean execute()
62         throws BuildException {
63         getJspc().log("Using jasper compiler", Project.MSG_VERBOSE);
64         CommandlineJava cmd = setupJasperCommand();
65
66         try {
67             // Create an instance of the compiler, redirecting output to
68
// the project log
69
Java java = new Java(owner);
70             Path p = getClasspath();
71             if (getJspc().getClasspath() != null) {
72                 getProject().log("using user supplied classpath: " + p,
73                                  Project.MSG_DEBUG);
74             } else {
75                 getProject().log("using system classpath: " + p,
76                                  Project.MSG_DEBUG);
77             }
78             java.setClasspath(p);
79             java.setDir(getProject().getBaseDir());
80             java.setClassname("org.apache.jasper.JspC");
81             //this is really irritating; we need a way to set stuff
82
String JavaDoc []args = cmd.getJavaCommand().getArguments();
83             for (int i = 0; i < args.length; i++) {
84                 java.createArg().setValue(args[i]);
85             }
86             java.setFailonerror(getJspc().getFailonerror());
87             //we are forking here to be sure that if JspC calls
88
//System.exit() it doesn't halt the build
89
java.setFork(true);
90             java.setTaskName("jasperc");
91             java.execute();
92             return true;
93         } catch (Exception JavaDoc ex) {
94             if (ex instanceof BuildException) {
95                 throw (BuildException) ex;
96             } else {
97                 throw new BuildException("Error running jsp compiler: ",
98                                          ex, getJspc().getLocation());
99             }
100         } finally {
101             getJspc().deleteEmptyJavaFiles();
102         }
103     }
104
105
106
107     /**
108      * build up a command line
109      * @return a command line for jasper
110      */

111     private CommandlineJava setupJasperCommand() {
112         CommandlineJava cmd = new CommandlineJava();
113         JspC jspc = getJspc();
114         addArg(cmd, "-d", jspc.getDestdir());
115         addArg(cmd, "-p", jspc.getPackage());
116
117         if (!isTomcat5x()) {
118             addArg(cmd, "-v" + jspc.getVerbose());
119         } else {
120             getProject().log("this task doesn't support Tomcat 5.x properly, "
121                              + "please use the Tomcat provided jspc task "
122                              + "instead");
123         }
124
125         addArg(cmd, "-uriroot", jspc.getUriroot());
126         addArg(cmd, "-uribase", jspc.getUribase());
127         addArg(cmd, "-ieplugin", jspc.getIeplugin());
128         addArg(cmd, "-webinc", jspc.getWebinc());
129         addArg(cmd, "-webxml", jspc.getWebxml());
130         addArg(cmd, "-die9");
131
132         if (jspc.isMapped()) {
133             addArg(cmd, "-mapped");
134         }
135         if (jspc.getWebApp() != null) {
136             File JavaDoc dir = jspc.getWebApp().getDirectory();
137             addArg(cmd, "-webapp", dir);
138         }
139         logAndAddFilesToCompile(getJspc(), getJspc().getCompileList(), cmd);
140         return cmd;
141     }
142
143     /**
144      * @return an instance of the mangler this compiler uses
145      */

146
147     public JspMangler createMangler() {
148         return mangler;
149     }
150
151     /**
152      * @since Ant 1.6.2
153      */

154     private Path getClasspath() {
155         Path p = getJspc().getClasspath();
156         if (p == null) {
157             p = new Path(getProject());
158             return p.concatSystemClasspath("only");
159         } else {
160             return p.concatSystemClasspath("ignore");
161         }
162     }
163
164     /**
165      * @since Ant 1.6.2
166      */

167     private boolean isTomcat5x() {
168         AntClassLoader l = null;
169         try {
170             l = getProject().createClassLoader(getClasspath());
171             l.loadClass("org.apache.jasper.tagplugins.jstl.If");
172             return true;
173         } catch (ClassNotFoundException JavaDoc e) {
174             return false;
175         } finally {
176             if (l != null) {
177                 l.cleanup();
178             }
179         }
180     }
181 }
182
Popular Tags