import org.xml.sax.*;
import org.xml.sax.helpers.*;

/**
 * Handles elements in CurrencyConverter SOAP requests and responses
 *
 * @author   Prof. David Bernstein, James Madison University
 * @version  1.0
 */
public class CurrencyConverterSOAPHandler extends DefaultHandler
{
    private boolean        foundConvert;
    private String         amount, from, root, to, waitingFor;



    /**
     * Explicit Value Constructor
     *
     * @param root    Either ConvertCurrency or ConvertCurrencyResponse
     */
    public CurrencyConverterSOAPHandler(String root)
    {
       this.root = root;
    }
    


    /**
     * Receive notification of character data inside an element
     *
     * @param ch     The characters
     * @param start  The start position in the character array
     * @param length The number of characters to use from the character array
     */
    public void characters(char[] ch, int start, int length)
    {
       if      (waitingFor.equals("fromCurrency_characters")) 
       {
          from =  new String(ch, start, length).trim();
       }
       else if (waitingFor.equals("toCurrency_characters")) 
       {
          to = new String(ch, start, length).trim();
       }
       else if (waitingFor.equals("amount_characters")) 
       {
          amount = new String(ch, start, length).trim();
       }
    }


    /**
     * Get the amount of the FromCurrency to be converted
     *
     * @return    The amount
     */
    public double getAmount()
    {
       double       amt;

       amt = 0.0;
       try 
       {
          amt = Double.parseDouble(amount);
       }
       catch (NumberFormatException nfe) 
       {
          amt = -1.0;
       }

       return amt;
    }


    /**
     * Get the FromCurrency
     *
     * @return  The FromCurrency
     */
    public String getFromCurrency()
    {
       return from;
    }


    /**
     * Get the ToCurrency
     *
     * @return  The ToCurrency
     */
    public String getToCurrency()
    {
       return to;
    }



    /**
     * Receive notification of the start of an element
     *
     * @param uri        The namespace URI 
     * @param localName  The local name or the empty string (if no namespace)
     * @param qName      The qualified name (with prefix)
     */
    public void endElement(String uri, String localName, String qName)
    {
       if (qName.equals(root))  foundConvert = false;
       if (foundConvert)        waitingFor = "";
    }





    /**
     * Receive notification of the beginning of the document
     *
     */
    public void startDocument()
    {
       foundConvert = false;
       waitingFor = "";
    }




    /**
     * Receive notification of the start of an element
     *
     * @param uri        The namespace URI 
     * @param localName  The local name or the empty string (if no namespace)
     * @param qName      The qualified name (with prefix)
     * @param attributes The specified or default attributes
     */
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes)
    {
       if (foundConvert) 
       {
          waitingFor = qName + "_characters";
       }
       else if (qName.equals(root)) 
       {
          foundConvert = true;
          waitingFor = "";
       }
    }
}
