KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.taskdefs.optional.jsp.compilers;
19
20 import org.apache.tools.ant.AntClassLoader;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Task;
23 import org.apache.tools.ant.taskdefs.optional.jsp.JspNameMangler;
24 import org.apache.tools.ant.taskdefs.optional.jsp.Jasper41Mangler;
25
26
27 /**
28  * Creates the necessary compiler adapter, given basic criteria.
29  *
30  */

31 public final class JspCompilerAdapterFactory {
32
33     /** This is a singleton -- can't create instances!! */
34     private JspCompilerAdapterFactory() {
35     }
36
37     /**
38      * Based on the parameter passed in, this method creates the necessary
39      * factory desired.
40      *
41      * The current mapping for compiler names are as follows:
42      * <ul><li>jasper = jasper compiler (the default)
43      * <li><i>a fully quallified classname</i> = the name of a jsp compiler
44      * adapter
45      * </ul>
46      *
47      * @param compilerType either the name of the desired compiler, or the
48      * full classname of the compiler's adapter.
49      * @param task a task to log through.
50      * @return the compiler
51      * @throws BuildException if the compiler type could not be resolved into
52      * a compiler adapter.
53      */

54     public static JspCompilerAdapter getCompiler(String JavaDoc compilerType, Task task)
55         throws BuildException {
56         return getCompiler(compilerType, task,
57                            task.getProject().createClassLoader(null));
58     }
59
60     /**
61      * Based on the parameter passed in, this method creates the necessary
62      * factory desired.
63      *
64      * The current mapping for compiler names are as follows:
65      * <ul><li>jasper = jasper compiler (the default)
66      * <li><i>a fully quallified classname</i> = the name of a jsp compiler
67      * adapter
68      * </ul>
69      *
70      * @param compilerType either the name of the desired compiler, or the
71      * full classname of the compiler's adapter.
72      * @param task a task to log through.
73      * @param loader AntClassLoader with which the compiler should be loaded
74      * @return the compiler
75      * @throws BuildException if the compiler type could not be resolved into
76      * a compiler adapter.
77      */

78     public static JspCompilerAdapter getCompiler(String JavaDoc compilerType, Task task,
79                                                  AntClassLoader loader)
80         throws BuildException {
81
82         if (compilerType.equalsIgnoreCase("jasper")) {
83             //tomcat4.0 gets the old mangler
84
return new JasperC(new JspNameMangler());
85         }
86         if (compilerType.equalsIgnoreCase("jasper41")) {
87             //tomcat4.1 gets the new one
88
return new JasperC(new Jasper41Mangler());
89         }
90         return resolveClassName(compilerType, loader);
91     }
92
93     /**
94      * Tries to resolve the given classname into a compiler adapter.
95      * Throws a fit if it can't.
96      *
97      * @param className The fully qualified classname to be created.
98      * @param classloader Classloader with which to load the class
99      * @throws BuildException This is the fit that is thrown if className
100      * isn't an instance of JspCompilerAdapter.
101      */

102     private static JspCompilerAdapter resolveClassName(String JavaDoc className,
103                                                        AntClassLoader classloader)
104         throws BuildException {
105         try {
106             Class JavaDoc c = classloader.findClass(className);
107             Object JavaDoc o = c.newInstance();
108             return (JspCompilerAdapter) o;
109         } catch (ClassNotFoundException JavaDoc cnfe) {
110             throw new BuildException(className + " can\'t be found.", cnfe);
111         } catch (ClassCastException JavaDoc cce) {
112             throw new BuildException(className + " isn\'t the classname of "
113                                      + "a compiler adapter.", cce);
114         } catch (Throwable JavaDoc t) {
115             // for all other possibilities
116
throw new BuildException(className + " caused an interesting "
117                                      + "exception.", t);
118         }
119     }
120
121 }
122
Popular Tags