KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * SplashCompiler.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.apache.log4j.*;
16 import org.jdom.Element;
17
18 /** Compiler for <code>preloader</code> elements.
19  *
20  */

21 class SplashCompiler extends ElementCompiler {
22     /** Logger */
23     private static Logger mLogger = org.apache.log4j.Logger.getLogger(SplashCompiler.class);
24     private ViewSchema mSchema;
25     private static final String JavaDoc VIEW_INSTANTIATION_FNAME = "_root.lzpreloader.create";
26
27     SplashCompiler(CompilationEnvironment env) {
28         super(env);
29         mSchema = env.getSchema();
30     }
31
32     /** Returns true iff this class applies to this element.
33      * @param element an element
34      * @return see doc
35      */

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 JavaDoc script = new StringBuffer JavaDoc();
45
46         boolean persistent = "true".equals(element.getAttributeValue("persistent"));
47         element.removeAttribute("persistent");
48         String JavaDoc hideafterinit = new Boolean JavaDoc(!persistent).toString();
49         element.setAttribute("hideafterinit", hideafterinit);
50
51         if (element.getChild("view", element.getNamespace()) == null) {
52             // Add default preloader
53
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             // Change the child into the format that the runtime expects
71
// TODO: [2003-01-13 ows] change the runtime to expect the
72
// format that the schema defines instead
73
child.setName("preloadresource");
74             if (child.getAttribute("ratio") != null) {
75                 String JavaDoc 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 JavaDoc(str).floatValue();
83                 } catch (NumberFormatException JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] getBlogList() {
131         return new String JavaDoc[] {
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