public void compile(Element element) {
    if (!element.getChildren().isEmpty()) {
      throw new CompilationError("<script> elements can't have children", element);
    }
    String pathname = null;
    String script = element.getText();
    if (element.getAttribute(SRC_ATTR_NAME) != null) {
      pathname = element.getAttributeValue(SRC_ATTR_NAME);
      File file = mEnv.resolveReference(element, SRC_ATTR_NAME);
      try {
        script =
            "#file "
                + element.getAttributeValue(SRC_ATTR_NAME)
                + "\n"
                + "#line 1\n"
                + FileUtils.readFileString(file);
      } catch (IOException e) {
        throw new CompilationError(e);
      }
    }
    try {
      // If it is when=immediate, emit code inline
      if ("immediate".equals(element.getAttributeValue("when"))) {
        mEnv.compileScript(
            CompilerUtils.sourceLocationDirective(element, true)
                + script
                + CompilerUtils.endSourceLocationDirective,
            element);
      } else {
        // Compile scripts to run at construction time in the view
        // instantiation queue.

        mEnv.compileScript(
            // Provide file info for anonymous function name
            CompilerUtils.sourceLocationDirective(element, true)
                + VIEW_INSTANTIATION_FNAME
                + "({'class': lz.script, attrs: "
                + "{script: function () {\n"
                + "#beginContent\n"
                + "#pragma 'scriptElement'\n"
                + CompilerUtils.sourceLocationDirective(element, true)
                + script
                + CompilerUtils.endSourceLocationDirective
                + "\n#endContent\n"
                +
                // Scripts have no children
                "}}}, 1)",
            element);
      }
    } catch (CompilationError e) {
      // TODO: [2003-01-16] Instead of this, put the filename in ParseException,
      // and modify CompilationError.initElement to copy it from there.
      if (pathname != null) {
        e.setPathname(pathname);
      }
      throw e;
    }
  }