StationlistXMLHandler.java

  1. /**
  2.  * This class is not currently in use, but kept for posterity and the ability
  3.  * to add this support back in if it is deemed necessary.
  4.  */
  5. package gov.usgs.earthquake.shakemap;

  6. import gov.usgs.util.StringUtils;
  7. import gov.usgs.util.XmlUtils;

  8. import java.math.BigDecimal;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.TimeZone;

  12. import org.xml.sax.Attributes;
  13. import org.xml.sax.SAXException;
  14. import org.xml.sax.helpers.DefaultHandler;

  15. /**
  16.  * Parser for Shakemap "stationlist.xml" metadata.
  17.  */
  18. public class StationlistXMLHandler extends DefaultHandler {

  19.   /** Element for Shakemap data */
  20.   public static final String SHAKEMAPDATA_ELEMENT = "shakemap-data";
  21.   /** Shakemap data version */
  22.   public static final String SHAKEMAPDATA_VERSION = "map_version";

  23.   /** Element for earthquake */
  24.   public static final String EARTHQUAKE_ELEMENT = "earthquake";
  25.   /** String for earthquake id */
  26.   public static final String EARTHQUAKE_ID = "id";
  27.   /** String for earthquake latitiude */
  28.   public static final String EARTHQUAKE_LAT = "lat";
  29.   /** String for earthquake longitude */
  30.   public static final String EARTHQUAKE_LON = "lon";
  31.   /** String for earthquake magnitude */
  32.   public static final String EARTHQUAKE_MAG = "mag";
  33.   /** String for earthquake year */
  34.   public static final String EARTHQUAKE_YEAR = "year";
  35.   /** String for earthquake month */
  36.   public static final String EARTHQUAKE_MONTH = "month";
  37.   /** String for earthquake day */
  38.   public static final String EARTHQUAKE_DAY = "day";
  39.   /** String for earthquake hour */
  40.   public static final String EARTHQUAKE_HOUR = "hour";
  41.   /** String for earthquake minute */
  42.   public static final String EARTHQUAKE_MINUTE = "minute";
  43.   /** String for earthquake second */
  44.   public static final String EARTHQUAKE_SECOND = "second";
  45.   /** String for earthquake timezone */
  46.   public static final String EARTHQUAKE_TIMEZONE = "timezone";
  47.   /** String for earthquake depth */
  48.   public static final String EARTHQUAKE_DEPTH = "depth";
  49.   /** String for earthquake locstring */
  50.   public static final String EARTHQUAKE_LOCSTRING = "locstring";
  51.   /** String for earthquake created */
  52.   public static final String EARTHQUAKE_CREATED = "created";

  53.   /** The ShakeMap object parsed by this handler. */
  54.   private ShakeMap shakemap;

  55.   /**
  56.    * Constructor
  57.    *
  58.    * @param shakemap a shakemap object parsed by handler
  59.    */
  60.   public StationlistXMLHandler(ShakeMap shakemap) {
  61.     this.shakemap = shakemap;
  62.   }

  63.   /** @return shakemap */
  64.   public ShakeMap getShakemap() {
  65.     return shakemap;
  66.   }

  67.   /** @param shakemap to set */
  68.   public void setShakemap(ShakeMap shakemap) {
  69.     this.shakemap = shakemap;
  70.   }

  71.   /**
  72.    * Takes in an XML object and parses it
  73.    *
  74.    * @param in an object
  75.    * @return A shakemap
  76.    * @throws Exception if error occurs
  77.    */
  78.   public ShakeMap parse(final Object in) throws Exception {
  79.     XmlUtils.parse(in, this);
  80.     return getShakemap();
  81.   }

  82.   /**
  83.    * Parse element attributes.
  84.    *
  85.    * @param uri        element namespace.
  86.    * @param localName  element name.
  87.    * @param qName      qualified element name.
  88.    * @param attributes element attributes.
  89.    * @throws SAXException if error occurs
  90.    */
  91.   public final void startElement(final String uri, final String localName, final String qName,
  92.       final Attributes attributes) throws SAXException {

  93.     try {
  94.       if (localName.equals(SHAKEMAPDATA_ELEMENT)) {
  95.         shakemap.setVersion(attributes.getValue(SHAKEMAPDATA_VERSION));
  96.       } else if (localName.equals(EARTHQUAKE_ELEMENT)) {
  97.         shakemap.setLatitude(new BigDecimal(attributes.getValue(EARTHQUAKE_LAT)));
  98.         shakemap.setLongitude(new BigDecimal(attributes.getValue(EARTHQUAKE_LON)));
  99.         shakemap.setMagnitude(new BigDecimal(attributes.getValue(EARTHQUAKE_MAG)));
  100.         shakemap.setDepth(new BigDecimal(attributes.getValue(EARTHQUAKE_DEPTH)));
  101.         Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(attributes.getValue(EARTHQUAKE_TIMEZONE)));
  102.         cal.set(Integer.parseInt(attributes.getValue(EARTHQUAKE_YEAR)),
  103.             Integer.parseInt(attributes.getValue(EARTHQUAKE_MONTH)),
  104.             Integer.parseInt(attributes.getValue(EARTHQUAKE_DAY)),
  105.             Integer.parseInt(attributes.getValue(EARTHQUAKE_HOUR)),
  106.             Integer.parseInt(attributes.getValue(EARTHQUAKE_MINUTE)),
  107.             Integer.parseInt(attributes.getValue(EARTHQUAKE_SECOND)));
  108.         shakemap.setEventTime(cal.getTime());
  109.         shakemap.setEventDescription(attributes.getValue(EARTHQUAKE_LOCSTRING));
  110.         shakemap.setProcessTimestamp(new Date(StringUtils.getLong(attributes.getValue(EARTHQUAKE_CREATED))));
  111.       }
  112.     } catch (Exception e) {
  113.       throw new SAXException(e);
  114.     }
  115.   }

  116. }