KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > programming > AbstractProgrammingLanguage


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

16 package org.apache.cocoon.components.language.programming;
17
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.logger.LogEnabled;
20 import org.apache.avalon.framework.parameters.Parameterizable;
21 import org.apache.avalon.framework.parameters.Parameters;
22 import org.apache.avalon.framework.parameters.ParameterException;
23
24 import org.apache.cocoon.components.language.LanguageException;
25 import org.apache.cocoon.components.language.generator.CompiledComponent;
26 import org.apache.cocoon.util.ClassUtils;
27
28 import java.io.File JavaDoc;
29
30 /**
31  * Base implementation of <code>ProgrammingLanguage</code>. This class sets the
32  * <code>CodeFormatter</code> instance and deletes source program files after
33  * unloading.
34  *
35  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
36  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
37  * @version CVS $Id: AbstractProgrammingLanguage.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public abstract class AbstractProgrammingLanguage extends AbstractLogEnabled
40         implements ProgrammingLanguage, Parameterizable {
41
42     /** The source code formatter */
43     protected Class JavaDoc codeFormatter;
44
45     protected String JavaDoc languageName;
46
47     /**
48      * Set the configuration parameters. This method instantiates the
49      * sitemap-specified source code formatter
50      *
51      * @param params The configuration parameters
52      * @exception ParameterException If the language compiler cannot be loaded
53      */

54     public void parameterize(Parameters params) throws ParameterException {
55         String JavaDoc className = params.getParameter("code-formatter", null);
56
57         try {
58             if (className != null) {
59                 this.codeFormatter = ClassUtils.loadClass(className);
60             }
61         } catch (Exception JavaDoc e) {
62             getLogger().error("Error with \"code-formatter\" parameter", e);
63             throw new ParameterException("Unable to load code formatter: " + className, e);
64         }
65     }
66
67     /**
68      * Return this language's source code formatter. A new formatter instance is
69      * created on each invocation.
70      *
71      * @return The language source code formatter
72      */

73     public CodeFormatter getCodeFormatter() {
74         if (this.codeFormatter != null) {
75             try {
76                 CodeFormatter formatter = (CodeFormatter) this.codeFormatter.newInstance();
77                 if (formatter instanceof LogEnabled) {
78                     ((LogEnabled) formatter).enableLogging(this.getLogger());
79                 }
80                 return formatter;
81             } catch (Exception JavaDoc e) {
82                 getLogger().error("Error instantiating CodeFormatter", e);
83             }
84         }
85
86         return null;
87     }
88
89     /**
90      * Unload a previously loaded program
91      *
92      * @param program A previously loaded object program
93      * @exception LanguageException If an error occurs during unloading
94      */

95     protected abstract void doUnload(Object JavaDoc program, String JavaDoc filename,
96                                      File JavaDoc baseDirectory)
97             throws LanguageException;
98
99     public final void unload(Object JavaDoc program, String JavaDoc filename, File JavaDoc baseDirectory)
100             throws LanguageException {
101
102         File JavaDoc file = new File JavaDoc(baseDirectory,
103                              filename + "." + getSourceExtension());
104         file.delete();
105         this.doUnload(program, filename, baseDirectory);
106     }
107
108     public final void setLanguageName(String JavaDoc name) {
109         this.languageName = name;
110     }
111
112     public final String JavaDoc getLanguageName() {
113         return this.languageName;
114     }
115
116     /**
117      * Create a new instance for the given class
118      *
119      * @param program The Java class
120      * @return A new class instance
121      * @exception LanguageException If an instantiation error occurs
122      */

123     public CompiledComponent instantiate(Program program) throws LanguageException {
124         try {
125             return program.newInstance();
126         } catch (Exception JavaDoc e) {
127             getLogger().warn("Could not instantiate program instance", e);
128             throw new LanguageException("Could not instantiate program instance due to a " + e.getClass().getName() + ": " + e.getMessage());
129         }
130     }
131 }
132
Popular Tags