1 4 5 9 10 package org.openlaszlo.compiler; 11 12 import java.io.*; 13 import java.util.*; 14 15 import org.apache.log4j.*; 16 import org.jdom.Element; 17 18 21 class SplashCompiler extends ElementCompiler { 22 23 private static Logger mLogger = org.apache.log4j.Logger.getLogger(SplashCompiler.class); 24 private ViewSchema mSchema; 25 private static final String VIEW_INSTANTIATION_FNAME = "_root.lzpreloader.create"; 26 27 SplashCompiler(CompilationEnvironment env) { 28 super(env); 29 mSchema = env.getSchema(); 30 } 31 32 36 static boolean isElement(Element element) { 37 return element.getName().equals("splash") 38 || element.getName().equals("preloader"); 39 } 40 41 public void compile(Element element) throws CompilationError { 42 ViewCompiler viewCompiler = new ViewCompiler(mEnv); 43 ResourceCompiler res = new ResourceCompiler(mEnv); 44 StringBuffer script = new StringBuffer (); 45 46 boolean persistent = "true".equals(element.getAttributeValue("persistent")); 47 element.removeAttribute("persistent"); 48 String hideafterinit = new Boolean (!persistent).toString(); 49 element.setAttribute("hideafterinit", hideafterinit); 50 51 if (element.getChild("view", element.getNamespace()) == null) { 52 ElementWithLocationInfo child = new ElementWithLocationInfo("view", element.getNamespace()); 54 child.initSourceLocator(((ElementWithLocationInfo)element).getSourceLocator() ); 55 child.setAttribute("hideafterinit", hideafterinit); 56 child.setAttribute("resource", "defaultpreloader.swf"); 57 child.setAttribute("center", "true"); 58 child.setAttribute("ratio", "100%"); 59 child.setAttribute("name", "lzprelresource"); 60 child.setAttribute("resourcename", "lzprelresource"); 61 element.addContent(child); 62 } 63 64 SWFWriter sw = mEnv.getGenerator(); 65 sw.addPreloader(mEnv); 66 67 for (Iterator iter = element.getChildren("view", element.getNamespace()).iterator(); 68 iter.hasNext(); ) { 69 Element child = (Element) iter.next(); 70 child.setName("preloadresource"); 74 if (child.getAttribute("ratio") != null) { 75 String str = child.getAttributeValue("ratio").trim(); 76 double value = 100; 77 if (str.endsWith("%")) { 78 str = str.substring(0, str.length() - 1); 79 value = 1; 80 } 81 try { 82 value *= new Float (str).floatValue(); 83 } catch (NumberFormatException e) { 84 throw new CompilationError(child, e); 85 } 86 child.setAttribute("synctoload", "true"); 87 child.setAttribute("lastframe", "" + value); 88 child.setAttribute("lastframe", "100"); 89 child.removeAttribute("ratio"); 90 } 91 if (child.getAttribute("resource") != null) { 92 File file = mEnv.resolveReference(child, "resource"); 93 String rname = child.getAttributeValue("resourcename"); 94 if (rname == null) { 95 rname = child.getAttributeValue("name"); 96 if (rname == null) { 97 rname = sw.createName(); 98 } 99 child.setAttribute("name", rname); 100 } 101 try { 102 sw.importPreloadResource(file.toString(), rname); 103 } catch (SWFWriter.ImportResourceError e) { 104 mEnv.warn(e, element); 105 } 106 child.setAttribute("resourcename", rname); 107 child.removeAttribute("resource"); 108 } 109 if (child.getAttribute("synchronized") != null) { 110 child.setAttribute("synctoload", child.getAttributeValue("synchronized")); 111 child.removeAttribute("synchronized"); 112 } 113 } 114 NodeModel model = NodeModel.elementAsModel(element, mSchema, mEnv); 115 script.append(VIEW_INSTANTIATION_FNAME + "(" + 116 model.asJavascript() + 117 ");" ); 118 119 String scriptstr = script.toString(); 120 if (scriptstr != "") { 121 try { 122 sw.addPreloaderScript(scriptstr); 123 } catch (org.openlaszlo.sc.CompilerException e) { 124 throw new CompilationError(element, e); 125 } 126 mLogger.debug("Adding preloader script: " + script); 127 } 128 } 129 130 private String [] getBlogList() { 131 return new String [] { 132 "http://wetmachine.com/", 133 "http://osteele.com/", 134 "http://www.davidtemkin.com/", 135 "http://www.ultrasaurus.com/", 136 "http://pt.withy.org/ptalk/" 137 }; 138 } 139 } 140 | Popular Tags |