/*****************************************************************************
 *                                                                           *
 *         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 From: header
 * field of a SIP message.</p>
 *
 * For example:
 * <pre>
 *     From: nagy@watson.ibm.com (Bill Nagy)
 * </pre>
 *
 * @author Bill Nagy
 * @version 1.0
 */
public class FromEntity
{
  /** The SIP-URL on the From: line. */
  SIPurl url;
  /** The comment field on the From: line.*/
  String comment;

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

	//fixme
	st.nextToken();

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

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

  /**
   * Constructs a From: string from the data present within the object.
   *
   * @return    String representation of the url.
   * @exception MalformedHeaderException
   *            Thrown if enough data is not present within the object to
   *            build the string.
   */
  public String toFromString()
       throws MalformedHeaderException
  {
    if (url == null)
      throw new MalformedHeaderException("Missing SIP-URL\n");

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

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

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