EventDataXMLHandler.java

  1. package gov.usgs.earthquake.dyfi;

  2. import gov.usgs.util.XmlUtils;

  3. import java.util.HashMap;
  4. import java.util.Map;

  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.SAXException;
  7. import org.xml.sax.helpers.DefaultHandler;

  8. /**
  9.  * Parser for DYFI "eventdata.xml" metadata.
  10.  */
  11. public class EventDataXMLHandler extends DefaultHandler {

  12.     /** XML Element Name for event_data */
  13.     public static final String DYFI_EVENTDATA_ELEMENT = "event_data";
  14.     /** XML Element Name  for event */
  15.     public static final String DYFI_EVENT_ELEMENT = "event";
  16.     /** XML Element Name for cdi_summary */
  17.     public static final String DYFI_CDI_SUMMARY_ELEMENT = "cdi_summary";
  18.     /** XML Element Name for products */
  19.     public static final String DYFI_PRODUCTS_ELEMENT = "products";

  20.     /** Static string to stop parsing before list of products */
  21.     public static final String DYFI_STOP_PARSING_BEFORE_PRODUCTS = "Stop parsing before list of product files.";

  22.     /** Map of XML attributes */
  23.     public static final Map<String, String[]> DYFI_ELEMENT_ATTRIBUTES = new HashMap<String, String[]>();
  24.     static {
  25.         // Statically add all these attributes and associate them to their
  26.         // corresponding elements

  27.         // Currently we only care about the max MMI and number of responses.
  28.         DYFI_ELEMENT_ATTRIBUTES.put(DYFI_CDI_SUMMARY_ELEMENT, new String[] {
  29.                 DYFIProduct.DYFI_NUM_RESP_PROPERTY,
  30.                 DYFIProduct.DYFI_MAX_MMI_PROPERTY });
  31.     }

  32.     private DYFIProduct dyfi = null;

  33.     /**
  34.      * Constructor
  35.      * @param dyfi takes in DYFIProduct
  36.      */
  37.     public EventDataXMLHandler(final DYFIProduct dyfi) {
  38.         this.dyfi = dyfi;
  39.     }

  40.     /** @return DYFIProduct */
  41.     public DYFIProduct getDYFI() {
  42.         return this.dyfi;
  43.     }

  44.     /** @param dyfi Product to set */
  45.     public void setDYFI(final DYFIProduct dyfi) {
  46.         this.dyfi = dyfi;
  47.     }

  48.     /**
  49.      *
  50.      * @param in XML object to parse
  51.      * @return DYFIProduct
  52.      * @throws Exception if exception message equals stop_parsing string
  53.      */
  54.     public DYFIProduct parse(final Object in) throws Exception {
  55.         try {
  56.             XmlUtils.parse(in, this);
  57.         } catch (Exception e) {
  58.             if (!DYFI_STOP_PARSING_BEFORE_PRODUCTS.equals(e.getMessage())) {
  59.                 throw e;
  60.             }
  61.         }
  62.         return getDYFI();
  63.     }

  64.     public final void startElement(final String uri, final String localName,
  65.             final String qName, final Attributes attributes)
  66.             throws SAXException {

  67.         if (localName != null && DYFI_PRODUCTS_ELEMENT.equals(localName)) {
  68.             // We don't need the list of product files at this time.
  69.             throw new SAXException(DYFI_STOP_PARSING_BEFORE_PRODUCTS);
  70.         }

  71.         if (DYFI_CDI_SUMMARY_ELEMENT.equals(localName)) {
  72.             dyfi.setNumResponses(attributes
  73.                     .getValue(DYFIProduct.DYFI_NUM_RESP_ATTRIBUTE));
  74.             dyfi.setMaxMMI(attributes
  75.                     .getValue(DYFIProduct.DYFI_MAX_MMI_ATTRIBUTE));
  76.         }
  77.     }
  78. }