LegacyConverter.java

  1. package gov.usgs.earthquake.eids;

  2. import java.io.InputStream;
  3. import java.util.Map;

  4. import gov.usgs.earthquake.cube.CubeMessage;
  5. import gov.usgs.earthquake.event.Converter;
  6. import gov.usgs.earthquake.product.Content;
  7. import gov.usgs.earthquake.product.Product;
  8. import gov.usgs.util.StreamUtils;

  9. import org.quakeml_1_2.Quakeml;
  10. import gov.usgs.ansseqmsg.EQMessage;

  11. /**
  12.  * Utility class to translate between Quakeml, EQXML, and CUBE formats.
  13.  */
  14. public class LegacyConverter {

  15.     /** Different format types */
  16.     public static enum Format {
  17.         /** Enum for Cube Format */
  18.         CUBE,
  19.         /** Enum for EQXML Format */
  20.         EQXML,
  21.         /** Enum for QUAKEML Format */
  22.         QUAKEML
  23.     };

  24.     /** Cube Format */
  25.     public static final Format CUBE = Format.CUBE;
  26.     /** EQXML Format */
  27.     public static final Format EQXML = Format.EQXML;
  28.     /** QUAKEML Format */
  29.     public static final Format QUAKEML = Format.QUAKEML;

  30.     /** Path to EQXML content */
  31.     public static final String EQXML_CONTENT_PATH = "eqxml.xml";
  32.     /** Path to Quakeml content */
  33.     public static final String QUAKEML_CONTENT_PATH = "quakeml.xml";

  34.     private final Format outputFormat;
  35.     private final Converter converter;

  36.     /**
  37.      * Constructor
  38.      * @param outputFormat format you want to switch to
  39.      */
  40.     public LegacyConverter(final Format outputFormat) {
  41.         this.outputFormat = outputFormat;
  42.         this.converter = new Converter();
  43.     }

  44.     /**
  45.      * @return converter that outputs cube.
  46.      */
  47.     public static LegacyConverter cubeConverter() {
  48.         return new LegacyConverter(CUBE);
  49.     }

  50.     /**
  51.      * @return converter that outputs eqxml.
  52.      */
  53.     public static LegacyConverter eqxmlConverter() {
  54.         return new LegacyConverter(EQXML);
  55.     }

  56.     /**
  57.      * @return converter that outputs quakeml.
  58.      */
  59.     public static LegacyConverter quakemlConverter() {
  60.         return new LegacyConverter(QUAKEML);
  61.     }

  62.     /**
  63.      * Handles conversion from a product containing either eqxml or quakeml
  64.      * contents to either eqxml, quakeml, or cube byte array.
  65.      *
  66.      * @param product
  67.      *            the product object to convert.
  68.      * @return byte array containing the output format, or null if unable to
  69.      *         convert.
  70.      * @throws Exception if error occurs
  71.      */
  72.     public byte[] convert(final Product product) throws Exception {
  73.         Map<String, Content> contents = product.getContents();
  74.         InputStream input = null;
  75.         try {
  76.             if (contents.containsKey(QUAKEML_CONTENT_PATH)) {
  77.                 input = contents.get(QUAKEML_CONTENT_PATH).getInputStream();
  78.                 return convert(converter.getQuakeml(input));
  79.             } else if (contents.containsKey(EQXML_CONTENT_PATH)) {
  80.                 input = contents.get(EQXML_CONTENT_PATH).getInputStream();
  81.                 return convert(converter.getEQMessage(input));
  82.             } else {
  83.                 // unable to convert
  84.                 return null;
  85.             }
  86.         } finally {
  87.             StreamUtils.closeStream(input);
  88.         }
  89.     }

  90.     /**
  91.      * Handles conversion from an eqxml to either eqxml, quakeml, or cube byte
  92.      * array.
  93.      *
  94.      * @param eqxml
  95.      *            the eqxml object to convert.
  96.      * @return byte array containing output format, or null if unable to
  97.      *         convert.
  98.      * @throws Exception if error occurs
  99.      */
  100.     public byte[] convert(EQMessage eqxml) throws Exception {
  101.         if (eqxml == null) {
  102.             return null;
  103.         }
  104.         try {
  105.             if (outputFormat == EQXML) {
  106.                 return converter.getString(eqxml).getBytes();
  107.             } else if (outputFormat == CUBE) {
  108.                 return converter.getString(converter.getCubeMessage(eqxml))
  109.                         .getBytes();
  110.             } else if (outputFormat == QUAKEML) {
  111.                 return converter.getString(converter.getQuakeml(eqxml))
  112.                         .getBytes();
  113.             } else {
  114.                 return null;
  115.             }
  116.         } catch (NullPointerException npe) {
  117.             return null;
  118.         }
  119.     }

  120.     /**
  121.      * Handles conversion from a quakeml message to either eqxml, quakeml, or
  122.      * cube byte array.
  123.      *
  124.      * @param quakeml
  125.      *            the quakeml object to convert.
  126.      * @return byte array containing output format, or null if unable to
  127.      *         convert.
  128.      * @throws Exception if error occurs
  129.      */
  130.     public byte[] convert(Quakeml quakeml) throws Exception {
  131.         if (quakeml == null) {
  132.             return null;
  133.         }
  134.         try {
  135.             if (outputFormat == EQXML) {
  136.                 return converter.getString(converter.getEQMessage(quakeml))
  137.                         .getBytes();
  138.             } else if (outputFormat == CUBE) {
  139.                 return converter.getString(converter.getCubeMessage(quakeml))
  140.                         .getBytes();
  141.             } else if (outputFormat == QUAKEML) {
  142.                 return converter.getString(quakeml).getBytes();
  143.             } else {
  144.                 return null;
  145.             }
  146.         } catch (NullPointerException npe) {
  147.             return null;
  148.         }
  149.     }

  150.     /**
  151.      * Handles conversion from a cube message to either eqxml, quakeml, or cube
  152.      * byte array.
  153.      *
  154.      * @param cube
  155.      *            the cube object to convert.
  156.      * @return byte array containing output format, or null if unable to
  157.      *         convert.
  158.      * @throws Exception if error occurs
  159.      */
  160.     public byte[] convert(CubeMessage cube) throws Exception {
  161.         if (cube == null) {
  162.             return null;
  163.         }
  164.         try {
  165.             if (outputFormat == EQXML) {
  166.                 return converter.getString(converter.getEQMessage(cube))
  167.                         .getBytes();
  168.             } else if (outputFormat == CUBE) {
  169.                 return converter.getString(cube).getBytes();
  170.             } else if (outputFormat == QUAKEML) {
  171.                 return converter.getString(converter.getQuakeml(cube))
  172.                         .getBytes();
  173.             } else {
  174.                 return null;
  175.             }
  176.         } catch (NullPointerException npe) {
  177.             return null;
  178.         }
  179.     }

  180. }