/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.io.*;
import java.util.*;
/**
 * This class represents the data contained in the To: header
 * field of a SIP message.</p>
 *
 * For example:
 * <pre>
 *     To: nagy@watson.ibm.com (Bill Nagy)
 * </pre>
 *
 * @author Bill Nagy
 * @version 1.0
 */
public class ToEntity
{
  /** The SIP-URL on the To: line. */
  SIPurl url;
  /** The comment field on the To: line.*/
  String comment;

  /**
   * Parses the To: data from a string, and fills in the object.
   *
   * @param tostr The string to parse.
   * @exception MalformedHeaderException
   *            Thrown if the To: statement in the string is malformed.
   */
  public void fromString(String tostr)
       throws MalformedHeaderException
  {
    StringTokenizer st = new StringTokenizer(tostr);
    try 
      {
	/*Throw away the "To:"*/
	st.nextToken();

	//fixme
	st.nextToken();

	url = new SIPurl();
	url.fromString(st.nextToken(" "));
      }
    catch (NoSuchElementException e)
      {
	throw new MalformedHeaderException("Malformed To: --> "+tostr);
      }
    catch (MalformedSIPurlException e)
      {
	throw new MalformedHeaderException("Malformed To: --> "+tostr);
      }

    try
      {
	comment = st.nextToken();
      }
    catch (NoSuchElementException e)
      {
      }
  }

  /**
   * Constructs a To: string from the data present within the object,
   * but leaves off the 'To:'.
   *
   * @return The string representing the To: field.
   * @exception MalformedHeaderException
   *            Thrown if enough data is not present within the object to
   *            build the string.
   */
  public String toToStringWithoutHeader()
       throws MalformedHeaderException
  {
    if (url == null)
      throw new MalformedHeaderException("Missing SIP-URL\n");

    try
      {
	if (comment == null)
	  return new String(url.toURLString());
	else
	  return new String(url.toURLString()+comment);
      }
    catch (MalformedSIPurlException e)
      {
	throw new MalformedHeaderException(e.toString());
      }
  }

  /**
   * Constructs a To: string from the data present within the object.
   *
   * @return The string representing the To: field.
   * @exception MalformedHeaderException
   *            Thrown if enough data is not present within the object to
   *            build the string.
   */
  public String toToString()
       throws MalformedHeaderException
  {
    return "To: "+toToStringWithoutHeader();
  }


  /**
   * Returns the SIP-URL portion of the To:.
   * 
   * @return A SIP URL
   */
  public SIPurl getURL()
  {
    return url;
  }
  
  /**
   * Sets the SIP-URL portion of the To:.
   *
   * @param newurl The new SIP URL.
   */
  public void setURL(SIPurl newurl)
  {
    url = newurl;
    return;
  }

  /**
   * Returns the comment portion of the To:.
   *
   * @return The comment.
   */
  public String getComment()
  {
    return comment;
  }
  
  /**
   * Sets the comment portion of the To:.
   * 
   * @param comment The new comment.
   */
  public void setComment(String comment)
  {
    this.comment = comment;
    return;
  }
}
