KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * CanvasCompiler.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
12 import java.io.*;
13 import java.util.*;
14
15 import org.openlaszlo.compiler.ViewCompiler.*;
16 import org.openlaszlo.compiler.ViewSchema.ColorFormatException;
17 import org.openlaszlo.sc.*;
18 import org.openlaszlo.server.*;
19 import org.openlaszlo.utils.*;
20 import org.jdom.*;
21 import org.apache.log4j.*;
22
23 /** Compiler for the <code>canvas</code> element. */
24 class CanvasCompiler extends ToplevelCompiler {
25     /** Logger */
26     private static Logger mLogger = Logger.getLogger(CanvasCompiler.class);
27
28     CanvasCompiler(CompilationEnvironment env) {
29         super(env);
30     }
31     
32     static boolean isElement(Element element) {
33         return element.getName().equals("canvas");
34     }
35     
36     // Apps are proxied by default.
37
public static boolean APP_PROXIED_DEFAULT = true;
38
39     public void compile(Element element) throws CompilationError
40     {
41         Canvas canvas = new Canvas();
42         // query arg
43
String JavaDoc lzproxied = mEnv.getProperty(mEnv.PROXIED_PROPERTY);
44         // canvas attribute
45
String JavaDoc cproxied = element.getAttributeValue("proxied");
46
47         // Set the "proxied" flag for this app.
48
// canvas attribute overrides passed in arg, warn for conflict
49
if (cproxied != null && !cproxied.equals("inherit")) {
50             if (lzproxied != null && !lzproxied.equals(cproxied)) {
51                 mEnv.warn("The canvas attribute 'proxied="+cproxied +
52                           "' conflicts with the 'lzproxied="+lzproxied +
53                           "' query arg" , element);
54             }
55             canvas.setProxied(cproxied.equals("true"));
56         } else {
57             // inherit from lzproxied arg, or default to APP_PROXIED_PROPERTY
58
if (lzproxied != null) {
59                 canvas.setProxied(lzproxied.equals("true"));
60             } else {
61                 canvas.setProxied(APP_PROXIED_DEFAULT);
62             }
63         }
64
65         String JavaDoc versionNumber = element.getAttributeValue("version");
66         if (versionNumber != null) {
67             String JavaDoc msg = "The canvas is declared with version=\"" + versionNumber + "\". This version of the LPS compiles version 1.1 files. This applicaton may not behave as intended when compiled with this version of the product.";
68             if (versionNumber.equals("1.1"))
69                 ;
70             else if (versionNumber.equals("1.0"))
71                 mEnv.warn(msg + " It is recommended that you run it in debug mode and fix all compiler and debugger warnings, and that you read the migration guide in the developer documentation.", element);
72             else
73                 mEnv.warn(msg, element);
74         }
75         
76         String JavaDoc baseLibraryName = getBaseLibraryName(mEnv);
77         String JavaDoc baseLibraryBecause = "Required for all applications";
78
79         // TODO [2004-06-02]: explanation for debug attribute and request
80
// parameter
81

82         mEnv.getGenerator().importBaseLibrary(baseLibraryName, mEnv);
83         
84         canvas.setSWFVersion(mEnv.getSWFVersion());
85         initializeFromElement(canvas, element);
86         
87         // Default to true, embed fonts in swf file
88
boolean embedFonts = true;
89         String JavaDoc embed = element.getAttributeValue(CompilationEnvironment.EMBEDFONTS_PROPERTY);
90         if ("false".equals(embed)) {
91             embedFonts = false;
92         }
93         mEnv.setEmbedFonts(embedFonts);
94         
95         Map map = createCanvasObject(element, canvas);
96         String JavaDoc script;
97         try {
98             java.io.Writer JavaDoc writer = new java.io.StringWriter JavaDoc();
99             writer.write("canvas = new LzCanvas(");
100             ScriptCompiler.writeObject(map, writer);
101             writer.write(");");
102             script = writer.toString();
103         } catch (java.io.IOException JavaDoc e) {
104             throw new ChainedException(e);
105         }
106         if (mEnv.getCanvas() != null) {
107             throw new CompilationError("An application may only have one canvas tag. Check included files for a duplicate canvas tag", element);
108         }
109         
110         mEnv.setCanvas(canvas, script);
111         
112         // Compile (import) canvas's fonts first
113
for (Iterator iter =
114                  element.getChildren("font", element.getNamespace()).iterator();
115              iter.hasNext(); ) {
116             Compiler.compileElement((Element)iter.next(), mEnv);
117         }
118         
119         handleAutoincludes(mEnv, element);
120         
121         // Compile child elements
122
for (Iterator iter = element.getChildren().iterator();
123              iter.hasNext(); ) {
124             Element child = (Element) iter.next();
125             // fonts were compiled above
126
// createCanvasObject, so ignore them here
127
if (!NodeModel.isPropertyElement(child) && !FontCompiler.isElement(child)) {
128                 Compiler.compileElement(child, mEnv);
129             }
130         }
131     }
132         
133     void updateSchema(Element element, ViewSchema schema, Set visited) {
134         for (Iterator iter = getLibraries(element).iterator();
135              iter.hasNext(); ) {
136             File file = (File) iter.next();
137             Compiler.updateSchemaFromLibrary(file, mEnv, schema, visited);
138         }
139         // Visit the children:
140
super.updateSchema(element, schema, visited);
141     }
142     
143     private Map createCanvasObject(Element element, Canvas canvas) {
144         NodeModel model =
145             NodeModel.elementOnlyAsModel(element, mEnv.getSchema(), mEnv);
146         Set visited = new HashSet();
147         for (Iterator iter = getLibraries(element).iterator();
148              iter.hasNext(); ) {
149             File file = (File) iter.next();
150             Element library = LibraryCompiler.resolveLibraryElement(
151                 file, mEnv, visited, false);
152             if (library != null) {
153                 collectObjectProperties(library, model, visited);
154             }
155         }
156         collectObjectProperties(element, model, visited);
157         model.updateAttrs();
158         Map attrs = model.attrs;
159         setDimension(attrs, "width", canvas.getWidth());
160         setDimension(attrs, "height", canvas.getHeight());
161         
162         attrs.put("build", ScriptCompiler.quote(LPS.getBuild()));
163         attrs.put("lpsversion", ScriptCompiler.quote(LPS.getVersion()));
164         attrs.put("lpsrelease", ScriptCompiler.quote(LPS.getRelease()));
165         attrs.put("runtime", ScriptCompiler.quote(canvas.getSWFVersion()));
166         attrs.put("__LZproxied",
167                   ScriptCompiler.quote(
168                       mEnv.getProperty(mEnv.PROXIED_PROPERTY, APP_PROXIED_DEFAULT ? "true" : "false")));
169         
170         attrs.put("embedfonts", Boolean.toString(mEnv.getEmbedFonts()));
171         attrs.put("bgcolor", new Integer JavaDoc(canvas.getBGColor()));
172         FontInfo fontInfo = canvas.getFontInfo();
173         attrs.put("fontname", ScriptCompiler.quote(fontInfo.getName()));
174         attrs.put("fontsize", new Integer JavaDoc(fontInfo.getSize()));
175         attrs.put("fontstyle", ScriptCompiler.quote(fontInfo.getStyle()));
176         if (element.getAttribute("id") != null)
177             attrs.put("id", ScriptCompiler.quote(element.getAttributeValue("id")));
178         // Remove this so that Debug.write works in canvas methods.
179
attrs.remove("debug");
180         // Remove this since it isn't a JavaScript expression.
181
attrs.remove("libraries");
182         return attrs;
183     }
184     
185     protected void setDimension(Map attrs, String JavaDoc name, int value) {
186         String JavaDoc strval = (String JavaDoc) attrs.get(name);
187         if (strval != null && isPercentageDimension(strval))
188             attrs.put(name, ScriptCompiler.quote(strval));
189         else
190             attrs.put(name, new Integer JavaDoc(value));
191     }
192     
193     protected boolean isPercentageDimension(String JavaDoc str) {
194         return str.matches("\\s*(?:\\d+[.\\d*]|.\\d+)%\\s*");
195     }
196     
197     /**
198      * Initializes the canvas from the Element and
199      * removes any "special" children elements that
200      * should not be compiled.
201      *
202      * @param elt element that contains the canvas
203      */

204     public void initializeFromElement(Canvas canvas, Element elt) {
205         final String JavaDoc BGCOLOR_ATTR_NAME = "bgcolor";
206         boolean resizable = false;
207         
208         String JavaDoc width = elt.getAttributeValue("width");
209         String JavaDoc height = elt.getAttributeValue("height");
210         String JavaDoc bgcolor = elt.getAttributeValue(BGCOLOR_ATTR_NAME);
211         String JavaDoc title = elt.getAttributeValue("title");
212         
213         if (width != null) {
214             if (isPercentageDimension(width)) {
215                 resizable = true;
216                 canvas.setWidthString(width);
217             } else {
218                 try {
219                     canvas.setWidth(Integer.parseInt(width));
220                 } catch (NumberFormatException JavaDoc e) {
221                     throw new CompilationError(elt, "width", e);
222                 }
223             }
224         }
225         if (height != null) {
226             if (isPercentageDimension(height)) {
227                 resizable = true;
228                 canvas.setHeightString(height);
229             } else {
230                 try {
231                     canvas.setHeight(Integer.parseInt(height));
232                 } catch (NumberFormatException JavaDoc e) {
233                     throw new CompilationError(elt, "height", e);
234                 }
235             }
236         }
237         if (resizable && canvas.getSWFVersion().equals("swf5")) {
238             mEnv.warn("Percentage values for canvas width and height are not compatible with the swf5 runtime.");
239         }
240         if (bgcolor != null) {
241             try {
242                 canvas.setBGColor(ViewSchema.parseColor(bgcolor));
243             } catch (ColorFormatException e) {
244                 throw new CompilationError(elt, BGCOLOR_ATTR_NAME, e);
245             }
246         }
247         if (title != null) {
248             canvas.setTitle(title);
249         }
250         
251         // Persistent connection parameters
252
canvas.initializeConnection(elt);
253         
254         String JavaDoc version = elt.getAttributeValue("version");
255         if (version != null && !version.equals(canvas.DEFAULT_VERSION)) {
256             if (version.equals("1.0")) {
257                 // TODO: [2003-10-25 bloch] these should come from a
258
// properties file
259
canvas.defaultFont = "lztahoe8";
260                 canvas.defaultFontFilename = "lztahoe8.ttf";
261                 canvas.defaultBoldFontFilename = "lztahoe8b.ttf";
262                 canvas.defaultItalicFontFilename = "lztahoe8i.ttf";
263                 // NOTE: [2003-10-30 bloch] we don't have lztahoe8bi yet
264
// But 1.0 didn't either so this is prolly ok.
265
canvas.defaultBoldItalicFontFilename = "lztahoe8bi.ttf";
266             }
267         }
268         
269         String JavaDoc font = elt.getAttributeValue("font");
270         String JavaDoc fontstyle = elt.getAttributeValue("fontstyle");
271         String JavaDoc fontsize = elt.getAttributeValue("fontsize");
272         if (font == null || font.equals("")) {
273             font = canvas.defaultFont();
274         }
275         if (fontstyle == null || fontstyle.equals("")) {
276             fontstyle = canvas.DEFAULT_FONTSTYLE;
277         }
278         if (fontsize == null || fontsize.equals("") ) {
279             fontsize = canvas.defaultFontsize();
280         }
281         
282         canvas.setFontInfo(new FontInfo(font, fontsize, fontstyle));
283         
284         // The width of the "root" Flash output text resource. Since
285
// it's fixed at compile time, this gives the developer a way
286
// to specify it. Defaults to canvas width if not specified.
287
String JavaDoc maxtextwidth = elt.getAttributeValue("maxtextwidth");
288         if (maxtextwidth != null) {
289             try {
290                 canvas.setMaxTextWidth(Integer.parseInt(maxtextwidth));
291             } catch (NumberFormatException JavaDoc e) {
292                 throw new CompilationError(elt, "maxtextwidth", e);
293             }
294         }
295         
296         // The height of the "root" Flash output text resource. Since
297
// it's fixed at compile time, this gives the developer a way
298
// to specify it. Defaults to canvas height if not specified.
299
String JavaDoc maxtextheight = elt.getAttributeValue("maxtextheight");
300         if (maxtextheight != null) {
301             try {
302                 canvas.setMaxTextHeight(Integer.parseInt(maxtextheight));
303             } catch (NumberFormatException JavaDoc e) {
304                 throw new CompilationError(elt, "maxtextheight", e);
305             }
306         }
307     }
308     
309     private void collectObjectProperties(Element element, NodeModel model,
310                                          Set visited) {
311         for (Iterator iter = element.getChildren().iterator();
312              iter.hasNext(); ) {
313             Element child = (Element) iter.next();
314             if (NodeModel.isPropertyElement(child)) {
315                 model.addPropertyElement(child);
316             } else if (LibraryCompiler.isElement(child)) {
317                 Element libraryElement = LibraryCompiler.resolveLibraryElement(
318                     child, mEnv, visited, false);
319                 if (libraryElement != null) {
320                     collectObjectProperties(libraryElement, model, visited);
321                 }
322             }
323         }
324     }
325 }
326
Popular Tags