1 4 5 9 10 package org.openlaszlo.compiler; 11 import java.util.*; 12 import java.io.*; 13 14 import org.jdom.Element; 15 import org.openlaszlo.compiler.ViewCompiler.*; 16 import org.openlaszlo.server.*; 17 import org.openlaszlo.utils.*; 18 import org.jdom.*; 19 import org.apache.log4j.*; 20 21 23 abstract class ToplevelCompiler extends ElementCompiler { 24 25 private static Logger mLogger = Logger.getLogger(ToplevelCompiler.class); 26 27 ToplevelCompiler(CompilationEnvironment env) { 28 super(env); 29 } 30 31 35 static boolean isElement(Element element) { 36 return CanvasCompiler.isElement(element) 37 || LibraryCompiler.isElement(element) 38 || SwitchCompiler.isElement(element); 39 } 40 41 public void compile(Element element) { 42 for (Iterator iter = element.getChildren().iterator(); 43 iter.hasNext(); ) { 44 Element child = (Element) iter.next(); 45 if (!NodeModel.isPropertyElement(child)) { 46 Compiler.compileElement(child, mEnv); 47 } 48 } 49 } 50 51 62 void updateSchema(Element element, ViewSchema schema, Set visited) { 63 Iterator iterator = element.getChildren().iterator(); 64 while (iterator.hasNext()) { 65 Element child = (Element) iterator.next(); 66 if (!NodeModel.isPropertyElement(child)) { 67 Compiler.updateSchema(child, mEnv, schema, visited); 68 } 69 } 70 } 71 72 73 static void collectReferences(CompilationEnvironment env, 74 Element element, Set defined, 75 Set referenced) 76 { 77 Set visited = new HashSet(); 78 ViewCompiler.collectLayoutElement(element, referenced); 79 collectReferences(env, element, defined, referenced, visited); 80 } 81 82 86 static void collectReferences(CompilationEnvironment env, 87 Element element, Set defined, 88 Set referenced, Set libsVisited) { 89 ElementCompiler compiler = Compiler.getElementCompiler(element, env); 90 if (compiler instanceof ToplevelCompiler) { 91 if (compiler instanceof LibraryCompiler || compiler instanceof ImportCompiler ) { 92 Element library = LibraryCompiler.resolveLibraryElement(element, env, libsVisited, false); 93 if (library != null) { 94 element = library; 95 } 96 } 97 98 for (Iterator iter = element.getChildren().iterator(); 99 iter.hasNext(); ) { 100 collectReferences(env, (Element) iter.next(), defined, referenced, 101 libsVisited); 102 } 103 } else if (compiler instanceof ClassCompiler) { 104 String name = element.getAttributeValue("name"); 105 if (name != null) { 106 defined.add(name); 107 } 108 String superclass = element.getAttributeValue("extends"); 109 if (superclass != null) { 110 referenced.add(superclass); 111 } 112 ViewCompiler.collectElementNames(element, referenced); 113 } else if (compiler instanceof ViewCompiler) { 114 ViewCompiler.collectElementNames(element, referenced); 115 } 116 } 117 118 119 120 static List getLibraries(CompilationEnvironment env, Element element, Map explanations) { 121 List libraryNames = new ArrayList(); 122 String librariesAttr = element.getAttributeValue("libraries"); 123 if (librariesAttr != null) { 124 for (StringTokenizer st = new StringTokenizer(librariesAttr); 125 st.hasMoreTokens();) { 126 String name = (String ) st.nextToken(); 127 libraryNames.add(name); 128 } 129 } 130 { 133 Set defined = new HashSet(); 134 Set referenced = new HashSet(); 135 collectReferences(env, element, defined, referenced); 136 referenced.removeAll(defined); 137 Set additionalLibraries = new TreeSet(); 139 Map autoincludes = env.getSchema().sAutoincludes; 141 for (Iterator iter = referenced.iterator(); iter.hasNext(); ) { 142 String key = (String ) iter.next(); 143 if (autoincludes.containsKey(key)) { 144 String value = (String ) autoincludes.get(key); 145 additionalLibraries.add(value); 146 explanations.put(value, "reference to <" + key + "> tag"); 147 } 148 } 149 libraryNames.addAll(additionalLibraries); 150 } 151 List libraries = new ArrayList(); 153 String base = new File(Parser.getSourcePathname(element)).getParent(); 154 for (Iterator iter = libraryNames.iterator(); iter.hasNext(); ) { 155 String name = (String ) iter.next(); 156 try { 157 libraries.add(env.getFileResolver().resolve(name, base)); 158 } catch (FileNotFoundException e) { 159 throw new CompilationError(element, e); 160 } 161 } 162 163 if (env.getBooleanProperty(env.DEBUG_PROPERTY)) { 165 explanations.put("debugger", "the canvas debug attribute is true"); 166 String pathname = LPS.getComponentsDirectory() + 167 File.separator + "debugger" + 168 File.separator + "debugger.lzx"; 169 libraries.add(new File(pathname)); 170 } 171 return libraries; 172 } 173 174 List getLibraries(Element element) { 175 return getLibraries(mEnv, element, new HashMap()); 176 } 177 178 179 static String getBaseLibraryName (CompilationEnvironment env) { 180 String swfversion = "" + env.getSWFVersionInt(); 182 183 186 String ext = swfversion; 192 193 ext += env.getBooleanProperty(env.KRANK_PROPERTY)?"-krank": 195 (env.getBooleanProperty(env.PROFILE_PROPERTY)?"-profile":""); 196 ext += env.getBooleanProperty(env.DEBUG_PROPERTY)?"-debug":""; 197 return "LFC" + ext + ".lzl"; 198 } 199 200 static void handleAutoincludes(CompilationEnvironment env, Element element) { 201 Canvas canvas = env.getCanvas(); 204 205 String baseLibraryName = getBaseLibraryName(env); 206 String baseLibraryBecause = "Required for all applications"; 207 208 Map explanations = new HashMap(); 209 for (Iterator iter = getLibraries(env, element, explanations).iterator(); 210 iter.hasNext(); ) { 211 File file = (File) iter.next(); 212 Compiler.importLibrary(file, env); 213 } 214 215 Element info = new Element("include"); 217 info.setAttribute("name", baseLibraryName); 218 info.setAttribute("explanation", baseLibraryBecause); 219 try { 220 info.setAttribute("size", "" + 221 FileUtils.getSize(env.resolve(baseLibraryName, ""))); 222 } catch (Exception e) { 223 mLogger.error("exception getting library size", e); 224 } 225 canvas.addInfo(info); 226 227 for (Iterator iter = explanations.entrySet().iterator(); 229 iter.hasNext(); ) { 230 Map.Entry entry = (Map.Entry) iter.next(); 231 info = new Element("include"); 232 info.setAttribute("name", entry.getKey().toString()); 233 info.setAttribute("explanation", entry.getValue().toString()); 234 canvas.addInfo(info); 235 } 236 } 237 238 } 239 | Popular Tags |