1 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 ; 27 import java.io.BufferedReader ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.io.InputStreamReader ; 31 import java.io.FileInputStream ; 32 import java.util.ArrayList ; 33 34 45 public class PythonLanguage extends AbstractProgrammingLanguage implements ProgrammingLanguage { 46 47 public Program preload(String filename, File baseDirectory, String encoding) throws LanguageException { 48 return load(filename, baseDirectory, encoding); 49 } 50 51 public Program load(String filename, File baseDirectory, String encoding) throws LanguageException { 52 File sourceFile = new File (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 clazz = null; 69 ArrayList dependecies = new ArrayList (); 70 71 String className = null; 72 BufferedReader r = null; 73 try { 74 r = new BufferedReader ( 75 (encoding == null)? 76 new FileReader (sourceFile): 77 new InputStreamReader (new FileInputStream (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 line; 87 while((line = getMeta(r.readLine(), "depends")) != null) { 88 dependecies.add(line); 89 } 90 } catch (IOException e) { 91 throw new LanguageException("Can't load program - Signature is not found: " 92 + IOUtils.getFullFilename(sourceFile)); 93 } catch (ClassNotFoundException 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 ignored) { 100 } 101 } 102 103 return new PythonProgram(sourceFile, clazz, dependecies); 104 } 105 106 private String getMeta(String line, String 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 program, String filename, File baseDir) 127 throws LanguageException { 128 } 130 131 public String quoteString(String constant) { 132 return XSLTExtension.escapeString(constant); 133 } 134 135 140 public String getSourceExtension() { 141 return "py"; 142 } 143 } 144 | Popular Tags |