ProductTrackerParser.java

  1. package gov.usgs.earthquake.distribution;

  2. import gov.usgs.earthquake.product.ProductId;

  3. import gov.usgs.util.SAXAdapter;
  4. import gov.usgs.util.XmlUtils;

  5. import java.net.InetAddress;
  6. import java.net.URL;

  7. import java.util.Date;
  8. import java.util.LinkedList;
  9. import java.util.List;

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

  12. /**
  13.  * Parser for ProductTracker responses.
  14.  */
  15. public class ProductTrackerParser extends SAXAdapter {

  16.     /** The tracker that generated the list being parsed. */
  17.     private URL trackerURL;

  18.     /** A list of parsed updates. */
  19.     private List<ProductTrackerUpdate> updates = new LinkedList<ProductTrackerUpdate>();

  20.     /** The current update being parsed. */
  21.     private ProductTrackerUpdate update = null;

  22.     /**
  23.      * Create a new TrackerUpdateParser.
  24.      * @param trackerURL URL that generated the list being parsed
  25.      */
  26.     public ProductTrackerParser(final URL trackerURL) {
  27.         this.trackerURL = trackerURL;
  28.     }

  29.     /**
  30.      * Get the parsed updates.
  31.      * @return list of parsed updates
  32.      */
  33.     public List<ProductTrackerUpdate> getUpdates() {
  34.         return updates;
  35.     }

  36.     /**
  37.      * SAXAdapter start element handler.
  38.      *
  39.      * @param uri
  40.      *            element uri.
  41.      * @param localName
  42.      *            element localName.
  43.      * @param qName
  44.      *            element qName.
  45.      * @param attributes
  46.      *            element attributes.
  47.      * @throws SAXException
  48.      *             if there is an error.
  49.      */
  50.     public void onStartElement(final String uri, final String localName,
  51.             final String qName, final Attributes attributes)
  52.             throws SAXException {
  53.         if (localName.equals("update")) {
  54.             try {
  55.                 Long sequenceNumber = Long.valueOf(XmlUtils.getAttribute(
  56.                         attributes, uri, "sequenceNumber"));
  57.                 Date created = XmlUtils.getDate(XmlUtils.getAttribute(
  58.                         attributes, uri, "created"));
  59.                 InetAddress host = InetAddress.getByName(XmlUtils.getAttribute(
  60.                         attributes, uri, "host"));
  61.                 ProductId id = new ProductId(XmlUtils.getAttribute(attributes,
  62.                         uri, "source"), XmlUtils.getAttribute(attributes,
  63.                                 uri, "type"), XmlUtils.getAttribute(attributes, uri,
  64.                         "code"), XmlUtils.getDate(XmlUtils.getAttribute(
  65.                         attributes, uri, "updateTime")));
  66.                 String className = XmlUtils.getAttribute(attributes, uri,
  67.                         "className");
  68.                 update = new ProductTrackerUpdate(trackerURL, sequenceNumber,
  69.                         created, host, id, className, null);
  70.             } catch (Exception e) {
  71.                 throw new SAXException(e);
  72.             }
  73.         }
  74.     }

  75.     /**
  76.      * SAXAdapter end element handler. Content only includes characters that
  77.      * were read from this element, NOT any characters from child elements.
  78.      *
  79.      * @param uri
  80.      *            element uri.
  81.      * @param localName
  82.      *            element localName.
  83.      * @param qName
  84.      *            element qName.
  85.      * @param content
  86.      *            element content.
  87.      * @throws SAXException
  88.      *             if onEndElement throws a SAXException.
  89.      */
  90.     public void onEndElement(final String uri, final String localName,
  91.             final String qName, final String content) throws SAXException {
  92.         // message is element content
  93.         update.setMessage(content);
  94.         // add update to list of parsed updates
  95.         updates.add(update);
  96.         // reset update to null
  97.         update = null;
  98.     }

  99. }