KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * DataCompiler.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 import org.openlaszlo.iv.flash.api.action.*;
12 import java.io.*;
13 import org.jdom.Element;
14 import org.jdom.JDOMException;
15 import org.openlaszlo.xml.internal.XMLUtils;
16
17 /** Compiler for local data elements.
18  *
19  * @author Henry Minsky
20  * @author Oliver Steele
21  */

22 class DataCompiler extends ElementCompiler {
23     static final String JavaDoc LOCAL_DATA_FNAME = "lzAddLocalData";
24
25     DataCompiler(CompilationEnvironment env) {
26         super(env);
27     }
28
29     static boolean isElement(Element element) {
30         if (element.getName().equals("dataset")) {
31             // return type in ('soap', 'http') or src is url
32
String JavaDoc src = element.getAttributeValue("src");
33             String JavaDoc type = element.getAttributeValue("type");
34             if (type != null && (type.equals("soap") || type.equals("http"))) {
35                 return false;
36             }
37             if (src != null && src.indexOf("http:") == 0) {
38                 return false;
39             }
40             return src == null || !XMLUtils.isURL(src);
41         }
42         return false;
43     }
44     
45     public void compile(Element element) {
46         Element data = element;
47         if (element.getAttribute("src") != null) {
48             File file = resolveSrcReference(element);
49             try {
50                 Element newdata = new org.jdom.input.SAXBuilder(false)
51                     .build(file)
52                     .getRootElement();
53
54                 data.addContent(newdata);
55             } catch (org.jdom.JDOMException e) {
56                 throw new CompilationError(e);
57             } catch (java.lang.OutOfMemoryError JavaDoc e) {
58                 // The runtime gc is necessary in order for subsequent
59
// compiles to succeed. The System gc is for good
60
// luck.
61
throw new CompilationError("out of memory", element);
62             }
63         }
64
65         boolean trimwhitespace = true;
66         String JavaDoc trimAttr = element.getAttributeValue("trimwhitespace");
67         if (trimAttr != null && trimAttr.equals("false")) {
68             trimwhitespace = false;
69         }
70             
71         int flashVersion = mEnv.getSWFVersionInt();
72
73         Program program = org.openlaszlo.xml.internal.DataCompiler.makeProgram(data, flashVersion, trimwhitespace, true);
74         // this leaves the value in a variable named "__lzdataroot"
75

76         program.push(LOCAL_DATA_FNAME);
77         program.push("_level0");
78         program.getVar();
79         program.push(LOCAL_DATA_FNAME);
80         program.getMember();
81         program.setVar();
82
83         program.push("__lzdataroot");
84         program.getVar();
85         program.push(XMLUtils.requireAttributeValue(element, "name"));
86         program.push(2);
87         program.push(LOCAL_DATA_FNAME);
88         program.callFunction();
89         program.pop();
90         mEnv.getGenerator().addProgram(program);
91     }
92 }
93
Popular Tags