RegionsHandler.java

  1. package gov.usgs.earthquake.qdm;

  2. import java.util.Date;
  3. import java.util.logging.Logger;

  4. import org.xml.sax.Attributes;
  5. import org.xml.sax.SAXException;

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


  8. /**
  9.  * XML SAX Handler for ANSS "regions.xml".
  10.  *
  11.  * See the resource file <code>etc/config/regions.xml</code>
  12.  *
  13.  * Example:
  14.  * <pre>{@code
  15.  * InputStream in = ...
  16.  * RegionsHanlder handler = new RegionsHandler();
  17.  * try {
  18.  *     handler.parse(in)
  19.  * } finally {
  20.  *     in.close();
  21.  * }
  22.  * return handler.regions;
  23.  * }</pre>
  24.  */
  25. public class RegionsHandler extends SAXAdapter {

  26.     /** Logger object */
  27.     public static final Logger LOGGER = Logger.getLogger(RegionsHandler.class.getName());

  28.     /** the regions that have been parsed */
  29.     public Regions regions = new Regions();
  30.     /** update timestamp */
  31.     public Date updated = null;
  32.     /** reported format version (no version-specific logic implemented) */
  33.     public String formatVersion = null;

  34.     // variables for tracking state
  35.     private boolean inRegions = false;
  36.     private String netid = null;
  37.     private Region region = null;

  38.     /**
  39.      * Start Element handler.
  40.      *
  41.      * Builds region objects, and parses other information.
  42.      *
  43.      * @param uri namespace of element.
  44.      * @param localName name of element.
  45.      * @param qName qualified name of element.
  46.      * @param attributes element attributes.
  47.      */
  48.     @Override
  49.     public void onStartElement(final String uri, final String localName,
  50.             final String qName, final Attributes attributes)
  51.             throws SAXException {
  52.         if ("regions".equals(localName)) {
  53.             this.inRegions = true;
  54.             LOGGER.finer("Parsing regions xml");
  55.         } else if (!inRegions) {
  56.             throw new SAXException("Expected 'regions' root element");
  57.         } else if ("update".equals(localName)) {
  58.             this.updated = XmlUtils.getDate(attributes.getValue("date"));
  59.             LOGGER.finer("\tupdated " + XmlUtils.formatDate(updated));
  60.         } else if ("format".equals(localName)) {
  61.             this.formatVersion = attributes.getValue("version");
  62.             LOGGER.finer("\tversion " + formatVersion);
  63.         } else if ("net".equals(localName)) {
  64.             if (this.region != null) {
  65.                 throw new SAXException("Unexpected 'net' element inside 'region'");
  66.             }
  67.             this.netid = attributes.getValue("code");
  68.             LOGGER.finer("\tnetid=" + netid);
  69.         } else if ("region".equals(localName)) {
  70.             if (this.region != null) {
  71.                 throw new SAXException("Unexpected 'region' element inside 'region'");
  72.             }
  73.             String regionid = attributes.getValue("code");
  74.             this.region = new Region(netid, regionid);
  75.             LOGGER.finer("\t\tregionid=" + regionid);
  76.         } else if ("coordinate".equals(localName)) {
  77.             if (region == null) {
  78.                 throw new SAXException("Unexpected 'coordinate' element outside 'region'");
  79.             }
  80.             Double latitude = Double.valueOf(attributes.getValue("latitude"));
  81.             Double longitude = Double.valueOf(attributes.getValue("longitude"));
  82.             this.region.points.add(new Point(longitude, latitude));
  83.             LOGGER.finer("\t\t\tcoordinate = " + latitude + ", " + longitude);
  84.         }
  85.     }

  86.     /**
  87.      * End element handler.
  88.      *
  89.      * Adds built region objects to regions object.
  90.      *
  91.      * @param uri namespace of element.
  92.      * @param localName name of element.
  93.      * @param qName qualified name of element.
  94.      * @param content element content.
  95.      */
  96.     @Override
  97.     public void onEndElement(final String uri, final String localName,
  98.             final String qName, final String content) throws SAXException {
  99.         if ("region".equals(localName)) {
  100.             if (this.region == null) {
  101.                 throw new SAXException("Unexpected closing 'region' element");
  102.             }
  103.             regions.regions.add(this.region);
  104.             if (this.region.points.size() == 0) {
  105.                 regions.defaultNetid = this.region.netid;
  106.             }
  107.             this.region = null;
  108.         } else if ("regions".equals(localName)) {
  109.             this.inRegions = false;
  110.         }
  111.     }

  112. }