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.      * @param shakemap a shakemap object parsed by handler
  58.      */
  59.     public StationlistXMLHandler(ShakeMap shakemap) {
  60.         this.shakemap = shakemap;
  61.     }

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

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

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

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

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

  123. }