KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * ToplevelCompiler.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 java.util.*;
12 import java.io.*;
13
14 import org.jdom.Element;
15 import org.openlaszlo.compiler.ViewCompiler.*;
16 import org.openlaszlo.server.*;
17 import org.openlaszlo.utils.*;
18 import org.jdom.*;
19 import org.apache.log4j.*;
20
21 /** Compiler for <code>canvas</code> and <code>library</code> elements.
22  */

23 abstract class ToplevelCompiler extends ElementCompiler {
24     /** Logger */
25     private static Logger mLogger = Logger.getLogger(ToplevelCompiler.class);
26
27     ToplevelCompiler(CompilationEnvironment env) {
28         super(env);
29     }
30     
31     /** Returns true if the element is capable of acting as a toplevel
32      * element. This is independent of whether it's positioned as a
33      * toplevel element; CompilerUtils.isTopLevel() tests for position
34      * as well. */

35     static boolean isElement(Element element) {
36         return CanvasCompiler.isElement(element)
37             || LibraryCompiler.isElement(element)
38             || SwitchCompiler.isElement(element);
39     }
40     
41     public void compile(Element element) {
42         for (Iterator iter = element.getChildren().iterator();
43              iter.hasNext(); ) {
44             Element child = (Element) iter.next();
45             if (!NodeModel.isPropertyElement(child)) {
46                 Compiler.compileElement(child, mEnv);
47             }
48         }
49     }
50     
51     /** Parses out user class definitions.
52      *
53      * <p>
54      * Iterates the direct children of the top level of the DOM tree and
55      * look for elements named "class", find the "name" and "extends"
56      * attributes, and enter them in the ViewSchema.
57      *
58      * @param visited {canonical filenames} for libraries whose
59      * schemas have been visited; used to prevent recursive
60      * processing.
61      */

62     void updateSchema(Element element, ViewSchema schema, Set visited) {
63         Iterator iterator = element.getChildren().iterator();
64         while (iterator.hasNext()) {
65             Element child = (Element) iterator.next();
66             if (!NodeModel.isPropertyElement(child)) {
67                 Compiler.updateSchema(child, mEnv, schema, visited);
68             }
69         }
70     }
71
72
73     static void collectReferences(CompilationEnvironment env,
74                                   Element element, Set defined,
75                                   Set referenced)
76     {
77         Set visited = new HashSet();
78         ViewCompiler.collectLayoutElement(element, referenced);
79         collectReferences(env, element, defined, referenced, visited);
80     }
81
82     /** This also collects "attribute", "method", and HTML element
83      * names, but that's okay since none of them has an autoinclude
84      * entry.
85      */

86     static void collectReferences(CompilationEnvironment env,
87                                   Element element, Set defined,
88                                   Set referenced, Set libsVisited) {
89         ElementCompiler compiler = Compiler.getElementCompiler(element, env);
90         if (compiler instanceof ToplevelCompiler) {
91             if (compiler instanceof LibraryCompiler || compiler instanceof ImportCompiler ) {
92                 Element library = LibraryCompiler.resolveLibraryElement(element, env, libsVisited, false);
93                 if (library != null) {
94                     element = library;
95                 }
96             }
97
98             for (Iterator iter = element.getChildren().iterator();
99                  iter.hasNext(); ) {
100                 collectReferences(env, (Element) iter.next(), defined, referenced,
101                                   libsVisited);
102             }
103         } else if (compiler instanceof ClassCompiler) {
104             String JavaDoc name = element.getAttributeValue("name");
105             if (name != null) {
106                 defined.add(name);
107             }
108             String JavaDoc superclass = element.getAttributeValue("extends");
109             if (superclass != null) {
110                 referenced.add(superclass);
111             }
112             ViewCompiler.collectElementNames(element, referenced);
113         } else if (compiler instanceof ViewCompiler) {
114             ViewCompiler.collectElementNames(element, referenced);
115         }
116     }
117
118
119
120     static List getLibraries(CompilationEnvironment env, Element element, Map explanations) {
121         List libraryNames = new ArrayList();
122         String JavaDoc librariesAttr = element.getAttributeValue("libraries");
123         if (librariesAttr != null) {
124             for (StringTokenizer st = new StringTokenizer(librariesAttr);
125                  st.hasMoreTokens();) {
126                 String JavaDoc name = (String JavaDoc) st.nextToken();
127                 libraryNames.add(name);
128             }
129         }
130         // figure out which tags are referenced but not defined, and
131
// look up their libraries in the autoincludes file
132
{
133             Set defined = new HashSet();
134             Set referenced = new HashSet();
135             collectReferences(env, element, defined, referenced);
136             referenced.removeAll(defined);
137             // keep the keys sorted so the order is deterministic for qa
138
Set additionalLibraries = new TreeSet();
139             // iterate undefined references
140
Map autoincludes = env.getSchema().sAutoincludes;
141             for (Iterator iter = referenced.iterator(); iter.hasNext(); ) {
142                 String JavaDoc key = (String JavaDoc) iter.next();
143                 if (autoincludes.containsKey(key)) {
144                     String JavaDoc value = (String JavaDoc) autoincludes.get(key);
145                     additionalLibraries.add(value);
146                     explanations.put(value, "reference to <" + key + "> tag");
147                 }
148             }
149             libraryNames.addAll(additionalLibraries);
150         }
151         // Turn the library names into pathnames
152
List libraries = new ArrayList();
153         String JavaDoc base = new File(Parser.getSourcePathname(element)).getParent();
154         for (Iterator iter = libraryNames.iterator(); iter.hasNext(); ) {
155             String JavaDoc name = (String JavaDoc) iter.next();
156             try {
157                 libraries.add(env.getFileResolver().resolve(name, base));
158             } catch (FileNotFoundException e) {
159                 throw new CompilationError(element, e);
160             }
161         }
162         
163         // add the debugger, if canvas debug=true
164
if (env.getBooleanProperty(env.DEBUG_PROPERTY)) {
165             explanations.put("debugger", "the canvas debug attribute is true");
166             String JavaDoc pathname = LPS.getComponentsDirectory() +
167                 File.separator + "debugger" +
168                 File.separator + "debugger.lzx";
169             libraries.add(new File(pathname));
170         }
171         return libraries;
172     }
173     
174     List getLibraries(Element element) {
175         return getLibraries(mEnv, element, new HashMap());
176     }
177
178
179     static String JavaDoc getBaseLibraryName (CompilationEnvironment env) {
180         // returns 5 or 6; coerce to string
181
String JavaDoc swfversion = "" + env.getSWFVersionInt();
182
183         // Load the appropriate LFC Library according to debug,
184
// profile, or krank
185

186         // We will now have LFC library with swf version encoded after
187
// the base name like:
188
// LFC6.lzl
189
// LFC5-debug.lzl
190
// etc.
191
String JavaDoc ext = swfversion;
192         
193         // krank trumps profiling
194
ext += env.getBooleanProperty(env.KRANK_PROPERTY)?"-krank":
195             (env.getBooleanProperty(env.PROFILE_PROPERTY)?"-profile":"");
196         ext += env.getBooleanProperty(env.DEBUG_PROPERTY)?"-debug":"";
197         return "LFC" + ext + ".lzl";
198     }
199
200     static void handleAutoincludes(CompilationEnvironment env, Element element) {
201         // import required libraries, and collect explanations as to
202
// why they were required
203
Canvas canvas = env.getCanvas();
204
205         String JavaDoc baseLibraryName = getBaseLibraryName(env);
206         String JavaDoc baseLibraryBecause = "Required for all applications";
207
208         Map explanations = new HashMap();
209         for (Iterator iter = getLibraries(env, element, explanations).iterator();
210              iter.hasNext(); ) {
211             File file = (File) iter.next();
212             Compiler.importLibrary(file, env);
213         }
214         
215         // canvas info += <include name= explanation= [size=]/> for LFC
216
Element info = new Element("include");
217         info.setAttribute("name", baseLibraryName);
218         info.setAttribute("explanation", baseLibraryBecause);
219         try {
220             info.setAttribute("size", "" +
221                               FileUtils.getSize(env.resolve(baseLibraryName, "")));
222         } catch (Exception JavaDoc e) {
223             mLogger.error("exception getting library size", e);
224         }
225         canvas.addInfo(info);
226         
227         // canvas info += <include name= explanation=/> for each library
228
for (Iterator iter = explanations.entrySet().iterator();
229              iter.hasNext(); ) {
230             Map.Entry entry = (Map.Entry) iter.next();
231             info = new Element("include");
232             info.setAttribute("name", entry.getKey().toString());
233             info.setAttribute("explanation", entry.getValue().toString());
234             canvas.addInfo(info);
235         }
236     }
237
238 }
239
Popular Tags