URLNotificationParser.java

  1. /*
  2.  * URLNotificationParser
  3.  */
  4. package gov.usgs.earthquake.distribution;

  5. import java.net.URL;
  6. import java.util.Date;

  7. import gov.usgs.earthquake.product.ProductId;
  8. import gov.usgs.util.SAXAdapter;
  9. import gov.usgs.util.XmlUtils;

  10. import org.xml.sax.Attributes;
  11. import org.xml.sax.SAXException;

  12. public class URLNotificationParser extends SAXAdapter {

  13.     /** Namespace for product XML */
  14.     public static final String PRODUCT_XML_NAMESPACE = "http://earthquake.usgs.gov/distribution/product";

  15.     /** static var for notification element */
  16.     public static final String NOTIFICATION_ELEMENT = "notification";
  17.     /** attribute for product id */
  18.     public static final String ATTRIBUTE_PRODUCT_ID = "id";
  19.     /** attribute for updated */
  20.     public static final String ATTRIBUTE_PRODUCT_UPDATED = "updated";
  21.     /** attribute for trackerURL */
  22.     public static final String ATTRIBUTE_TRACKER_URL = "trackerURL";
  23.     /** attribute for expires */
  24.     public static final String ATTRIBUTE_EXPIRES = "expires";
  25.     /** attribute for url */
  26.     public static final String ATTRIBUTE_URL = "url";

  27.     /** The parsed notification. */
  28.     private URLNotification notification;

  29.     /**
  30.      * Construct a URLNotificationParser. This class is not intended to be
  31.      * instantiated directly. Instead, use the static URLNotification.parse
  32.      * method.
  33.      */
  34.     protected URLNotificationParser() {
  35.     }

  36.     /**
  37.      * @return the parsed notification
  38.      */
  39.     public URLNotification getNotification() {
  40.         return notification;
  41.     }

  42.     /**
  43.      * SAXAdapter start element handler.
  44.      *
  45.      * @param uri
  46.      *            element uri.
  47.      * @param localName
  48.      *            element localName.
  49.      * @param qName
  50.      *            element qName.
  51.      * @param attributes
  52.      *            element attributes.
  53.      * @throws SAXException
  54.      *             if there is an error.
  55.      */
  56.     public void onStartElement(final String uri, final String localName,
  57.             final String qName, final Attributes attributes)
  58.             throws SAXException {

  59.         if (!uri.equals(PRODUCT_XML_NAMESPACE)) {
  60.             return;
  61.         }

  62.         if (localName.equals(NOTIFICATION_ELEMENT)) {
  63.             ProductId id = ProductId.parse(XmlUtils.getAttribute(
  64.                     attributes, uri, ATTRIBUTE_PRODUCT_ID));
  65.             id.setUpdateTime(XmlUtils.getDate(XmlUtils.getAttribute(
  66.                     attributes, uri, ATTRIBUTE_PRODUCT_UPDATED)));

  67.             URL trackerURL = null;
  68.             try {
  69.                 trackerURL = new URL(XmlUtils.getAttribute(attributes, uri,
  70.                         ATTRIBUTE_TRACKER_URL));
  71.             } catch (Exception e) {
  72.                 throw new SAXException("Unable to parse tracker url", e);
  73.             }

  74.             Date expirationDate = XmlUtils.getDate(XmlUtils.getAttribute(
  75.                     attributes, uri, ATTRIBUTE_EXPIRES));

  76.             URL productURL = null;
  77.             try {
  78.                 productURL = new URL(XmlUtils.getAttribute(attributes, uri,
  79.                         ATTRIBUTE_URL));
  80.             } catch (Exception e) {
  81.                 throw new SAXException("Unable to parse product url", e);
  82.             }

  83.             notification = new URLNotification(id, expirationDate,
  84.                     trackerURL, productURL);
  85.         }
  86.     }

  87. }