Esempio n. 1
0
  @Override
  public Collection<Throwable> call() throws Exception {

    if (this.formatString != null) {
      try {
        this.format = FORMAT.valueOf(this.formatString.toUpperCase());
      } catch (Exception e) {
        return wrapException(e);
      }
    }
    try {
      this.script = super.compileJavascript();
    } catch (Exception err) {
      return wrapException(err);
    }

    final List<String> args = getInputFiles();

    try {
      this.bindings = this.script.getEngine().createBindings();
      if (getOutputFile() == null) {
        this.writer = new PrintWriter(stdout());
      } else {
        this.writer = new PrintWriter(getOutputFile());
      }
      this.bindings.put("out", this.writer);

      if (args.isEmpty()) {
        if (this.format == null) {
          return wrapException("Format must be specified when reading from stdin");
        }
        return execute(this.format, null);
      }
      for (String filename : IOUtils.unrollFiles(args)) {
        this.bindings.put("FILENAME", filename);
        FORMAT fmt = this.format;
        if (fmt == null) {
          for (FORMAT t : FORMAT.values()) {
            if (t.canAs(filename)) {
              fmt = t;
              break;
            }
          }
        }
        if (fmt == null) {
          return wrapException("Cannot get file format for " + filename);
        }
        Collection<Throwable> errors = execute(fmt, filename);
        if (!errors.isEmpty()) {
          return errors;
        }
      }

      this.writer.flush();
      return RETURN_OK;
    } catch (Exception err) {
      return wrapException(err);
    } finally {
      if (this.writer != null) this.writer.flush();
      if (getOutputFile() != null) {
        CloserUtil.close(this.writer);
      }
      this.writer = null;
      bindings = null;
      this.bindings = null;
      this.format = null;
    }
  }