KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************************
2  * ResourceCompiler.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
12 import org.openlaszlo.xml.internal.XMLUtils;
13 import org.openlaszlo.sc.ScriptCompiler;
14 import org.jdom.Element;
15 import java.io.File JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.util.*;
18 import org.apache.log4j.*;
19
20 /**
21  * Compiler for resource and audio elements.
22  *
23  * @author Oliver Steele
24  */

25 class ResourceCompiler extends ElementCompiler {
26
27     Logger mLogger = Logger.getLogger(ResourceCompiler.class);
28
29     ResourceCompiler(CompilationEnvironment env) {
30         super(env);
31     }
32
33     /** Returns true iff this class applies to this element.
34      * @param element an element
35      * @return see doc
36      */

37     static boolean isElement(Element element) {
38         String JavaDoc name = element.getName();
39         return name.equals("resource")
40             || name.equals("audio");
41     }
42
43     /*
44      * @see <code>CompilerNode</code>
45      * @param env a compilation environment
46      */

47     public void compile(Element element)
48         throws CompilationError
49     {
50         File JavaDoc 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 JavaDoc ioe) {
64                 mLogger.warn("Can't canonicalize " + file.toString());
65             }
66             mEnv.getCanvas().addInfo(info);
67         }
68         
69         String JavaDoc tagName = element.getName();
70         try {
71             if (tagName.equals("resource") || tagName.equals("preloadresource")) {
72                 String JavaDoc name = element.getAttributeValue("name");
73                 String JavaDoc 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                 // TODO: [1-02-2003 ows]
84
// XMLUtils.requireAttributeValue should work here,
85
// and the caller should add the source location info
86
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                 // N.B.: Resources are always imported into the main
96
// program for the Flash target, hence the use of
97
// getResourceGenerator below
98
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                         // throw error if 'src' attribute not found
106
XMLUtils.requireAttributeValue(child, "src");
107                         File JavaDoc 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 JavaDoc name = XMLUtils.requireAttributeValue(element, "name");
136                 mEnv.getResourceGenerator().importResource(file.getAbsolutePath(),
137                                                    name);
138                 return;
139             } else {
140                 // Program error: this shouldn't happen
141
throw new RuntimeException JavaDoc("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