/*****************************************************************************
 *                                                                           *
 *         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 a SIP-URL.
 *
 * For example:
 * <pre>
 *     nagy@watson.ibm.com:1107
 * </pre>
 *
 * @author Bill Nagy
 * @version 1.0
 */
public class SIPurl
{
  /** The userid contained within the URL*/
  private String user;
  /** The host portion of the URL*/
  private String host;
  /** The port number contained withing the URL*/
  private int port = 1107;
  
  
  /**
   * Parses a SIP-URL from a string, and fills in the object.
   *
   * @param url The string which contains the url.
   * @exception MalformedSIPurlException
   *            Thrown if the SIP-URL in the string is malformed.
   */
  public void fromString(String url)
       throws MalformedSIPurlException
  {
    StringTokenizer st = new StringTokenizer(url, "@:");
    try 
      {
	user = st.nextToken();
	host = st.nextToken();
      }
    catch (NoSuchElementException e)
      {
	throw new MalformedSIPurlException("Malformed SIP-URL --> "+url);
      }
    try
      {
	port = (new Integer(st.nextToken())).intValue();
      }
    catch (NoSuchElementException e)
      {}
  }

  /**
   * Constructs a SIP-URL string from the data present within the object.
   *
   * @return The string representing the URL.
   * @exception MalformedSIPurlException
   *            Thrown if enough data is not present within the object to
   *            build the string.
   */
  public String toURLString()
       throws MalformedSIPurlException
  {
    if (user == null)
      throw new MalformedSIPurlException("Missing userid");
    if (host == null)
      throw new MalformedSIPurlException("Missing hostname");
    if (port == 0)
      return new String(user+"@"+host);
    else
      return new String(user+"@"+host+":"+port);
  }

  /**
   * Returns the userid contained within the SIP-URL.
   *
   * @return The user portion of the URL.
   */
  public String getUser()
  {
    return user;
  }
  
  /**
   * Sets the userid of the SIP-URL.
   *
   * @param val The user name for the URL.
   */
  public void setUser(String val)
  {
    user = val;
    return;
  }

  /**
   * Returns the host contained within the SIP-URL.
   *
   * @return The host portion of the URL.
   */
  public String getHost()
  {
    return host;
  }

  /**
   * Sets the host portion of the SIP-URL.
   *
   * @param val The hostname for the URL.
   */
  public void setHost(String val)
  {
    host = val;
    return;
  }

  /**
   * Returns the port contained within the SIP-URL.
   *
   * @return The port contained within the URL.
   */
  public int getPort()
  {
    return port;
  }

  /**
   * Sets the port number in the SIP-URL.
   *
   * @param val The port for the URL.
   */
  public void setPort(int val)
  {
    port = val;
    return;
  }

  public static void main(String argv[])
       throws Exception
  {
    String input;
    SIPurl url = new SIPurl();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println("Enter the SIP-URL line:");
    input = br.readLine();
    url.fromString(input);
    
    System.out.println();
    System.out.println("User: "+url.getUser());
    System.out.println("Host: "+url.getHost());
    System.out.println("Port: "+url.getPort());
    System.out.println(url.toURLString());
  }

}
