001: /* *****************************************************************************
002: * SplashCompiler.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.compiler;
011:
012: import java.io.*;
013: import java.util.*;
014:
015: import org.apache.log4j.*;
016: import org.jdom.Element;
017:
018: /** Compiler for <code>preloader</code> elements.
019: *
020: */
021: class SplashCompiler extends ElementCompiler {
022: /** Logger */
023: private static Logger mLogger = org.apache.log4j.Logger
024: .getLogger(SplashCompiler.class);
025: private ViewSchema mSchema;
026: private static final String VIEW_INSTANTIATION_FNAME = "_root.lzpreloader.create";
027:
028: SplashCompiler(CompilationEnvironment env) {
029: super (env);
030: mSchema = env.getSchema();
031: }
032:
033: /** Returns true iff this class applies to this element.
034: * @param element an element
035: * @return see doc
036: */
037: static boolean isElement(Element element) {
038: return element.getName().equals("splash")
039: || element.getName().equals("preloader");
040: }
041:
042: public void compile(Element element) throws CompilationError {
043: if (mEnv.isDHTML())
044: return;
045:
046: ViewCompiler viewCompiler = new ViewCompiler(mEnv);
047: ResourceCompiler res = new ResourceCompiler(mEnv);
048: StringBuffer script = new StringBuffer();
049:
050: boolean persistent = "true".equals(element
051: .getAttributeValue("persistent"));
052: element.removeAttribute("persistent");
053: String hideafterinit = new Boolean(!persistent).toString();
054: element.setAttribute("hideafterinit", hideafterinit);
055:
056: if (element.getChild("view", element.getNamespace()) == null) {
057: // Add default preloader
058:
059: ElementWithLocationInfo child = new ElementWithLocationInfo(
060: "view", element.getNamespace());
061: child.initSourceLocator(((ElementWithLocationInfo) element)
062: .getSourceLocator());
063: child.setAttribute("hideafterinit", hideafterinit);
064: child.setAttribute("resource", "defaultpreloader.swf");
065: child.setAttribute("center", "true");
066: child.setAttribute("ratio", "100%");
067: child.setAttribute("name", "lzprelresource");
068: child.setAttribute("resourcename", "lzprelresource");
069: element.addContent(child);
070: }
071:
072: ObjectWriter sw = mEnv.getGenerator();
073: sw.addPreloader(mEnv);
074:
075: for (Iterator iter = element.getChildren("view",
076: element.getNamespace()).iterator(); iter.hasNext();) {
077: Element child = (Element) iter.next();
078: // Change the child into the format that the runtime expects
079: // TODO: [2003-01-13 ows] change the runtime to expect the
080: // format that the schema defines instead
081: child.setName("preloadresource");
082: if (child.getAttribute("ratio") != null) {
083: String str = child.getAttributeValue("ratio").trim();
084: double value = 1;
085: if (str.endsWith("%")) {
086: str = str.substring(0, str.length() - 1);
087: value = Float.parseFloat(str) / 100;
088: } else {
089: try {
090: value = Float.parseFloat(str);
091: } catch (NumberFormatException e) {
092: throw new CompilationError(child, e);
093: }
094: }
095: child.setAttribute("synctoload", "true");
096: child.setAttribute("lastframe", "" + value);
097: child.removeAttribute("ratio");
098: }
099: if (child.getAttribute("resource") != null) {
100: File file = mEnv.resolveReference(child, "resource");
101: String rname = child.getAttributeValue("resourcename");
102: if (rname == null) {
103: rname = child.getAttributeValue("name");
104: if (rname == null) {
105: rname = sw.createName();
106: }
107: child.setAttribute("name", rname);
108: }
109: try {
110: sw.importPreloadResource(file.toString(), rname);
111: } catch (SWFWriter.ImportResourceError e) {
112: mEnv.warn(e, element);
113: }
114: child.setAttribute("resourcename", rname);
115: child.removeAttribute("resource");
116: }
117: if (child.getAttribute("synchronized") != null) {
118: child.setAttribute("synctoload", child
119: .getAttributeValue("synchronized"));
120: child.removeAttribute("synchronized");
121: }
122: }
123: NodeModel model = NodeModel.elementAsModel(element, mSchema,
124: mEnv);
125: script.append(VIEW_INSTANTIATION_FNAME + "("
126: + model.asJavascript() + ");");
127:
128: String scriptstr = script.toString();
129: if (scriptstr != "") {
130: try {
131: sw.addPreloaderScript(scriptstr);
132: } catch (org.openlaszlo.sc.CompilerException e) {
133: throw new CompilationError(element, e);
134: }
135: mLogger.debug("Adding preloader script: " + script);
136: }
137: }
138:
139: private String[] getBlogList() {
140: return new String[] { "http://wetmachine.com/",
141: "http://osteele.com/", "http://www.davidtemkin.com/",
142: "http://www.ultrasaurus.com/",
143: "http://pt.withy.org/ptalk/" };
144: }
145: }
|