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 the client that uses this processor.
  27.    */
  28.   public QWEmbeddedMsgProcessor(final QWEmbeddedClient client) {
  29.     this.client = client;
  30.   }

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

  52.     Element root = (Element) dataMsgElement.getChildren().get(0);
  53.     String rootElement = root.getName();
  54.     String rootNamespace = root.getNamespaceURI();

  55.     client.onEIDSMessage(new EIDSMessageEvent(client, msgNumObj, timeGenObj, fdrSourceHost, fdrSourceMsgNum,
  56.         rootNamespace, rootElement, getXML(root)));
  57.   }

  58.   /**
  59.    * Extract a JDOM attribute value.
  60.    *
  61.    * @param element a JDOM element
  62.    * @param name    the attribute name
  63.    * @return the attribute value, or null if not present.
  64.    */
  65.   public String getAttribute(final Element element, final String name) {
  66.     Attribute attribute = element.getAttribute(name);
  67.     String value = null;
  68.     if (attribute != null) {
  69.       value = attribute.getValue();
  70.     }
  71.     return value;
  72.   }

  73.   /**
  74.    * Serialize an element to an outputstream.
  75.    *
  76.    * @param element element to serialize.
  77.    * @param out     outputstream where serialized element is written.s
  78.    */
  79.   public static void writeXML(final Element element, final OutputStream out) {
  80.     try {
  81.       Document doc = new Document((Element) element.clone());
  82.       XMLOutputter outputter = new XMLOutputter();
  83.       outputter.output(doc, out);
  84.     } catch (IOException e) {
  85.       e.printStackTrace();
  86.     }
  87.   }

  88.   /**
  89.    * Serialize an element into a String.
  90.    *
  91.    * @param element element to serialize.
  92.    */
  93.   public static String getXML(final Element element) {
  94.     try {
  95.       ByteArrayOutputStream baos = new ByteArrayOutputStream();
  96.       writeXML(element, baos);
  97.       return baos.toString();
  98.     } catch (Exception e) {
  99.       e.printStackTrace();
  100.       return "";
  101.     }
  102.   }
  103. }