1 4 5 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 24 class ImportCompiler extends ToplevelCompiler { 25 final static String HREF_ANAME = "href"; 26 final static String 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 41 static String stringDiff(String s1, String 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 href = XMLUtils.requireAttributeValue(element, HREF_ANAME); 50 String libname = XMLUtils.requireAttributeValue(element, NAME_ANAME); 51 String 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 61 String libproxied = module.getAttributeValue("proxied", "inherit"); 62 String 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 File appdir = mEnv.getApplicationFile().getParentFile(); 72 File file = mEnv.resolveReference(element, HREF_ANAME); 73 File libsrcfile = LibraryCompiler.resolveLibraryName(file); 75 String adjustedhref = libsrcfile.getPath(); 76 77 if (appdir != null) { 78 adjustedhref = FileUtils.relativePath(libsrcfile.getPath(), appdir.getPath()); 79 } 80 81 82 element.setAttribute(HREF_ANAME, adjustedhref); 84 85 String libfile = libsrcfile.getName(); 88 String libprefix = mEnv.getLibPrefix(); 89 String 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 e) { 96 throw new CompilationError(element, e); 97 } 98 99 compileLibrary(libsrcfile, objfilename, module); 100 101 ViewCompiler vc = new ViewCompiler(mEnv); 104 105 String 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 122 void compileLibrary(File infile, String outfile, Element element) throws CompilationError{ 123 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 env.setMainSWFWriter(mEnv.getGenerator()); 141 142 env.setEmbedFonts(false); 146 147 writer.setFontManager(mEnv.getGenerator().getFontManager()); 149 writer.setCanvasDefaults(mEnv.getCanvas(), mEnv.getMediaCache()); 150 151 writer.openSnippet(); 152 153 env.compileScript("var "+VIEW_INSTANTIATION_FNAME+" = _level0."+VIEW_INSTANTIATION_FNAME, element); 156 157 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 178 mEnv.getErrorHandler().appendErrors(env.getErrorHandler()); 179 180 } catch (CompilationError e) { 181 e.attachErrors(errors.getErrors()); 183 throw e; 184 } catch (org.openlaszlo.xml.internal.MissingAttributeException e) { 187 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 |