1 4 5 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 24 class CanvasCompiler extends ToplevelCompiler { 25 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 public static boolean APP_PROXIED_DEFAULT = true; 38 39 public void compile(Element element) throws CompilationError 40 { 41 Canvas canvas = new Canvas(); 42 String lzproxied = mEnv.getProperty(mEnv.PROXIED_PROPERTY); 44 String cproxied = element.getAttributeValue("proxied"); 46 47 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 if (lzproxied != null) { 59 canvas.setProxied(lzproxied.equals("true")); 60 } else { 61 canvas.setProxied(APP_PROXIED_DEFAULT); 62 } 63 } 64 65 String versionNumber = element.getAttributeValue("version"); 66 if (versionNumber != null) { 67 String 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 baseLibraryName = getBaseLibraryName(mEnv); 77 String baseLibraryBecause = "Required for all applications"; 78 79 82 mEnv.getGenerator().importBaseLibrary(baseLibraryName, mEnv); 83 84 canvas.setSWFVersion(mEnv.getSWFVersion()); 85 initializeFromElement(canvas, element); 86 87 boolean embedFonts = true; 89 String 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 script; 97 try { 98 java.io.Writer writer = new java.io.StringWriter (); 99 writer.write("canvas = new LzCanvas("); 100 ScriptCompiler.writeObject(map, writer); 101 writer.write(");"); 102 script = writer.toString(); 103 } catch (java.io.IOException 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 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 for (Iterator iter = element.getChildren().iterator(); 123 iter.hasNext(); ) { 124 Element child = (Element) iter.next(); 125 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 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 (canvas.getBGColor())); 172 FontInfo fontInfo = canvas.getFontInfo(); 173 attrs.put("fontname", ScriptCompiler.quote(fontInfo.getName())); 174 attrs.put("fontsize", new Integer (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 attrs.remove("debug"); 180 attrs.remove("libraries"); 182 return attrs; 183 } 184 185 protected void setDimension(Map attrs, String name, int value) { 186 String strval = (String ) attrs.get(name); 187 if (strval != null && isPercentageDimension(strval)) 188 attrs.put(name, ScriptCompiler.quote(strval)); 189 else 190 attrs.put(name, new Integer (value)); 191 } 192 193 protected boolean isPercentageDimension(String str) { 194 return str.matches("\\s*(?:\\d+[.\\d*]|.\\d+)%\\s*"); 195 } 196 197 204 public void initializeFromElement(Canvas canvas, Element elt) { 205 final String BGCOLOR_ATTR_NAME = "bgcolor"; 206 boolean resizable = false; 207 208 String width = elt.getAttributeValue("width"); 209 String height = elt.getAttributeValue("height"); 210 String bgcolor = elt.getAttributeValue(BGCOLOR_ATTR_NAME); 211 String 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 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 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 canvas.initializeConnection(elt); 253 254 String version = elt.getAttributeValue("version"); 255 if (version != null && !version.equals(canvas.DEFAULT_VERSION)) { 256 if (version.equals("1.0")) { 257 canvas.defaultFont = "lztahoe8"; 260 canvas.defaultFontFilename = "lztahoe8.ttf"; 261 canvas.defaultBoldFontFilename = "lztahoe8b.ttf"; 262 canvas.defaultItalicFontFilename = "lztahoe8i.ttf"; 263 canvas.defaultBoldItalicFontFilename = "lztahoe8bi.ttf"; 266 } 267 } 268 269 String font = elt.getAttributeValue("font"); 270 String fontstyle = elt.getAttributeValue("fontstyle"); 271 String 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 String maxtextwidth = elt.getAttributeValue("maxtextwidth"); 288 if (maxtextwidth != null) { 289 try { 290 canvas.setMaxTextWidth(Integer.parseInt(maxtextwidth)); 291 } catch (NumberFormatException e) { 292 throw new CompilationError(elt, "maxtextwidth", e); 293 } 294 } 295 296 String maxtextheight = elt.getAttributeValue("maxtextheight"); 300 if (maxtextheight != null) { 301 try { 302 canvas.setMaxTextHeight(Integer.parseInt(maxtextheight)); 303 } catch (NumberFormatException 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 |