001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.programming.python;
018:
019: import org.apache.cocoon.components.language.programming.AbstractProgrammingLanguage;
020: import org.apache.cocoon.components.language.programming.ProgrammingLanguage;
021: import org.apache.cocoon.components.language.programming.Program;
022: import org.apache.cocoon.components.language.LanguageException;
023: import org.apache.cocoon.components.language.markup.xsp.XSLTExtension;
024: import org.apache.cocoon.util.IOUtils;
025: import org.apache.cocoon.util.ClassUtils;
026:
027: import java.io.File;
028: import java.io.BufferedReader;
029: import java.io.FileReader;
030: import java.io.IOException;
031: import java.io.InputStreamReader;
032: import java.io.FileInputStream;
033: import java.util.ArrayList;
034:
035: /**
036: * The interpreted Python programming language.
037: * Program in Python must have comment line as first line of file:
038: * <pre>
039: * """ $Cocoon extends: org.apache.cocoon.components.language.xsp.JSGenerator$ """
040: * </pre>
041: * The class specified will be used as a Java wrapper interpreting javascript program.
042: *
043: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
044: * @version CVS $Id: PythonLanguage.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public class PythonLanguage extends AbstractProgrammingLanguage
047: implements ProgrammingLanguage {
048:
049: public Program preload(String filename, File baseDirectory,
050: String encoding) throws LanguageException {
051: return load(filename, baseDirectory, encoding);
052: }
053:
054: public Program load(String filename, File baseDirectory,
055: String encoding) throws LanguageException {
056: // Does source file exist?
057: File sourceFile = new File(baseDirectory, filename + "."
058: + this .getSourceExtension());
059: if (!sourceFile.exists()) {
060: throw new LanguageException(
061: "Can't load program - File doesn't exist: "
062: + IOUtils.getFullFilename(sourceFile));
063: }
064: if (!sourceFile.isFile()) {
065: throw new LanguageException(
066: "Can't load program - File is not a normal file: "
067: + IOUtils.getFullFilename(sourceFile));
068: }
069: if (!sourceFile.canRead()) {
070: throw new LanguageException(
071: "Can't load program - File cannot be read: "
072: + IOUtils.getFullFilename(sourceFile));
073: }
074:
075: Class clazz = null;
076: ArrayList dependecies = new ArrayList();
077:
078: String className = null;
079: BufferedReader r = null;
080: try {
081: r = new BufferedReader((encoding == null) ? new FileReader(
082: sourceFile) : new InputStreamReader(
083: new FileInputStream(sourceFile), encoding));
084: className = getMeta(r.readLine(), "extends");
085: if (className == null) {
086: throw new LanguageException(
087: "Can't load program - Signature is not found: "
088: + IOUtils.getFullFilename(sourceFile));
089: }
090:
091: clazz = ClassUtils.loadClass(className);
092:
093: String line;
094: while ((line = getMeta(r.readLine(), "depends")) != null) {
095: dependecies.add(line);
096: }
097: } catch (IOException e) {
098: throw new LanguageException(
099: "Can't load program - Signature is not found: "
100: + IOUtils.getFullFilename(sourceFile));
101: } catch (ClassNotFoundException e) {
102: throw new LanguageException(
103: "Can't load program - Base class " + className
104: + " is not found: "
105: + IOUtils.getFullFilename(sourceFile));
106: } finally {
107: if (r != null)
108: try {
109: r.close();
110: } catch (IOException ignored) {
111: }
112: }
113:
114: return new PythonProgram(sourceFile, clazz, dependecies);
115: }
116:
117: private String getMeta(String line, String meta) {
118: if (line == null) {
119: return null;
120: }
121:
122: meta = "$Cocoon " + meta + ": ";
123: int i = line.indexOf(meta);
124: if (i != -1) {
125: int j = line.indexOf("$", i + 1);
126: if (j != -1) {
127: line = line.substring(i + meta.length(), j);
128: } else {
129: line = null;
130: }
131: } else {
132: line = null;
133: }
134: return line;
135: }
136:
137: protected void doUnload(Object program, String filename,
138: File baseDir) throws LanguageException {
139: // Do nothing. Source is already deleted by the AbstractProgrammingLanguage.
140: }
141:
142: public String quoteString(String constant) {
143: return XSLTExtension.escapeString(constant);
144: }
145:
146: /**
147: * Return the language's canonical source file extension.
148: *
149: * @return The source file extension
150: */
151: public String getSourceExtension() {
152: return "py";
153: }
154: }
|