////////////////////////////////////////////////////////////////////////////////
//																			  //
// AUTHOR:      Tevfik Metin Sezgin	                        				  //
//				Massachusetts Institute of Technology						  //
//				Department of Electrical Engineering and Computer Science	  //
//				Artificial Intelligence Laboratory							  //
//																			  //
// E-MAIL:		mtsezgin@ai.mit.edu, mtsezgin@mit.edu						  //
//																			  //
// COPYRIGHT:   Tevfik Metin Sezgin                    						  //
//              All rights reserved. This code can not be copied, modified,   //
//              or distributed in whole or partially without the written 	  //
//				permission of the author. Also see the COPYRIGHT file.		  //
//																			  //
////////////////////////////////////////////////////////////////////////////////

/**
  *
  * See the end of the file for the log of changes.
  *
  * $Author: mtsezgin $
  * $Date: 2000/06/08 03:21:34 $
  * $Revision: 1.1 $
  * $Headers$
  * $Id: LoadStoreModule.java,v 1.1 2000/06/08 03:21:34 mtsezgin Exp $
  * $Name:  $
  * $Locker:  $
  * $Source: /mit/mtsezgin/CVS/Repository/REC/LoadStoreModule.java,v $
  *
  **/


import java.util.Vector;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
  *
  * This class loads and stores objects used by the 
  * ObjectManager. See loadFromFile and storeInFile functions.
  *
  **/

class LoadStoreModule
{
    /**
    *
    * Load Terminals and NonTerminals from the given file, and
    * return them in a Vector
    *
    **/
    public static Vector
    loadFromFile( String file_name )
    {
        Object            read_object;
        FileInputStream   fis = null;
        ObjectInputStream ois = null;

        Vector objects = new Vector();

        try {
            fis = new FileInputStream( file_name );
            ois = new ObjectInputStream( fis );
            
            
            System.out.print( "Loading" );
            while ( true ) {
                read_object = ois.readObject();
                System.out.print( "." );

                if ( read_object instanceof Cross ) {
                    objects.add( (Cross)(read_object) );
                    continue;
                }
                
                if ( read_object instanceof Ellipse ) {
                    objects.add( (Ellipse)(read_object) );
                    continue;
                }
                
                if ( read_object instanceof Ground ) {
                    objects.add( (Ground)(read_object) );
                    continue;
                }
                
                if ( read_object instanceof Line ) {
                    objects.add( (Line)(read_object) );
                    continue;
                }
        
                if ( read_object instanceof Motor ) {
                    objects.add( (Motor)(read_object) );
                    continue;
                }
        
                if ( read_object instanceof Pin ) {
                    objects.add( (Pin)(read_object) );
                    continue;
                }
        
                if ( read_object instanceof Point ) {
                    objects.add( (Point)(read_object) );
                    continue;
                }
        
                if ( read_object instanceof Polygon ) {
                    objects.add( (Polygon)(read_object) );
                    continue;
                }
        
                if ( read_object instanceof Rectangle ) {
                    objects.add( (Rectangle)(read_object) );
                    continue;
                }
        
                if ( read_object instanceof Shade ) {
                    objects.add( (Shade)(read_object) );
                    continue;
                }
        
                if ( read_object instanceof Spring ) {
                    objects.add( (Spring)(read_object) );
                    continue;
                }
                
                System.out.println( "Unresolved object" + read_object );
            }
        } catch ( EOFException e ) {
            try {
                ois.close();
            } catch( Exception e2 ) {
                e2.printStackTrace(); 
            }
        } catch ( Exception e ) {
                e.printStackTrace(); 
        }
        
        System.out.println( "" );
        return objects;
    }


    /**
    *
    * Store Terminals and NonTerminals in objects to the given file.
    *
    **/
    public static void
    storeInFile( String file_name, Vector objects )
    {
        System.out.print( "Storing" );
        try {
            FileOutputStream   fos = new FileOutputStream( file_name );
            ObjectOutputStream oos = new ObjectOutputStream( fos );

            
            for ( int i=0; i<objects.size(); i++ ) {
                System.out.print( "." );
                Object output_object = objects.elementAt( i );
                
                if ( output_object instanceof Cross )
                    oos.writeObject( (Cross)(output_object) );
        
                if ( output_object instanceof Ellipse )
                    oos.writeObject( (Ellipse)(output_object) );
        
                if ( output_object instanceof Ground )
                    oos.writeObject( (Ground)(output_object) );
        
                if ( output_object instanceof Line )
                    oos.writeObject( (Line)(output_object) );
        
                if ( output_object instanceof Motor )
                    oos.writeObject( (Motor)(output_object) );
        
                if ( output_object instanceof Pin )
                    oos.writeObject( (Pin)(output_object) );
        
                if ( output_object instanceof Point )
                    oos.writeObject( (Point)(output_object) );
        
                if ( output_object instanceof Polygon )
                    oos.writeObject( (Polygon)(output_object) );
        
                if ( output_object instanceof Rectangle )
                    oos.writeObject( (Rectangle)(output_object) );
        
                if ( output_object instanceof Shade )
                    oos.writeObject( (Shade)(output_object) );
        
                if ( output_object instanceof Spring )
                    oos.writeObject( (Spring)(output_object) );
            }
            oos.close();
        } catch( Exception e ) { 
            e.printStackTrace(); 
        }
        System.out.println( "" );
    }
}


/** 
  * 
  * $Log: ObjectManager.java,v $
  *
  **/
