KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > programming > python > PythonLanguage


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

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

140     public String JavaDoc getSourceExtension() {
141         return "py";
142     }
143 }
144
Popular Tags