// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HashMapHandler extends DefaultHandler {
   private StringBuffer content;
   private HashMap map;
   private String name;
   private String type;
   public HashMapHandler(HashMap hm) {
      content = new StringBuffer();
      map = hm;
   }
   public void characters(char buf[], int offset, int len) throws SAXException {
      content.append(new String(buf, offset, len));
      return;
   }
   public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException {
      content = new StringBuffer();
      if(rawName.equals("attribute")) {
         name = atts.getValue("name");
         type = atts.getValue("type");
      }
      return;
   }
   public void endElement(String namespaceURI, String localName, String rawName) throws SAXException {
      try {
         if(rawName.equals("attribute")) {
            Class declarg[] = new Class[1];
            declarg[0] = Class.forName("java.lang.String");
            Object callarg[] = new Object[1];
            callarg[0] = content.toString();
            map.put(name, Class.forName(type).getConstructor(declarg).newInstance(callarg));
         }
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } catch (NoSuchMethodException e) {
         e.printStackTrace();
      } catch (InstantiationException e) {
         e.printStackTrace();
      } catch (IllegalAccessException e) {
         e.printStackTrace();
      } catch (InvocationTargetException e) {
         e.printStackTrace();
      }
      return;
   }
}

