01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.cocoon.components.language.programming.javascript;
18:
19: import org.apache.cocoon.components.language.LanguageException;
20: import org.apache.cocoon.components.language.programming.java.JavaLanguage;
21:
22: import org.mozilla.javascript.tools.jsc.Main;
23:
24: import java.io.File;
25:
26: /**
27: * The compiled Javascript (Rhino) programming language processor
28: *
29: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
30: * @version CVS $Id: CompiledJavascriptLanguage.java 433543 2006-08-22 06:22:54Z crossley $
31: */
32: public class CompiledJavascriptLanguage extends JavaLanguage {
33:
34: /**
35: * Return the language's canonical source file extension.
36: *
37: * @return The source file extension
38: */
39: public String getSourceExtension() {
40: return "js";
41: }
42:
43: /**
44: * Compile a source file yielding a loadable class file.
45: *
46: * @param name The object program base file name
47: * @param baseDirectory The directory containing the object program file
48: * @param encoding The encoding expected in the source file or
49: * <code>null</code> if it is the platform's default encoding
50: * @exception LanguageException If an error occurs during compilation
51: */
52: protected void compile(String name, File baseDirectory,
53: String encoding) throws LanguageException {
54: try {
55: int pos = name.lastIndexOf(File.separatorChar);
56: String filename = name.substring(pos + 1);
57: String pathname = baseDirectory.getCanonicalPath()
58: + File.separator
59: + name.substring(0, pos).replace(
60: File.separatorChar, '/');
61: String packageName = name.substring(0, pos).replace(
62: File.separatorChar, '.');
63:
64: String[] args = {
65: "-extends",
66: "org.apache.cocoon.components.language.markup.xsp.JSGenerator",
67: "-nosource",
68: "-O",
69: "9",
70: "-package",
71: packageName,
72: "-o",
73: filename + ".class",
74: pathname + File.separator + filename + "."
75: + this .getSourceExtension() };
76:
77: Main.main(args);
78: } catch (Exception e) {
79: getLogger().warn("JavascriptLanguage.compile", e);
80: throw new LanguageException(e.getMessage());
81: }
82: }
83: }
|