EventAddonParser.java

  1. /*
  2.  * EventAddonProductParser
  3.  */
  4. package gov.usgs.earthquake.eids;

  5. import java.util.Date;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;

  8. import org.xml.sax.Attributes;
  9. import org.xml.sax.SAXException;

  10. import gov.usgs.ansseqmsg.Action;
  11. import gov.usgs.ansseqmsg.Comment;
  12. import gov.usgs.ansseqmsg.EQMessage;
  13. import gov.usgs.ansseqmsg.Event;
  14. import gov.usgs.ansseqmsg.ProductLink;
  15. import gov.usgs.earthquake.eidsutil.EIDSMessageEvent;
  16. import gov.usgs.util.SAXAdapter;
  17. import gov.usgs.util.XmlUtils;

  18. /**
  19.  * Parser for event addon messages.
  20.  *
  21.  * Maps these messages into an EQMessage with a
  22.  * product link.
  23.  */
  24. public class EventAddonParser extends SAXAdapter {

  25.     /** Date format used in event addon message. */
  26.     public static final SimpleDateFormat ADDON_DATE_FORMAT = new SimpleDateFormat(
  27.             "yyyy/MM/dd_HH:mm:ss");

  28.     /** The parsed addon object. */
  29.     private EventAddon addon;

  30.     /**
  31.      * Takes a EIDSMessage event and returns the EQMessage
  32.      * @param event EIDSMessageEvent to parse
  33.      * @return an EQmessage
  34.      * @throws Exception if error occurs
  35.      */
  36.     public EQMessage parseMessage(EIDSMessageEvent event) throws Exception {
  37.         // create a parser object for this message
  38.         EventAddonParser parser = new EventAddonParser();

  39.         try {
  40.             // parse the message using the parser
  41.             XmlUtils.parse(event.getMessage(), parser);
  42.             // get the parsed addon
  43.             EventAddon addon = parser.getAddon();
  44.             // convert the parsed addon to an EQMessage
  45.             return addon.getEQMessage();
  46.         } catch (SAXException e) {
  47.             if (!(e.getCause() instanceof ParseException)) {
  48.                 throw e;
  49.             }
  50.         }

  51.         return null;
  52.     }

  53.     /**
  54.      * Data structure for event addon message. Also performs mapping onto
  55.      * EQMessage,Event, and ProductLink or Comment elements.
  56.      */
  57.     public static class EventAddon {

  58.         /** fileName */
  59.         public String fileName;
  60.         /** Submitter */
  61.         public String submitter;
  62.         /** Submit time */
  63.         public Date submitTime;
  64.         /** email */
  65.         public String email;
  66.         /** event id */
  67.         public String eventid;
  68.         /** type */
  69.         public String type;
  70.         /** version */
  71.         public String version;
  72.         /** action */
  73.         public String action;
  74.         /** description */
  75.         public String description;
  76.         /** link */
  77.         public String link;
  78.         /** text */
  79.         public String text;

  80.         /** Constructor */
  81.         public EventAddon() {
  82.         }

  83.         /**
  84.          * Parse eventaddon xml into a ProductLink.
  85.          *
  86.          * @return null, if there is no link.
  87.          */
  88.         public ProductLink getProductLink() {
  89.             if (this.link == null) {
  90.                 return null;
  91.             }

  92.             Action plAction = Action.UPDATE;
  93.             if (action.equalsIgnoreCase("DEL")) {
  94.                 plAction = Action.DELETE;
  95.             }

  96.             ProductLink link = new ProductLink();
  97.             link.setSourceKey(submitter);
  98.             link.setTypeKey("LinkURL");
  99.             link.setAction(plAction);
  100.             link.setCode(type);
  101.             link.setLink(this.link);
  102.             link.setNote(text);
  103.             link.setVersion(version);

  104.             return link;
  105.         }

  106.         /**
  107.          * Parse eventaddon xml into a Comment.
  108.          *
  109.          * @return null, if there is a link.
  110.          */
  111.         public Comment getComment() {
  112.             if (this.link != null) {
  113.                 return null;
  114.             }

  115.             Action cAction = Action.UPDATE;
  116.             if (action.equalsIgnoreCase("DEL")) {
  117.                 cAction = Action.DELETE;
  118.             }

  119.             Comment comment = new Comment();
  120.             comment.setSourceKey(submitter);
  121.             comment.setTypeKey(type);
  122.             comment.setAction(cAction);
  123.             comment.setText(text);
  124.             comment.setVersion(version);

  125.             return comment;
  126.         }

  127.         /** @return event */
  128.         public Event getEvent() {
  129.             Event event = new Event();
  130.             event.setDataSource(eventid.substring(0, 2));
  131.             event.setEventID(eventid.substring(2));

  132.             ProductLink link = getProductLink();
  133.             if (link != null) {
  134.                 event.getProductLink().add(getProductLink());
  135.             } else {
  136.                 Comment comment = getComment();
  137.                 event.getComment().add(comment);
  138.             }

  139.             return event;
  140.         }

  141.         /** @return EQMessage */
  142.         public EQMessage getEQMessage() {
  143.             EQMessage message = new EQMessage();
  144.             message.setSent(submitTime);
  145.             message.setSource(submitter);
  146.             message.getEvent().add(getEvent());
  147.             return message;
  148.         }
  149.     }

  150.     /**
  151.      * Get parsed addon.
  152.      *
  153.      * @return parsed addon, or null if nothing parsed.
  154.      */
  155.     public EventAddon getAddon() {
  156.         return addon;
  157.     }

  158.     /**
  159.      * SAXAdapter start element handler.
  160.      *
  161.      * @param uri
  162.      *            element uri.
  163.      * @param localName
  164.      *            element localName.
  165.      * @param qName
  166.      *            element qName.
  167.      * @param attributes
  168.      *            element attributes.
  169.      * @throws SAXException
  170.      *             if there is an error.
  171.      */
  172.     public void onStartElement(final String uri, final String localName,
  173.             final String qName, final Attributes attributes)
  174.             throws SAXException {

  175.         if (localName.equals("eventaddon")) {
  176.             addon = new EventAddon();
  177.         } else if (localName.equals("file")) {
  178.             addon.fileName = attributes.getValue("name");
  179.         }
  180.     }

  181.     /**
  182.      * SAXAdapter end element handler. Content only includes characters that
  183.      * were read from this element, NOT any characters from child elements.
  184.      *
  185.      * @param uri
  186.      *            element uri.
  187.      * @param localName
  188.      *            element localName.
  189.      * @param qName
  190.      *            element qName.
  191.      * @param content
  192.      *            element content.
  193.      * @throws SAXException
  194.      *             if onEndElement throws a SAXException.
  195.      */
  196.     public void onEndElement(final String uri, final String localName,
  197.             final String qName, final String content) throws SAXException {

  198.         if (localName.equals("submitter")) {
  199.             addon.submitter = content;
  200.         } else if (localName.equals("email")) {
  201.             addon.email = content;
  202.         } else if (localName.equals("eventid")) {
  203.             addon.eventid = content;
  204.         } else if (localName.equals("type")) {
  205.             addon.type = content;
  206.         } else if (localName.equals("version")) {
  207.             addon.version = content;
  208.         } else if (localName.equals("action")) {
  209.             addon.action = content;
  210.         } else if (localName.equals("description")) {
  211.             addon.description = content;
  212.         } else if (localName.equals("link")) {
  213.             addon.link = content;
  214.         } else if (localName.equals("text")) {
  215.             addon.text = content;
  216.         } else if (localName.equals("submit_time")) {
  217.             try {
  218.                 addon.submitTime = ADDON_DATE_FORMAT.parse(content);
  219.             } catch (ParseException e) {
  220.                 throw new SAXException(e);
  221.             }
  222.         }
  223.     }

  224. }