KickJava   Java API By Example, From Geeks To Geeks.

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


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.markup.xsp.XSLTExtension;
20 import org.apache.cocoon.components.language.programming.AbstractProgrammingLanguage;
21 import org.apache.cocoon.components.language.programming.Program;
22 import org.apache.cocoon.components.language.programming.ProgrammingLanguage;
23 import org.apache.cocoon.util.ClassUtils;
24 import org.apache.cocoon.util.IOUtils;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 /**
33  * The interpreted Javascript programming language.
34  * Program in Javascript must have comment line as first line of file:
35  * <pre>
36  * // $Cocoon extends: org.apache.cocoon.components.language.xsp.JSGenerator$
37  * </pre>
38  * The class specified will be used as a Java wrapper interpreting javascript program.
39  *
40  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
41  * @version CVS $Id: JavascriptLanguage.java 30932 2004-07-29 17:35:38Z vgritsenko $
42  */

43 public class JavascriptLanguage extends AbstractProgrammingLanguage implements ProgrammingLanguage {
44
45     public Program preload(String JavaDoc filename, File JavaDoc baseDirectory, String JavaDoc encoding) throws LanguageException {
46         return load(filename, baseDirectory, encoding);
47     }
48
49     public Program load(String JavaDoc filename, File JavaDoc baseDirectory, String JavaDoc encoding) throws LanguageException {
50         // Does source file exist?
51
File JavaDoc sourceFile = new File JavaDoc(baseDirectory,
52                 filename + "." + this.getSourceExtension());
53         if (!sourceFile.exists()) {
54             throw new LanguageException("Can't load program - File doesn't exist: "
55                     + IOUtils.getFullFilename(sourceFile));
56         }
57         if (!sourceFile.isFile()) {
58             throw new LanguageException("Can't load program - File is not a normal file: "
59                     + IOUtils.getFullFilename(sourceFile));
60         }
61         if (!sourceFile.canRead()) {
62             throw new LanguageException("Can't load program - File cannot be read: "
63                     + IOUtils.getFullFilename(sourceFile));
64         }
65
66         Class JavaDoc clazz = null;
67         ArrayList JavaDoc dependecies = new ArrayList JavaDoc();
68
69         String JavaDoc className = null;
70         BufferedReader JavaDoc r = null;
71         try {
72             r = new BufferedReader JavaDoc(new FileReader JavaDoc(sourceFile));
73             className = getMeta(r.readLine(), "extends");
74             if (className == null) {
75                 throw new LanguageException("Can't load program - Signature is not found: "
76                         + IOUtils.getFullFilename(sourceFile));
77             }
78
79             clazz = ClassUtils.loadClass(className);
80
81             String JavaDoc line;
82             while((line = getMeta(r.readLine(), "depends")) != null) {
83                 dependecies.add(line);
84             }
85         } catch (IOException JavaDoc e) {
86             throw new LanguageException("Can't load program - Signature is not found: "
87                     + IOUtils.getFullFilename(sourceFile));
88         } catch (ClassNotFoundException JavaDoc e) {
89             throw new LanguageException("Can't load program - Base class " + className + " is not found: "
90                     + IOUtils.getFullFilename(sourceFile));
91         } finally {
92             if (r != null) try {
93                 r.close();
94             } catch (IOException JavaDoc ignored) {
95             }
96         }
97
98         return new JavascriptProgram(sourceFile, clazz, dependecies);
99     }
100
101     private String JavaDoc getMeta(String JavaDoc line, String JavaDoc meta) {
102         if (line == null) {
103             return null;
104         }
105
106         meta = "$Cocoon " + meta + ": ";
107         int i = line.indexOf(meta);
108         if (i != -1) {
109             int j = line.indexOf("$", i + 1);
110             if (j != -1) {
111                 line = line.substring(i + meta.length(), j);
112             } else {
113                 line = null;
114             }
115         } else {
116             line = null;
117         }
118         return line;
119     }
120
121     protected void doUnload(Object JavaDoc program, String JavaDoc filename, File JavaDoc baseDir)
122             throws LanguageException {
123         // Do nothing. Source is already deleted by the AbstractProgrammingLanguage.
124
}
125
126     public String JavaDoc quoteString(String JavaDoc constant) {
127         return XSLTExtension.escapeJavaString(constant);
128     }
129
130     /**
131      * Return the language's canonical source file extension.
132      *
133      * @return The source file extension
134      */

135     public String JavaDoc getSourceExtension() {
136         return "js";
137     }
138 }
139
Popular Tags