001: /* -*- mode: Java; c-basic-offset: 2; -*- */
002:
003: /**
004: * LZX Resource Compiler
005: */package org.openlaszlo.compiler;
006:
007: import org.openlaszlo.xml.internal.XMLUtils;
008: import org.openlaszlo.sc.ScriptCompiler;
009: import org.jdom.Element;
010: import java.io.File;
011: import java.io.FileNotFoundException;
012: import java.util.*;
013: import org.apache.log4j.*;
014:
015: /**
016: * Compiler for resource and audio elements.
017: *
018: * @author Oliver Steele
019: */
020: class ResourceCompiler extends ElementCompiler {
021:
022: Logger mLogger = Logger.getLogger(ResourceCompiler.class);
023:
024: ResourceCompiler(CompilationEnvironment env) {
025: super (env);
026: }
027:
028: /** Returns true iff this class applies to this element.
029: * @param element an element
030: * @return see doc
031: */
032: static boolean isElement(Element element) {
033: String name = element.getName();
034: return name.equals("resource") || name.equals("audio");
035: }
036:
037: /*
038: * @see <code>CompilerNode</code>
039: * @param env a compilation environment
040: */
041: public void compile(Element element) throws CompilationError {
042: File file = null;
043:
044: if (!element.getChildren().isEmpty()) {
045: file = mEnv.resolveParentReference(element);
046: } else {
047: file = mEnv.resolveReference(element);
048: }
049:
050: if (element.getAttribute("src") != null) {
051: Element info = new Element("resolve");
052: info.setAttribute("src", element.getAttributeValue("src"));
053: try {
054: info.setAttribute("pathname", file.getCanonicalPath());
055: } catch (java.io.IOException ioe) {
056: mLogger.warn(
057: /* (non-Javadoc)
058: * @i18n.test
059: * @org-mes="Can't canonicalize " + p[0]
060: */
061: org.openlaszlo.i18n.LaszloMessages.getMessage(
062: ResourceCompiler.class.getName(), "051018-69",
063: new Object[] { file.toString() }));
064: }
065: if (mEnv.isCanvas()) {
066: mEnv.getCanvas().addInfo(info);
067: }
068: }
069:
070: String tagName = element.getName();
071: try {
072: if (tagName.equals("resource")
073: || tagName.equals("preloadresource")) {
074: String name = element.getAttributeValue("name");
075: String src = element.getAttributeValue("src");
076:
077: if (name == null) {
078: throw new CompilationError(
079: /* (non-Javadoc)
080: * @i18n.test
081: * @org-mes="You must supply a value for the 'name' attribute, and it must be a valid Javascript identifier."
082: */
083: org.openlaszlo.i18n.LaszloMessages.getMessage(
084: ResourceCompiler.class.getName(),
085: "051018-88"), element);
086: }
087:
088: if (!ScriptCompiler.isIdentifier(name)) {
089: mEnv.warn(
090: /* (non-Javadoc)
091: * @i18n.test
092: * @org-mes="Resource names must be valid Javascript identifiers. The resource name '" + p[0] + "' is not a valid Javascript identifier"
093: */
094: org.openlaszlo.i18n.LaszloMessages.getMessage(
095: ResourceCompiler.class.getName(),
096: "051018-99", new Object[] { name }),
097: element);
098: }
099:
100: // TODO: [1-02-2003 ows]
101: // XMLUtils.requireAttributeValue should work here,
102: // and the caller should add the source location info
103: if (name == null)
104: throw new CompilationError(
105: /* (non-Javadoc)
106: * @i18n.test
107: * @org-mes="resource name is required"
108: */
109: org.openlaszlo.i18n.LaszloMessages.getMessage(
110: ResourceCompiler.class.getName(),
111: "051018-113"), element);
112:
113: Set resourceNames = mEnv.getResourceNames();
114: if (false) {
115: if (resourceNames.contains(name)) {
116: mEnv.warn(
117: /* (non-Javadoc)
118: * @i18n.test
119: * @org-mes="The resource name '" + p[0] + "' has already been defined"
120: */
121: org.openlaszlo.i18n.LaszloMessages.getMessage(
122: ResourceCompiler.class.getName(),
123: "051018-124", new Object[] { name }),
124: element);
125: }
126: }
127: resourceNames.add(name);
128:
129: // Check if children are valid tags to be contained
130: mEnv.checkValidChildContainment(element);
131:
132: // N.B.: Resources are always imported into the main
133: // program for the Flash target, hence the use of
134: // getResourceGenerator below
135: if (src == null) {
136: List sources = new Vector();
137: for (Iterator iter = element.getChildren("frame",
138: element.getNamespace()).iterator(); iter
139: .hasNext();) {
140: Element child = (Element) iter.next();
141: mEnv.resolveReference(child);
142:
143: // throw error if 'src' attribute not found
144: XMLUtils.requireAttributeValue(child, "src");
145: File pathname = mEnv.resolveReference(child);
146: sources.add(pathname.getAbsolutePath());
147:
148: Element rinfo = new Element("resolve");
149: rinfo.setAttribute("src", child
150: .getAttributeValue("src"));
151: rinfo.setAttribute("pathname", pathname
152: .toString());
153: if (mEnv.isCanvas()) {
154: mEnv.getCanvas().addInfo(rinfo);
155: }
156: }
157: if (!sources.isEmpty()) {
158: if (tagName.equals("preloadresource")) {
159: mEnv.getResourceGenerator()
160: .importPreloadResource(sources,
161: name, file);
162: } else {
163: mEnv.getResourceGenerator().importResource(
164: sources, name, file);
165: }
166: } else {
167: throw new CompilationError(
168: /* (non-Javadoc)
169: * @i18n.test
170: * @org-mes="required src or children"
171: */
172: org.openlaszlo.i18n.LaszloMessages.getMessage(
173: ResourceCompiler.class.getName(),
174: "051018-162"), element);
175: }
176: } else {
177: if (tagName.equals("preloadresource")) {
178: mEnv.getResourceGenerator()
179: .importPreloadResource(
180: file.getAbsolutePath(), name);
181: } else {
182: mEnv.getResourceGenerator().importResource(
183: file.getAbsolutePath(), name);
184: }
185: }
186: return;
187: } else if (tagName.equals("audio")) {
188: String name = XMLUtils.requireAttributeValue(element,
189: "name");
190: mEnv.getResourceGenerator().importResource(
191: file.getAbsolutePath(), name);
192: return;
193: } else {
194: // Program error: this shouldn't happen
195: throw new RuntimeException(
196: /* (non-Javadoc)
197: * @i18n.test
198: * @org-mes="Unknown resource tag: " + p[0]
199: */
200: org.openlaszlo.i18n.LaszloMessages.getMessage(
201: ResourceCompiler.class.getName(), "051018-188",
202: new Object[] { element.getName() }));
203: }
204: } catch (SWFWriter.ImportResourceError e) {
205: mEnv.warn(e, element);
206: return;
207: }
208: }
209: }
210:
211: /**
212: * @copyright Copyright 2001-2007 Laszlo Systems, Inc. All Rights
213: * Reserved. Use is subject to license terms.
214: */
|