KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * ImportCompiler.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 java.io.*;
12 import java.util.*;
13 import org.jdom.Document;
14 import org.jdom.Element;
15 import org.openlaszlo.xml.internal.XMLUtils;
16 import org.openlaszlo.utils.FileUtils;
17 import org.openlaszlo.sc.ScriptCompiler;
18 import org.openlaszlo.utils.ChainedException;
19 import org.apache.log4j.*;
20 /** Compiler for <code>import</code> elements.
21  *
22  * @author Henry Minsky
23  */

24 class ImportCompiler extends ToplevelCompiler {
25     final static String JavaDoc HREF_ANAME = "href";
26     final static String JavaDoc NAME_ANAME = "name";
27     
28     private static Logger mLogger = Logger.getLogger(ImportCompiler.class);
29
30     ImportCompiler(CompilationEnvironment env) {
31         super(env);
32     }
33
34     static boolean isElement(Element element) {
35         return element.getName().equals("import");
36     }
37
38     /** Return the difference of two strings.
39      * stringDiff("foo/bar/baz.lzx", "foo/bar") ==> /baz.lzx
40      */

41     static String JavaDoc stringDiff(String JavaDoc s1, String JavaDoc s2) {
42         int sl1 = s1.length();
43         int sl2 = s2.length();
44         return s1.substring(sl2+1, sl1);
45     }
46
47     public void compile(Element element) throws CompilationError
48     {
49         String JavaDoc href = XMLUtils.requireAttributeValue(element, HREF_ANAME);
50         String JavaDoc libname = XMLUtils.requireAttributeValue(element, NAME_ANAME);
51         String JavaDoc stage = XMLUtils.requireAttributeValue(element, "stage");
52
53         Element module = LibraryCompiler.resolveLibraryElement(
54             element, mEnv, mEnv.getImportedLibraryFiles(),
55             mEnv.getBooleanProperty(mEnv.VALIDATE_PROPERTY));
56         if (module != null) {
57             // check for conflict in the value of the "proxied"
58
// attribute declared on the <import> tag vs the
59
// <library> tag.
60

61             String JavaDoc libproxied = module.getAttributeValue("proxied", "inherit");
62             String JavaDoc importproxied = element.getAttributeValue("proxied", "inherit");
63
64             if ((importproxied.equals("true") && libproxied.equals("false")) ||
65                 (importproxied.equals("false") && libproxied.equals("true"))){
66                 mEnv.warn("The value of the 'proxied' attribute on this library, '"+libproxied+"', conflicts with the import element value of '"+importproxied+"'", element);
67             }
68                 
69             // We're not compiling this into the current app, we're
70
// building a separate binary library object file for it.
71
File appdir = mEnv.getApplicationFile().getParentFile();
72             File file = mEnv.resolveReference(element, HREF_ANAME);
73             // If the filename is a directory "dir", we need to make it "dir/library.lzx"
74
File libsrcfile = LibraryCompiler.resolveLibraryName(file);
75             String JavaDoc adjustedhref = libsrcfile.getPath();
76
77             if (appdir != null) {
78                 adjustedhref = FileUtils.relativePath(libsrcfile.getPath(), appdir.getPath());
79             }
80
81
82             // If we added a "library.lzx" to the path, set it properly in the XML
83
element.setAttribute(HREF_ANAME, adjustedhref);
84
85             // I'm scared of the CompilationManager, just generate the output file
86
// directly for now.
87
String JavaDoc libfile = libsrcfile.getName();
88             String JavaDoc libprefix = mEnv.getLibPrefix();
89             String JavaDoc objfilename = libprefix + "/" + libfile + ".swf";
90                 
91             mLogger.info("calling compilerLibrary libsrcfile="+libsrcfile+" objfile="+objfilename);
92
93             try {
94                 FileUtils.makeFileAndParentDirs(new File(objfilename));
95             } catch (java.io.IOException JavaDoc e) {
96                 throw new CompilationError(element, e);
97             }
98
99             compileLibrary(libsrcfile, objfilename, module);
100
101             // Emit code into main app to instantiate a LzLibrary
102
// object, which implements the load() method.
103
ViewCompiler vc = new ViewCompiler(mEnv);
104
105             // Override the href with a pointer to the library object-file build directory
106
String JavaDoc objpath = mEnv.getLibPrefixRelative() + "/" + libfile + ".swf";
107             element.setAttribute("href", objpath);
108             vc.compile(element);
109         }
110     }
111
112     void updateSchema(Element element, ViewSchema schema, Set visited) {
113         element = LibraryCompiler.resolveLibraryElement(element, mEnv, visited, false);
114         if (element != null) {
115             super.updateSchema(element, schema, visited);
116         }
117     }
118
119     /**
120      * Compile a standalone binary library file with no canvas.
121      */

122     void compileLibrary(File infile, String JavaDoc outfile, Element element) throws CompilationError{
123         // copy fields from mEnv to new Environment
124
CompilationEnvironment env = new CompilationEnvironment(mEnv);
125         CompilationErrorHandler errors = env.getErrorHandler();
126         env.setApplicationFile(infile);
127         Properties props = (Properties) env.getProperties().clone();
128         byte[] action;
129
130         try {
131
132             OutputStream ostream = new FileOutputStream(outfile);
133
134             try {
135                 SWFWriter writer = new SWFWriter(env.getProperties(), ostream,
136                                                  env.getMediaCache(), false, env);
137                 env.setSWFWriter(writer);
138                 // Set the main SWFWriter so we can output resources
139
// to the main app
140
env.setMainSWFWriter(mEnv.getGenerator());
141
142                 // TODO [hqm 2004-10-05] For now we'll never include fonts in the library file.
143
// At some later date we can decide if we want to do that, and if so, we'll have to
144
// make the compiler use a separate FontsCollector when it descends into the library.
145
env.setEmbedFonts(false);
146
147                 // copy the fontmanager from old env to new one.
148
writer.setFontManager(mEnv.getGenerator().getFontManager());
149                 writer.setCanvasDefaults(mEnv.getCanvas(), mEnv.getMediaCache());
150
151                 writer.openSnippet();
152
153                 // allows snippet code to call out to LzInstantiateView in the main app:
154
// var LzInstantiateView = _level0.LzInstantiateView;
155
env.compileScript("var "+VIEW_INSTANTIATION_FNAME+" = _level0."+VIEW_INSTANTIATION_FNAME, element);
156
157                 // Note: canvas.initDone() resets the _lzinitialsubviews list, so
158
// that has to be called when the library finishes loading. This is currently
159
// done by LzLibraryLoader.snippetLoaded(), which is the callback
160
// that we emit at the end of our snippet file.
161

162                 for (Iterator iter = element.getChildren().iterator();
163                      iter.hasNext(); ) {
164                     Element child = (Element) iter.next();
165                     if (!NodeModel.isPropertyElement(child)) {
166                         Compiler.compileElement(child, env);
167                     }
168                 }
169
170                 ViewCompiler.checkUnresolvedResourceReferences (env);
171                 writer.closeSnippet();
172             } finally {
173                 ostream.close();
174             }
175
176             /** Copy any compilation errors/warnings over to canvas's CompilationErrorHandler
177              */

178             mEnv.getErrorHandler().appendErrors(env.getErrorHandler());
179
180         } catch (CompilationError e) {
181             // TBD: e.initPathname(file.getPath());
182
e.attachErrors(errors.getErrors());
183             throw e;
184             //errors.addError(new CompilationError(e.getMessage() + "; compilation aborted"));
185
//throw errors.toCompilationError();
186
} catch (org.openlaszlo.xml.internal.MissingAttributeException e) {
187             /* The validator will have caught this, but if we simply
188              * pass the error through, the vaildation warnings will
189              * never be printed. Create a new message that includes
190              * them so that we get the source information. */

191             errors.addError(new CompilationError(e.getMessage() + "; compilation aborted"));
192             throw errors.toCompilationError();
193         } catch (IOException e) {
194             throw new CompilationError(element, e);
195         }
196
197     }
198 }
199
Popular Tags