// DataNode.java, version 1.03, February 14, 1998.
// Copyright 1998 by Rick Wagner, all rights reserved.
// This file defines a class that is a node in a tree data structure.

import java.util.*;

public class DataNode extends Object
{
  private String sName = null;
  private Vector vChild = null;

  public DataNode()                       // Default constructor
  {
    sName = new String();
    vChild = new Vector();
  }

  public DataNode(DataNode o)             // Copy constructor
  {
    int i = 0;
    sName = new String(o.sName);
    vChild = new Vector();
    for (i = 0; i < o.vChild.size(); i++)
    {
      vChild.addElement(o.vChild.elementAt(i));
    }
  }

  public DataNode(String s)               // General name constructor
  {
    int i = 0;
    sName = new String(s);
    vChild = new Vector();
  }

  public DataNode(Vector v)               // General vector constructor
  {
    int i = 0;
    sName = new String();
    vChild = new Vector();
    for (i = 0; i < v.size(); i++)
    {
      vChild.addElement(v.elementAt(i));
    }
  }

  public DataNode getChild(int i)         // Single child accessor
  {
    return new DataNode((DataNode) vChild.elementAt(i));
  }

  public void setChild(DataNode o, int i) // Child mutator
  {
    vChild.setElementAt(o, i);
  }

  public Vector getChildVector()          // All children accessor
  {
    int i = 0;
    Vector vTemp = new Vector();
    for (i = 0; i < vChild.size(); i++)
    {
      vTemp.addElement(vChild.elementAt(i));
    }
    return vTemp;
  }

  public String getName()                  // Name accessor
  {
    return new String(sName);
  }

  public void setName(String s)            // Name mutator
  {
    sName = s;
  }

  public void addChild(DataNode o)         // Add a child
  {
    vChild.addElement(o);
  }

  public int numChildren()                 // Get the number of children
  {
    return vChild.size();
  }

  public void removeChild(int i)           // Remove a particular child
  {
    vChild.removeElementAt(i);
  }

  public DataNode findChild(String s)      // Search the tree for name
  {
    System.out.println("Finding child " + s + ", looking at " + sName);
    DataNode oFoundChild = null;
    if (sName.equalsIgnoreCase(s))
    {
      oFoundChild = this;
    }
    else
    {
      int i = 0;
      for (i = 0; i < vChild.size(); i++)
      {
        oFoundChild = ((DataNode) vChild.elementAt(i)).findChild(s);
        if (oFoundChild != null)
        {
          break;
        }
      }
    }
    return oFoundChild;
  }

} // End class DataNode
