/*
 * DomainSecurity.java (Implements Domain based security)
 *
 * Written by   : Nitin Jeevan Motgi.
 * Date Written : 04/28/2001.
 *
 * Portions copyright (c) 2001-2002 to School of Electrical Engineering
 * and computer science.
 *
 * All rights reserved by author.
 *
*/

/* Import Java Libraries.*/
import java.io.*;
import java.lang.*;
import java.util.*;

/* 
 * This class provides basic implementation start-up for domain based
 * implementation of the system.
 */
public class DomainSecurity{
  static int nUserEntry = 0;
  /* total number of actions present in the file.*/
  static int nActionEntry = 0;
  /* Number of requests in the Input file .*/
  static int NoRequest = 0;
  /* # of domains.*/
  static int nDomainEntry = 0;

  /* Entry-point in to the program.*/
  public static void main(String args[]){
   String [][] Domain; String [][] User; String [][] Action;
   String proc;
   Domain = new String [1000][3];
   int Level = 0,nFSMState = 0,TempInt = 0,nPos = 0,intake =0;

   /* Initialise memory for basic scratch pad.*/
   User = new String [1000][2];
   Action = new String [1000][3];

   String LineBuf;              /* Stores each input line.*/
   String LineLowCase;          /* Stored lower case line.*/
   int Line;                    /* Current Line count.*/

   String szSyntax = new String(":,<({})> ");      /* Demlimiters used.*/
   
   /* Perform Command line intregity Check.*/
   if (args.length < 1)
    System.out.println("Usage: java DomainSecurity <data file> ");

   /* Store the name of the file.*/
   String FileName = (args[0]);

   /* Start processing the file till <EOF> is reached.*/
   try{
    /* Open the file.*/
    BufferedReader pFileHandle = new BufferedReader (new FileReader(FileName));
    /* Initialise current line count.*/
    Line = 0;
    int nIndexj = 0;

    /* Read and prase each line at a time.*/
    while ((LineBuf = pFileHandle.readLine()) != null){
     /* If there is no character in line, continue without processing.*/
     if (LineBuf.length() == 0) continue;
     Line++;
     /* Check for comments in the line.*/
     if(LineBuf.charAt(0) == '/'){
      if(nFSMState == 1) nDomainEntry = nPos;
      if(nFSMState == 2) nUserEntry = nPos;
      nFSMState++;
      nPos=0;
      nIndexj = 0;
      continue;
     }/* End if.*/

     /* Convert the line to lower case.*/
     LineLowCase = LineBuf.toLowerCase();
     StringTokenizer st = new StringTokenizer(LineLowCase, szSyntax);

     /* While tokenising move the from state to state, to
	get back the inputs from the structured file. A 
	FSM for this is implemented.*/
     while (st.hasMoreTokens()){
     if(nFSMState == 1){
      try{
       proc = st.nextToken();           /* Get the next token.*/
       try{
	int Value = Integer.parseInt(proc);
	String szBuffer = Domain[nPos][nIndexj-1];
	Domain[nPos][nIndexj-1] = szBuffer.concat(proc);
       }catch(Exception e){
	Domain[nPos][nIndexj++] = proc;
	}/* End catch.*/
       }catch(Exception pExecption){pExecption.printStackTrace();}
      }else 
      if (nFSMState == 2){
       try{
	proc = st.nextToken();          /* Get the next token.*/
	try{
	 int Value = Integer.parseInt(proc);
	 String szBuffer = User[nPos][nIndexj-1];
	 User[nPos][nIndexj-1] = szBuffer.concat(proc);
	}catch(Exception e){
	 User[nPos][nIndexj++] = proc;
	}/* End of catch.*/
       }catch(Exception pExecption){pExecption.printStackTrace();}
      }else 
       if(nFSMState == 3){
	try{
	 proc = st.nextToken();
	 try{
	  int Value = Integer.parseInt(proc);
	  String szBuffer = Action[nPos][nIndexj-1];
	  Action[nPos][nIndexj-1] = szBuffer.concat(proc);
	 }catch(Exception e){
	  Action[nPos][nIndexj++] = proc;
	 }/* End of catch.*/
	}catch(Exception pExecption){pExecption.printStackTrace();}
       }/* End of catch.*/
     }/*end of while*/
     nPos++; nIndexj =0;
    }/*end of outter while*/
     pFileHandle.close();
  }/*end of outter try*/
  catch (Exception pExecption){
   System.out.println("** ERROR : Failed to open input file.");
  }/* End of catch.*/
  nActionEntry = nPos;

  /* Create a Domain Arbitartor to control all the requirements of the
     users.*/
  DomainArbitrator DomArb = new DomainArbitrator(Domain, User, Action);
  DomArb.TakeRequest();
 }/*End of Main*/
}/*End of Class*/






