SearchRequestParser.java

  1. package gov.usgs.earthquake.indexer;

  2. import java.math.BigDecimal;

  3. import gov.usgs.earthquake.product.ProductId;
  4. import gov.usgs.earthquake.product.io.XmlProductHandler;
  5. import gov.usgs.util.XmlUtils;

  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.helpers.DefaultHandler;

  9. /**
  10.  * Parser for SearchXML request.
  11.  */
  12. public class SearchRequestParser extends DefaultHandler {

  13.     /** The request being parsed. */
  14.     private SearchRequest searchRequest;

  15.     /** The query being parsed. */
  16.     private SearchQuery searchQuery;

  17.     /** Empty constructor */
  18.     public SearchRequestParser() {
  19.     }

  20.     /** @return searchRequest */
  21.     public SearchRequest getSearchRequest() {
  22.         return searchRequest;
  23.     }

  24.     @Override
  25.     public void startElement(String uri, String localName, String qName,
  26.             Attributes attributes) throws SAXException {
  27.         if (SearchXML.INDEXER_XMLNS.equals(uri)) {
  28.             if (SearchXML.REQUEST_ELEMENT.equals(localName)) {
  29.                 searchRequest = new SearchRequest();
  30.             } else if (SearchXML.PRODUCT_SUMMARY_ELEMENT.equals(localName)) {
  31.                 if (searchQuery == null) {
  32.                     throw new SAXException(
  33.                             "Expected searchQuery element around productSummary element");
  34.                 } else {
  35.                     ProductId id = ProductId.parse(XmlUtils.getAttribute(
  36.                             attributes, uri, SearchXML.ID_ATTRIBUTE));
  37.                     if (id != null) {
  38.                         searchQuery.getProductIndexQuery().getProductIds()
  39.                                 .add(id);
  40.                     } else {
  41.                         throw new SAXException(
  42.                                 "Expected id attribute on productSummary element");
  43.                     }
  44.                 }
  45.             } else if (SearchXML.QUERY_ELEMENT.equals(localName)) {
  46.                 String method = XmlUtils.getAttribute(attributes,
  47.                         SearchXML.INDEXER_XMLNS, SearchXML.METHOD_ATTRIBUTE);
  48.                 String value;

  49.                 ProductIndexQuery query = new ProductIndexQuery();
  50.                 value = XmlUtils.getAttribute(attributes, uri,
  51.                         SearchXML.EVENT_SOURCE_ATTRIBUTE);
  52.                 query.setEventSource(value);

  53.                 value = XmlUtils.getAttribute(attributes, uri,
  54.                         SearchXML.EVENT_SOURCE_CODE_ATTRIBUTE);
  55.                 query.setEventSourceCode(value);

  56.                 value = XmlUtils.getAttribute(attributes, uri,
  57.                         SearchXML.MIN_EVENT_TIME_ATTRIBUTE);
  58.                 if (value != null) {
  59.                     query.setMinEventTime(XmlUtils.getDate(value));
  60.                 }

  61.                 value = XmlUtils.getAttribute(attributes, uri,
  62.                         SearchXML.MAX_EVENT_TIME_ATTRIBUTE);
  63.                 if (value != null) {
  64.                     query.setMaxEventTime(XmlUtils.getDate(value));
  65.                 }

  66.                 value = XmlUtils.getAttribute(attributes, uri,
  67.                         SearchXML.MIN_EVENT_LATITUDE_ATTRIBUTE);
  68.                 if (value != null) {
  69.                     query.setMinEventLatitude(new BigDecimal(value));
  70.                 }

  71.                 value = XmlUtils.getAttribute(attributes, uri,
  72.                         SearchXML.MAX_EVENT_LATITUDE_ATTRIBUTE);
  73.                 if (value != null) {
  74.                     query.setMaxEventLatitude(new BigDecimal(value));
  75.                 }

  76.                 value = XmlUtils.getAttribute(attributes, uri,
  77.                         SearchXML.MIN_EVENT_LONGITUDE_ATTRIBUTE);
  78.                 if (value != null) {
  79.                     query.setMinEventLongitude(new BigDecimal(value));
  80.                 }

  81.                 value = XmlUtils.getAttribute(attributes, uri,
  82.                         SearchXML.MAX_EVENT_LONGITUDE_ATTRIBUTE);
  83.                 if (value != null) {
  84.                     query.setMaxEventLongitude(new BigDecimal(value));
  85.                 }

  86.                 value = XmlUtils.getAttribute(attributes, uri,
  87.                         SearchXML.MIN_EVENT_DEPTH_ATTRIBUTE);
  88.                 if (value != null) {
  89.                     query.setMinEventDepth(new BigDecimal(value));
  90.                 }

  91.                 value = XmlUtils.getAttribute(attributes, uri,
  92.                         SearchXML.MAX_EVENT_DEPTH_ATTRIBUTE);
  93.                 if (value != null) {
  94.                     query.setMaxEventDepth(new BigDecimal(value));
  95.                 }

  96.                 value = XmlUtils.getAttribute(attributes, uri,
  97.                         SearchXML.MIN_EVENT_MAGNITUDE_ATTRIBUTE);
  98.                 if (value != null) {
  99.                     query.setMinEventMagnitude(new BigDecimal(value));
  100.                 }

  101.                 value = XmlUtils.getAttribute(attributes, uri,
  102.                         SearchXML.MAX_EVENT_MAGNITUDE_ATTRIBUTE);
  103.                 if (value != null) {
  104.                     query.setMaxEventMagnitude(new BigDecimal(value));
  105.                 }

  106.                 value = XmlUtils.getAttribute(attributes, uri,
  107.                         SearchXML.MIN_PRODUCT_UPDATE_TIME_ATTRIBUTE);
  108.                 if (value != null) {
  109.                     query.setMinProductUpdateTime(XmlUtils.getDate(value));
  110.                 }

  111.                 value = XmlUtils.getAttribute(attributes, uri,
  112.                         SearchXML.MAX_PRODUCT_UPDATE_TIME_ATTRIBUTE);
  113.                 if (value != null) {
  114.                     query.setMaxProductUpdateTime(XmlUtils.getDate(value));
  115.                 }

  116.                 value = XmlUtils.getAttribute(attributes, uri,
  117.                         SearchXML.PRODUCT_SOURCE_ATTRIBUTE);
  118.                 if (value != null) {
  119.                     query.setProductSource(value);
  120.                 }

  121.                 value = XmlUtils.getAttribute(attributes, uri,
  122.                         SearchXML.PRODUCT_TYPE_ATTRIBUTE);
  123.                 if (value != null) {
  124.                     query.setProductType(value);
  125.                 }

  126.                 value = XmlUtils.getAttribute(attributes, uri,
  127.                         SearchXML.PRODUCT_CODE_ATTRIBUTE);
  128.                 if (value != null) {
  129.                     query.setProductCode(value);
  130.                 }

  131.                 value = XmlUtils.getAttribute(attributes, uri,
  132.                         SearchXML.PRODUCT_VERSION_ATTRIBUTE);
  133.                 if (value != null) {
  134.                     query.setProductVersion(value);
  135.                 }

  136.                 value = XmlUtils.getAttribute(attributes, uri,
  137.                         SearchXML.PRODUCT_STATUS_ATTRIBUTE);
  138.                 if (value != null) {
  139.                     query.setProductStatus(value);
  140.                 }

  141.                 searchQuery = SearchQuery.getSearchQuery(
  142.                         SearchMethod.fromXmlMethodName(method), query);
  143.             }
  144.         } else if (XmlProductHandler.PRODUCT_XML_NAMESPACE.equals(uri)) {
  145.             // Possible inclusion of properties in the future
  146.         }
  147.     }

  148.     @Override
  149.     public void endElement(String uri, String localName, String qName)
  150.             throws SAXException {
  151.         if (SearchXML.INDEXER_XMLNS.equals(uri)) {
  152.             if (SearchXML.REQUEST_ELEMENT.equals(localName)) {
  153.                 // search request, done
  154.             } else if (SearchXML.QUERY_ELEMENT.equals(localName)) {
  155.                 searchRequest.addQuery(searchQuery);
  156.                 searchQuery = null;
  157.             }
  158.         }
  159.     }

  160.     @Override
  161.     public void characters(char[] ch, int start, int length)
  162.             throws SAXException {
  163.     }

  164. }