URLNotificationXMLConverter.java

  1. package gov.usgs.earthquake.distribution;

  2. import gov.usgs.util.XmlUtils;

  3. import java.io.InputStream;

  4. public class URLNotificationXMLConverter {

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

  7.   /** static var for notification element */
  8.   public static final String NOTIFICATION_ELEMENT = "notification";
  9.   /** attribute for product id */
  10.   public static final String ATTRIBUTE_PRODUCT_ID = "id";
  11.   /** attribute for updated */
  12.   public static final String ATTRIBUTE_PRODUCT_UPDATED = "updated";
  13.   /** attribute for trackerURL */
  14.   public static final String ATTRIBUTE_TRACKER_URL = "trackerURL";
  15.   /** attribute for expires */
  16.   public static final String ATTRIBUTE_EXPIRES = "expires";
  17.   /** attribute for url */
  18.   public static final String ATTRIBUTE_URL = "url";

  19.   /**
  20.    * Converts a URLNotification to XML
  21.    *
  22.    * @param notification
  23.    *                  The URLNotification to be converted
  24.    * @return an XML-formatted string
  25.    */
  26.   public static String toXML(final URLNotification notification) {
  27.     StringBuffer buf = new StringBuffer();

  28.     buf.append("<?xml version=\"1.0\"?>\n");
  29.     // start element
  30.     buf.append("<").append(NOTIFICATION_ELEMENT);
  31.     buf.append(" xmlns=\"").append(
  32.             PRODUCT_XML_NAMESPACE).append("\"");

  33.     // add attributes
  34.     buf.append(" ").append(ATTRIBUTE_PRODUCT_ID)
  35.             .append("=\"").append(notification.getProductId().toString()).append(
  36.             "\"");
  37.     buf
  38.             .append(" ")
  39.             .append(ATTRIBUTE_PRODUCT_UPDATED)
  40.             .append("=\"")
  41.             .append(
  42.                     XmlUtils
  43.                             .formatDate(notification.getProductId().getUpdateTime()))
  44.             .append("\"");
  45.     buf.append(" ").append(ATTRIBUTE_TRACKER_URL)
  46.             .append("=\"").append(notification.getTrackerURL().toString()).append(
  47.             "\"");
  48.     buf.append(" ").append(ATTRIBUTE_EXPIRES).append(
  49.             "=\"").append(XmlUtils.formatDate(notification.getExpirationDate()))
  50.             .append("\"");
  51.     buf.append(" ").append(ATTRIBUTE_URL).append(
  52.             "=\"").append(notification.getProductURL().toString()).append("\"");

  53.     // end element
  54.     buf.append("/>");

  55.     return buf.toString();
  56.   }

  57.   /**
  58.    * Parses an XML message into a URLNotification
  59.    *
  60.    * @param message
  61.    *             The convertee
  62.    *
  63.    * @return A parsed URL notification
  64.    * @throws Exception If parse goes wrong
  65.    */
  66.   public static URLNotification parseXML(final InputStream message) throws Exception{
  67.     URLNotificationParser parser = new URLNotificationParser();
  68.     parser.parse(message);
  69.     return parser.getNotification();
  70.   }

  71. }