1 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 ; 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.util.ArrayList ; 31 32 43 public class JavascriptLanguage extends AbstractProgrammingLanguage implements ProgrammingLanguage { 44 45 public Program preload(String filename, File baseDirectory, String encoding) throws LanguageException { 46 return load(filename, baseDirectory, encoding); 47 } 48 49 public Program load(String filename, File baseDirectory, String encoding) throws LanguageException { 50 File sourceFile = new File (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 clazz = null; 67 ArrayList dependecies = new ArrayList (); 68 69 String className = null; 70 BufferedReader r = null; 71 try { 72 r = new BufferedReader (new FileReader (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 line; 82 while((line = getMeta(r.readLine(), "depends")) != null) { 83 dependecies.add(line); 84 } 85 } catch (IOException e) { 86 throw new LanguageException("Can't load program - Signature is not found: " 87 + IOUtils.getFullFilename(sourceFile)); 88 } catch (ClassNotFoundException 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 ignored) { 95 } 96 } 97 98 return new JavascriptProgram(sourceFile, clazz, dependecies); 99 } 100 101 private String getMeta(String line, String 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 program, String filename, File baseDir) 122 throws LanguageException { 123 } 125 126 public String quoteString(String constant) { 127 return XSLTExtension.escapeJavaString(constant); 128 } 129 130 135 public String getSourceExtension() { 136 return "js"; 137 } 138 } 139 | Popular Tags |