DirectoryProductHandler.java

  1. /*
  2.  * DirectoryProductHandler
  3.  */
  4. package gov.usgs.earthquake.product.io;

  5. import gov.usgs.earthquake.product.Content;
  6. import gov.usgs.earthquake.product.FileContent;
  7. import gov.usgs.earthquake.product.ProductId;
  8. import gov.usgs.earthquake.product.URLContent;
  9. import gov.usgs.util.StreamUtils;

  10. import java.io.File;
  11. import java.io.OutputStream;

  12. /**
  13.  * Store a product to a Directory.
  14.  *
  15.  * Product attributes are stored to a file named "product.xml". All
  16.  * ProductOutput methods are passed to an ObjectProductOutput object, except
  17.  * files with non-empty paths. Files are stored in the directory, and all other
  18.  * product attributes are stored using the product xml format to a file name
  19.  * "product.xml".
  20.  */
  21. public class DirectoryProductHandler extends ObjectProductHandler {

  22.     /** The file where product attributes are stored. */
  23.     public static final String PRODUCT_XML_FILENAME = "product.xml";

  24.     /** Directory where product contents are stored. */
  25.     private File directory;

  26.     /**
  27.      * Construct a new DirectoryProductHandler object.
  28.      *
  29.      * @param directory
  30.      *            where product contents will be stored.
  31.      */
  32.     public DirectoryProductHandler(final File directory) {
  33.         this.directory = directory;
  34.     }

  35.     /**
  36.      * Extract content when path isn't empty.
  37.      */
  38.     public void onContent(ProductId id, String path, Content content)
  39.             throws Exception {
  40.         if ("".equals(path)) {
  41.             super.onContent(id, path, content);
  42.         } else {
  43.             // FileContent copy constructor extracts content
  44.             FileContent fc = new FileContent(content, new File(directory, path));
  45.             super.onContent(id, path, new URLContent(fc));
  46.             fc = null;
  47.         }
  48.     }

  49.     /**
  50.      * Store all except product contents to product.xml.
  51.      */
  52.     public void onEndProduct(ProductId id) throws Exception {
  53.         super.onEndProduct(id);

  54.         // save reference to stream, so it can be forced close.
  55.         OutputStream out = null;
  56.         ProductSource source = null;
  57.         ProductHandler handler = null;
  58.         try {
  59.             out = StreamUtils.getOutputStream(new File(directory,
  60.                     PRODUCT_XML_FILENAME));

  61.             // save product attributes as xml
  62.             source = new ObjectProductSource(getProduct());
  63.             handler = new XmlProductHandler(out);
  64.             source.streamTo(handler);
  65.         } finally {
  66.             // close stream
  67.             StreamUtils.closeStream(out);
  68.             if (source != null) {
  69.                 source.close();
  70.             }
  71.             if (handler != null) {
  72.                 handler.close();
  73.             }
  74.         }
  75.     }

  76. }