InfoXMLHandler.java

  1. package gov.usgs.earthquake.shakemap;

  2. import gov.usgs.util.XmlUtils;

  3. import java.util.HashMap;

  4. import org.xml.sax.Attributes;
  5. import org.xml.sax.helpers.DefaultHandler;

  6. /**
  7.  * Parser for ShakeMap info.xml metadata.
  8.  */
  9. public class InfoXMLHandler extends DefaultHandler {

  10.     private static final String XML_TAG = "tag";
  11.     private static final String XML_NAME = "name";
  12.     private static final String XML_VALUE = "value";
  13.     private HashMap<String, String> info = new HashMap<String, String>();

  14.     /**
  15.      * Construct a new SAX Handler for an info.xml document.
  16.      */
  17.     public InfoXMLHandler() {
  18.     }

  19.     /**
  20.      * @param in
  21.      *            - the file or stream to parse
  22.      * @return the ShakeMap associated with this XML handler
  23.      * @throws Exception if error occurs
  24.      */
  25.     public HashMap<String, String> parse(final Object in) throws Exception {
  26.         XmlUtils.parse(in, this);
  27.         return this.info;
  28.     }

  29.     /**
  30.      * @return the parsed info.
  31.      */
  32.     public HashMap<String, String> getInfo() {
  33.         return this.info;
  34.     }

  35.     /**
  36.      * @param uri
  37.      *            - the uri for this element
  38.      * @param localName
  39.      *            - the local name for this element
  40.      * @param qName
  41.      *            - the fully-qualified name for this element
  42.      * @param attributes
  43.      *            - the attributes of the element
  44.      */
  45.     public final void startElement(final String uri, final String localName,
  46.             final String qName, final Attributes attributes) {
  47.         if (localName.equals(XML_TAG)) {
  48.             info.put(attributes.getValue(XML_NAME),
  49.                     attributes.getValue(XML_VALUE));
  50.         }
  51.     }

  52. }