KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > ScriptElementCompiler


1 /* *****************************************************************************
2  * ScriptElementCompiler.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.compiler;
11 import org.openlaszlo.utils.FileUtils;
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import org.jdom.Element;
15
16 /** Compiler for <code>script</code> elements.
17  *
18  * @author Oliver Steele
19  */

20 class ScriptElementCompiler extends ElementCompiler {
21     private static final String JavaDoc SRC_ATTR_NAME = "src";
22
23     ScriptElementCompiler(CompilationEnvironment env) {
24         super(env);
25     }
26
27     /** Returns true iff this class applies to this element.
28      * @param element an element
29      * @return see doc
30      */

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 JavaDoc pathname = null;
41         String JavaDoc script = element.getText();
42         if (element.getAttribute(SRC_ATTR_NAME) != null) {
43             pathname = element.getAttributeValue(SRC_ATTR_NAME);
44             File JavaDoc 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 JavaDoc e) {
50                 throw new CompilationError(e);
51             }
52         }
53         
54         // Compile scripts to run at construction time in the view
55
// instantiation queue.
56
try {
57             mEnv.compileScript(
58                 // Provide file info for anonymous function name
59
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                 // Scripts have no children
69
"}}}, 1)",
70                 element);
71         } catch (CompilationError e) {
72             // TODO: [2003-01-16] Instead of this, put the filename in ParseException,
73
// and modify CompilationError.initElement to copy it from there.
74
if (pathname != null) {
75                 e.setPathname(pathname);
76             }
77             throw e;
78         }
79     }
80 }
81
82
83
84
85
86
87
Popular Tags