1 4 5 9 10 package org.openlaszlo.compiler; 11 12 import org.openlaszlo.xml.internal.XMLUtils; 13 import org.openlaszlo.sc.ScriptCompiler; 14 import org.jdom.Element; 15 import java.io.File ; 16 import java.io.FileNotFoundException ; 17 import java.util.*; 18 import org.apache.log4j.*; 19 20 25 class ResourceCompiler extends ElementCompiler { 26 27 Logger mLogger = Logger.getLogger(ResourceCompiler.class); 28 29 ResourceCompiler(CompilationEnvironment env) { 30 super(env); 31 } 32 33 37 static boolean isElement(Element element) { 38 String name = element.getName(); 39 return name.equals("resource") 40 || name.equals("audio"); 41 } 42 43 47 public void compile(Element element) 48 throws CompilationError 49 { 50 File file = null; 51 52 if (element.hasChildren()) { 53 file = mEnv.resolveParentReference(element); 54 } else { 55 file = mEnv.resolveReference(element); 56 } 57 58 if (element.getAttribute("src") != null) { 59 Element info = new Element("resolve"); 60 info.setAttribute("src", element.getAttributeValue("src")); 61 try { 62 info.setAttribute("pathname", file.getCanonicalPath()); 63 } catch (java.io.IOException ioe) { 64 mLogger.warn("Can't canonicalize " + file.toString()); 65 } 66 mEnv.getCanvas().addInfo(info); 67 } 68 69 String tagName = element.getName(); 70 try { 71 if (tagName.equals("resource") || tagName.equals("preloadresource")) { 72 String name = element.getAttributeValue("name"); 73 String src = element.getAttributeValue("src"); 74 75 if (name == null) { 76 throw new CompilationError("You must supply a value for the 'name' attribute, and it must be a valid Javascript identifier.", element); 77 } 78 79 if (!ScriptCompiler.isIdentifier(name)) { 80 mEnv.warn("Resource names must be valid Javascript identifiers. The resource name '"+name+"' is not a valid Javascript identifier", element); 81 } 82 83 if (name == null) 87 throw new CompilationError("resource name is required", element); 88 89 Set resourceNames = mEnv.getResourceNames(); 90 if (resourceNames.contains(name)) { 91 mEnv.warn("The resource name '"+name+"' has already been defined", element); 92 } 93 resourceNames.add(name); 94 95 if (src == null) { 99 List sources = new Vector(); 100 for (Iterator iter = element.getChildren("frame", element.getNamespace()).iterator(); 101 iter.hasNext(); ) { 102 Element child = (Element) iter.next(); 103 mEnv.resolveReference(child); 104 105 XMLUtils.requireAttributeValue(child, "src"); 107 File pathname = mEnv.resolveReference(child); 108 sources.add(pathname.getAbsolutePath()); 109 110 Element rinfo = new Element("resolve"); 111 rinfo.setAttribute("src", child.getAttributeValue("src")); 112 rinfo.setAttribute("pathname", pathname.toString()); 113 mEnv.getCanvas().addInfo(rinfo); 114 } 115 if (!sources.isEmpty()) { 116 if (tagName.equals("preloadresource")) { 117 mEnv.getResourceGenerator().importPreloadResource(sources, name, file); 118 } else { 119 mEnv.getResourceGenerator().importResource(sources, name, file); 120 } 121 } else { 122 throw new CompilationError("required src or children", element); 123 } 124 } else { 125 if (tagName.equals("preloadresource")) { 126 mEnv.getResourceGenerator().importPreloadResource(file.getAbsolutePath(), 127 name); 128 } else { 129 mEnv.getResourceGenerator().importResource(file.getAbsolutePath(), 130 name); 131 } 132 } 133 return; 134 } else if (tagName.equals("audio")) { 135 String name = XMLUtils.requireAttributeValue(element, "name"); 136 mEnv.getResourceGenerator().importResource(file.getAbsolutePath(), 137 name); 138 return; 139 } else { 140 throw new RuntimeException ("Unknown resource tag: " + 142 element.getName()); 143 } 144 } catch (SWFWriter.ImportResourceError e) { 145 mEnv.warn(e, element); 146 return; 147 } 148 } 149 } 150 | Popular Tags |