1 4 5 9 10 package org.openlaszlo.compiler; 11 import org.openlaszlo.utils.FileUtils; 12 import java.io.File ; 13 import java.io.IOException ; 14 import org.jdom.Element; 15 16 20 class ScriptElementCompiler extends ElementCompiler { 21 private static final String SRC_ATTR_NAME = "src"; 22 23 ScriptElementCompiler(CompilationEnvironment env) { 24 super(env); 25 } 26 27 31 static boolean isElement(Element element) { 32 return element.getName().intern() == "script"; 33 } 34 35 public void compile(Element element) { 36 if (element.hasChildren()) { 37 throw new CompilationError("<script> elements can't have children", 38 element); 39 } 40 String pathname = null; 41 String script = element.getText(); 42 if (element.getAttribute(SRC_ATTR_NAME) != null) { 43 pathname = element.getAttributeValue(SRC_ATTR_NAME); 44 File file = mEnv.resolveReference(element, SRC_ATTR_NAME); 45 try { 46 script = "#file " + element.getAttributeValue(SRC_ATTR_NAME) 47 + "\n" + 48 "#line 1\n" + FileUtils.readFileString(file); 49 } catch (IOException e) { 50 throw new CompilationError(e); 51 } 52 } 53 54 try { 57 mEnv.compileScript( 58 CompilerUtils.sourceLocationDirective(element, true) + 60 VIEW_INSTANTIATION_FNAME + 61 "({name: 'script', attrs: " + 62 "{script: function () {\n" + 63 "#beginContent\n" + 64 "#pragma 'scriptElement'\n" + 65 CompilerUtils.sourceLocationDirective(element, true) + 66 script + 67 "\n#endContent\n" + 68 "}}}, 1)", 70 element); 71 } catch (CompilationError e) { 72 if (pathname != null) { 75 e.setPathname(pathname); 76 } 77 throw e; 78 } 79 } 80 } 81 82 83 84 85 86 87 | Popular Tags |