KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * LibraryCompiler.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.utils.ChainedException;
16 import org.apache.log4j.*;
17
18 /** Compiler for <code>library</code> elements.
19  *
20  * @author Oliver Steele
21  */

22 class LibraryCompiler extends ToplevelCompiler {
23     final static String JavaDoc HREF_ANAME = "href";
24
25     /** Logger
26      */

27     private static Logger mLogger = Logger.getLogger(Compiler JavaDoc.class);
28
29
30     
31     LibraryCompiler(CompilationEnvironment env) {
32         super(env);
33     }
34
35     static boolean isElement(Element element) {
36         return element.getName().equals("library");
37     }
38
39     // TODO: [ows] this duplicates functionality in Parser
40
static File resolveLibraryName(File file)
41     {
42         if (file.isDirectory()) {
43             file = new File(file, "library.lzx");
44         }
45         return file;
46     }
47
48     /** Return the library element and add the library to visited. If
49      * the library has already been visited, return null instead.
50      */

51     static Element resolveLibraryElement(File file,
52                                          CompilationEnvironment env,
53                                          Set visited,
54                                          boolean validate)
55     {
56         try {
57             file = resolveLibraryName(file);
58             File key = file.getCanonicalFile();
59             if (!visited.contains(key)) {
60                 visited.add(key);
61
62                 // If we're compiling a loadable library, add this to
63
// the list of library files which which have been
64
// included by loadable libraries, so we can warn on
65
// duplicates.
66
if (env.isImportLib()) {
67
68                     // compare this library file with the set of all known libraries that
69
// have been included in loadable modules. If this has been seen before,
70
// issue warning.
71
if (env.isImportLib() && env.getLoadableImportedLibraryFiles().containsKey(key)) {
72                         env.warn("The library file \""+file+"\" included by loadable library \""
73                                  + env.getApplicationFile()
74                                  + "\" was also included by another loadable library \""
75                                  + env.getLoadableImportedLibraryFiles().get(key) +"\". "
76                                  + "This may lead to unexpected behavior, especially if the library defines new classes.");
77                     }
78
79                     env.getLoadableImportedLibraryFiles().put(key, env.getApplicationFile());
80                 }
81
82                 Document doc = env.getParser().parse(file);
83                 if (validate)
84                     Parser.validate(doc, file.getPath(), env);
85                 Element root = doc.getRootElement();
86                 return root;
87             } else {
88                 return null;
89             }
90         } catch (IOException e) {
91             throw new CompilationError(e);
92         }
93     }
94     
95     /** Return the resolved library element and add the library to visited.
96      * If the library has already been visited, return null instead.
97      */

98     static Element resolveLibraryElement(Element element,
99                                          CompilationEnvironment env,
100                                          Set visited,
101                                          boolean validate)
102     {
103         String JavaDoc href = element.getAttributeValue(HREF_ANAME);
104         if (href == null) {
105             return element;
106         }
107         File file = env.resolveReference(element, HREF_ANAME);
108         return resolveLibraryElement(file, env, visited, validate);
109     }
110     
111     public void compile(Element element) throws CompilationError
112     {
113         element = resolveLibraryElement(
114             element, mEnv, mEnv.getImportedLibraryFiles(),
115             mEnv.getBooleanProperty(mEnv.VALIDATE_PROPERTY));
116         if (element != null) {
117             super.compile(element);
118         }
119     }
120
121     void updateSchema(Element element, ViewSchema schema, Set visited) {
122         element = resolveLibraryElement(element, mEnv, visited, false);
123         if (element != null) {
124             super.updateSchema(element, schema, visited);
125             // TODO [hqm 2005-02-09] can we compare any 'proxied' attribute here
126
// with the parent element (canvas) to warn if it conflicts.
127
}
128     }
129 }
130
Popular Tags