package W3C.PICS;
import java.util.Vector;
import java.io.*;
/**
 * Filter's structure is similar to SingleLabel. Instead of having
 * a value for each category, Filter stores the upper and lower limits.
 * @see W3C.PICS.Range
 * @see W3C.PICS.SingleLabel
 */

public class Filter
{

  public Filter(String ratingsystem)
  {
     rangelist=new Vector();
     this.ratingsystem=ratingsystem;
  }
  /**
   * Constructs a Filter from an InputStream
   */

  public Filter(InputStream ins) throws PICSException, IOException
  {
    int type;
    rangelist=new Vector();

    PICSTokenizer PT=new PICSTokenizer(ins);
    PT.firstParen();
    type=PT.nextPair("rating-system",'"');
    if (type==PICSTokenizer.T_CLOSEPAREN)
       throw new PICSException("Unexpected ) ");
    ratingsystem=PT.sval;
    PT.firstParen();
    while (true)
        {type=PT.nextRange();
         if (type==PICSTokenizer.T_CLOSEPAREN)
            break;
         rangelist.addElement(new Range(PT));
        }
  }

  public void addRange(Range r)
  {
    rangelist.addElement(r);
  }
  /**
   * @return The a text representation of the Filter
   */
  public String toString()
  {
    String rangestring="( (rating-system \"" + ratingsystem + "\") (";

    for (int i=0;i<rangelist.size();i++)
    {
     if (i!=0) rangestring+=" ";
     rangestring+=" " + rangelist.elementAt(i) + " ";
    }

    rangestring+=") )";
    return rangestring;
  }


  public Vector getRangeList()
  {
    return rangelist;
  }

  public String getRatingsystem()
  { return ratingsystem;
  }

  /**
   * Reset the internal pointer so that the next call of nextRange() will return
   * the first Range,
   */
  public void reset()
  {
    index=0;
  }

  /**
   * @return The next Range
   */
  public Range nextRange()
  {
    if (index==rangelist.size())
        index=-1;
    return (Range)rangelist.elementAt(index++);
  }

  private Vector rangelist;

  private String ratingsystem;

  private int index=0;
}

