KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > programming > javascript > CompiledJavascriptLanguage


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.javascript;
17
18 import org.apache.cocoon.components.language.LanguageException;
19 import org.apache.cocoon.components.language.programming.java.JavaLanguage;
20
21 import org.mozilla.javascript.tools.jsc.Main;
22
23 import java.io.File JavaDoc;
24
25 /**
26  * The compiled Javascript (Rhino) programming language processor
27  *
28  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
29  * @version CVS $Id: CompiledJavascriptLanguage.java 30932 2004-07-29 17:35:38Z vgritsenko $
30  */

31 public class CompiledJavascriptLanguage extends JavaLanguage {
32
33     /**
34      * Return the language's canonical source file extension.
35      *
36      * @return The source file extension
37      */

38     public String JavaDoc getSourceExtension() {
39         return "js";
40     }
41
42     /**
43      * Compile a source file yielding a loadable class file.
44      *
45      * @param name The object program base file name
46      * @param baseDirectory The directory containing the object program file
47      * @param encoding The encoding expected in the source file or
48      * <code>null</code> if it is the platform's default encoding
49      * @exception LanguageException If an error occurs during compilation
50      */

51     protected void compile(
52             String JavaDoc name, File JavaDoc baseDirectory, String JavaDoc encoding
53             ) throws LanguageException {
54         try {
55             int pos = name.lastIndexOf(File.separatorChar);
56             String JavaDoc filename = name.substring(pos + 1);
57             String JavaDoc pathname =
58                     baseDirectory.getCanonicalPath() + File.separator +
59                     name.substring(0, pos).replace(File.separatorChar, '/');
60             String JavaDoc packageName =
61                     name.substring(0, pos).replace(File.separatorChar, '.');
62
63             String JavaDoc[] args = {
64                 "-extends",
65                 "org.apache.cocoon.components.language.markup.xsp.JSGenerator",
66                 "-nosource",
67                 "-O", "9",
68                 "-package", packageName,
69                 "-o", filename + ".class",
70                 pathname + File.separator + filename + "." + this.getSourceExtension()
71             };
72
73             Main.main(args);
74         } catch (Exception JavaDoc e) {
75             getLogger().warn("JavascriptLanguage.compile", e);
76             throw new LanguageException(e.getMessage());
77         }
78     }
79 }
80
Popular Tags