QWEmbeddedMsgProcessor.java

  1. /*
  2.  * EIDSMsgProcessor
  3.  */
  4. package gov.usgs.earthquake.eidsutil;

  5. import com.isti.quakewatch.message.MsgProcessorInterface;

  6. import org.jdom.Attribute;
  7. import org.jdom.Document;
  8. import org.jdom.Element;
  9. import org.jdom.output.XMLOutputter;

  10. import java.io.ByteArrayOutputStream;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.util.Date;

  14. /**
  15.  * Adapts the ISTI MsgProcessorInterface for EIDSClient. When the EIDSClient
  16.  * receives a message, the processDataMessage method is invoked.
  17.  *
  18.  * @see EIDSClient
  19.  */
  20. class QWEmbeddedMsgProcessor implements MsgProcessorInterface {

  21.     /** The client to notify when messages are available. */
  22.     private QWEmbeddedClient client;

  23.     /**
  24.      * Construct a new EIDSMsgProcessor.
  25.      *
  26.      * @param client
  27.      *            the client that uses this processor.
  28.      */
  29.     public QWEmbeddedMsgProcessor(final QWEmbeddedClient client) {
  30.         this.client = client;
  31.     }

  32.     /**
  33.      * Convert QWmessages to QWMsgRecords. Override the notifyMessage method to
  34.      * further process messages before sending to listeners.
  35.      *
  36.      * @param qwMsgElement
  37.      *            The "QWmessage" element.
  38.      * @param dataMsgElement
  39.      *            The "DataMessage" element.
  40.      * @param xmlMsgStr
  41.      *            the XML text message string.
  42.      * @param requestedFlag
  43.      *            true to indicate the the message was "requested" (and that it
  44.      *            should not be processed as a "real-time" message).
  45.      * @param msgNumObj
  46.      *            a 'Long' object holding the message number for the message, or
  47.      *            null if a message number is not available.
  48.      * @param timeGenObj
  49.      *            a 'Date' object holding the time-generated value for the
  50.      *            message, or null if a time-generated value is not available.
  51.      */
  52.     public void processDataMessage(Element qwMsgElement,
  53.             Element dataMsgElement, String xmlMsgStr, boolean requestedFlag,
  54.             Long msgNumObj, Date timeGenObj) {
  55.         // extract unique message id
  56.         String fdrSourceHost = getAttribute(qwMsgElement, "FdrSourceHost");
  57.         Long fdrSourceMsgNum = Long.valueOf(getAttribute(qwMsgElement,
  58.                 "FdrSourceMsgNum"));

  59.         Element root = (Element) dataMsgElement.getChildren().get(0);
  60.         String rootElement = root.getName();
  61.         String rootNamespace = root.getNamespaceURI();

  62.         client.onEIDSMessage(new EIDSMessageEvent(client, msgNumObj,
  63.                 timeGenObj, fdrSourceHost, fdrSourceMsgNum, rootNamespace,
  64.                 rootElement, getXML(root)));
  65.     }

  66.     /**
  67.      * Extract a JDOM attribute value.
  68.      *
  69.      * @param element
  70.      *            a JDOM element
  71.      * @param name
  72.      *            the attribute name
  73.      * @return the attribute value, or null if not present.
  74.      */
  75.     public String getAttribute(final Element element, final String name) {
  76.         Attribute attribute = element.getAttribute(name);
  77.         String value = null;
  78.         if (attribute != null) {
  79.             value = attribute.getValue();
  80.         }
  81.         return value;
  82.     }

  83.     /**
  84.      * Serialize an element to an outputstream.
  85.      *
  86.      * @param element
  87.      *            element to serialize.
  88.      * @param out
  89.      *            outputstream where serialized element is written.s
  90.      */
  91.     public static void writeXML(final Element element, final OutputStream out) {
  92.         try {
  93.             Document doc = new Document((Element) element.clone());
  94.             XMLOutputter outputter = new XMLOutputter();
  95.             outputter.output(doc, out);
  96.         } catch (IOException e) {
  97.             e.printStackTrace();
  98.         }
  99.     }

  100.     /**
  101.      * Serialize an element into a String.
  102.      *
  103.      * @param element
  104.      *            element to serialize.
  105.      */
  106.     public static String getXML(final Element element) {
  107.         try {
  108.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  109.             writeXML(element, baos);
  110.             return baos.toString();
  111.         } catch (Exception e) {
  112.             e.printStackTrace();
  113.             return "";
  114.         }
  115.     }
  116. }