package PdhData;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.StringTokenizer;

/**
 * A root class for data objects which can be read from a text file
 */
public class PdhData extends Object
{
  /**
   * Attributes
   * int id_
   * the id number of it's position in a vector
   */
  private int id_;

  /**
   * Constructor
   */
  public PdhData()
  {
    id_ = 0;
  }

  /**
   * Constructor
   */
  public PdhData(int id)
  {
    id_ = id;
  }
  
  public PdhData(PdhData d)
  {
    if (d != null)
    {
      id_ = d.id_;
    }
    else
    {
      id_ = 0;
    }
  }
  
  public PdhData(String line)
  {
    if (line != null)
    {
      id_ = readIndex(line);
    }
  }

  /**
   * toString - displays the contents of the class as a text string
   * @return String ""
   */
  public String toString()
  {
    return "";
  }

  /**
   * toFile - outputs the contents of the class as a text string for writing to file
   * @return an empty string
   */
  public String toFile()
  {
    return "";
  }
  
  /**
   * toXML - outputs the contents of the class as a String in XML format 
   * @return an empty string
   */
  public String toXML()
  {
    return "";
  }

  /**
   * readIndex - reads the index number from the input line
   * @param line - the input line as a String in the format "n=x,y,z" where n is an integer
   * @return the integer read
   */
  public int readIndex(String line)
  {
    //String restOfLine;
    int idx = line.indexOf("=");
    Integer val = null;

    if (idx > 0)
    {
      val = new Integer(line.substring(0, idx));
      return val.intValue();
    }

    return -1;
  }

  public int readInteger(String line, int col)
  {
    try
    {
      int idx = line.indexOf("=");
      int next;
      Integer val;

      int c = 0;

      while(idx > 0)
      {
        idx++;
        next = line.indexOf("," , idx + 1);
        if (c == col)
        {
          try
          {
            if (next > 0)
              val = new Integer(line.substring(idx,next));
            else
              val = new Integer(line.substring(idx));
          }
          catch(Exception e)
          {
            e.printStackTrace();
            val = new Integer(0);
          }
          return val.intValue();
        }
        idx = next;
        c++;
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    
    return 0;
  }

  public double readDouble(String line, int col)
  {
    int idx = line.indexOf("=");
    int next;
    Double val;

    int c = 0;

    while(idx > 0)
    {
      idx++;
      next = line.indexOf("," , idx + 1);
      if (c == col)
      {
        if (next > 0)
          val = new Double(line.substring(idx,next));
        else
          val = new Double(line.substring(idx));
        return val.doubleValue();
      }
      idx = next;
      c++;
    }

    return 0.0;
  }

  public float readFloat(String line, int col)
  {
    int idx = line.indexOf("=");
    int next;
    Float val;

    int c = 0;

    while(idx > 0)
    {
      idx++;
      next = line.indexOf("," , idx + 1);
      if (c == col)
      {
        if (next > 0)
          val = new Float(line.substring(idx,next));
        else
          val = new Float(line.substring(idx));
        return val.floatValue();
      }
      idx = next;
      c++;
    }

    return 0;
  }

  public long readLong(String line, int col)
  {
    int idx = line.indexOf("=");
    int next;
    Long val;

    int c = 0;

    while(idx > 0)
    {
      idx++;
      next = line.indexOf("," , idx + 1);
      if (c == col)
      {
        if (next > 0)
          val = new Long(line.substring(idx,next));
        else
          val = new Long(line.substring(idx));
        return val.longValue();
      }
      idx = next;
      c++;
    }

    return 0;
  }

  public String readString(String line, int col)
  {
    int idx = line.indexOf("=");
    int next;

    int c = 0;

    while(idx > 0)
    {
      idx++;
      next = line.indexOf("," , idx);
      if (c == col)
      {
        if (next > 0)
          return line.substring(idx,next);
        else
          return line.substring(idx);
      }
      idx = next;
      c++;
    }

    return "";
  }


  public boolean readBoolean(String line, int col)
  {
    int idx = line.indexOf("=");
    int next;

    int c = 0;

    while(idx > 0)
    {
      idx++;
      next = line.indexOf("," , idx);
      if (c == col)
      {
        if (next > 0)
          return (line.substring(idx,next).startsWith("Y"));
          
        else
          return (line.substring(idx) == "Y");
      }
      idx = next;
      c++;
    }

    return false;
  }

  public Date readDate(String line, int col)
  {
    int idx = line.indexOf("=");
    int next;
    Date val;

    int c = 0;

    while(idx > 0)
    {
      idx++;
      next = line.indexOf("," , idx + 1);
      if (c == col)
      {
        if (next > 0)
          val = new Date(line.substring(idx,next));
        else
          val = new Date(line.substring(idx));
        return val;
      }
      idx = next;
      c++;
    }

    return null;
  }

  public int[] readIntegerArray(String line, int col)
  {
    String s = readString(line,col);
    int intArray[] = null;
    int idx = 1;
    int next;
    int arraySize = 0;
    String strArray[] = s.split(";");
    arraySize = strArray.length;
    intArray = new int[arraySize];
    Integer val;
    for(int i=0; i<arraySize; i++)
    {
      try
      {
        val = new Integer(strArray[i]);
        intArray[i] = val.intValue();
      }
      catch(Exception e)
      {
        intArray[i] = 0;
      }
    }

    return intArray;
  }
  
  public int getId()
  {
    return id_;
  }
  
  public void setId(int id)
  {
    id_ = id;
  }
  
  public String readXML(String attribute, String line)
  {
    int idx = line.indexOf("<");
    int next;
    int len = attribute.length();

    while(idx > 0)
    {
      idx++;
      if ( line.substring(idx,idx+len) == attribute)
      {
        idx = line.indexOf("=", idx) + 1;
        if (idx > 0)
          return "";
        next = line.indexOf(">", idx);
        if (next > 0)
          return line.substring(idx,next);
        return "";
      }
      next = line.indexOf("<", idx);
      idx = next;
    }

    return "";
  }
  
  public int readIntValue(String value)
  {
    if (value == null) return PdhError.NULL_VALUE;
    Integer i = new Integer(value);
    return i.intValue();
  }
  public long readLongValue(String value)
  {
    if (value == null) return PdhError.NULL_VALUE;
    Long i = new Long(value);
    return i.longValue();
  }
  public float readFloatValue(String value)
  {
    if (value == null) return PdhError.NULL_VALUE;
    Float i = new Float(value);
    return i.floatValue();
  }
  public double readDoubleValue(String value)
  {
    if (value == null) return PdhError.NULL_VALUE;
    Double i = new Double(value);
    return i.doubleValue();
  }
  public Date readDateValue(String value)
  {
    if (value == null) return null;
    return new Date(value);
  }

}

 
