All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.matrixweb.smaller.sweetjs.SweetjsProcessor Maven / Gradle / Ivy

package de.matrixweb.smaller.sweetjs;

import java.io.IOException;
import java.util.Map;

import de.matrixweb.smaller.common.SmallerException;
import de.matrixweb.nodejs.NodeJsExecutor;
import de.matrixweb.smaller.resource.MergingProcessor;
import de.matrixweb.smaller.resource.Resource;
import de.matrixweb.smaller.resource.Type;
import de.matrixweb.vfs.VFS;
import de.matrixweb.vfs.VFile;

/**
 *
 */
public class SweetjsProcessor implements MergingProcessor {

  private String version;

  private NodeJsExecutor node;

  /**
   *
   */
  public SweetjsProcessor() {
    this("0.2.5");
  }

  /**
   * @param version
   */
  public SweetjsProcessor(String version) {
    this.version = version;
  }

  /**
   * @see de.matrixweb.smaller.resource.Processor#supportsType(de.matrixweb.smaller.resource.Type)
   */
  @Override
  public boolean supportsType(final Type type) {
    return type == Type.JS;
  }

  /**
   * @see de.matrixweb.smaller.resource.Processor#execute(de.matrixweb.vfs.VFS,
   *      de.matrixweb.smaller.resource.Resource, java.util.Map)
   */
  @Override
  public Resource execute(final VFS vfs, final Resource resource, final Map options) throws IOException {
    if (this.node == null) {
      try {
        this.node = new NodeJsExecutor();
        this.node.setModule(getClass(), "sweetjs-" + this.version, null);
      } catch (final IOException e) {
        this.node = null;
        throw new SmallerException("Failed to setup node for sweetjs", e);
      }
    }
    String outfile = this.node.run(vfs, resource != null ? resource.getPath() : null, options);
    if (outfile != null) {
      final VFile file = vfs.find('/' + outfile);
      if (!file.exists()) {
        throw new SmallerException("SweetjsProcessor result does not exists");
      }
    }
    return resource == null || outfile == null ? resource : resource.getResolver().resolve('/' + outfile);
  }

  /**
   * @see de.matrixweb.smaller.resource.Processor#dispose()
   */
  @Override
  public void dispose() {
    if (this.node != null) {
      this.node.dispose();
      this.node = null;
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy