DirectoryProductHandler.java

/*
 * DirectoryProductHandler
 */
package gov.usgs.earthquake.product.io;

import gov.usgs.earthquake.product.Content;
import gov.usgs.earthquake.product.FileContent;
import gov.usgs.earthquake.product.ProductId;
import gov.usgs.earthquake.product.URLContent;

import java.io.File;

/**
 * Base class for storing a product to a Directory.
 * 
 */
public abstract class DirectoryProductHandler extends ObjectProductHandler {

  /** Directory where product contents are stored. */
  protected File directory;

  /**
   * Construct a new DirectoryProductHandler object.
   *
   * @param directory where product contents will be stored.
   */
  public DirectoryProductHandler(final File directory) {
    this.directory = directory;
  }

  /**
   * Extract content when path isn't empty.
   */
  public void onContent(ProductId id, String path, Content content) throws Exception {
    if ("".equals(path)) {
      super.onContent(id, path, content);
    } else {
      // FileContent copy constructor extracts content
      FileContent fc = new FileContent(content, new File(directory, path));
      super.onContent(id, path, new URLContent(fc));
      fc = null;
    }
  }

  public void onEndProduct(ProductId id) throws Exception {
    super.onEndProduct(id);
  }

}