key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
**
**  $Id$
**
**  Class History
**
**  Date        Name         Description
**  ---------|------------|-----------------------------------------------
**  24Aug98     subtle       start of recorded history
**
*/

package key.commands;

import key.*;
import key.util.SeperatedIdentifier;
import java.util.StringTokenizer;
import java.io.IOException;

import java.util.Enumeration;

public class Load extends Command
{
	public Load()
	{
		setKey( "load" );
		usage = "<type> <identifier> [<argument>]";
	}
	
	public void run( Player p, StringTokenizer args, String fullLine, CategoryCommand caller, InteractiveConnection ic, Flags flags ) throws IOException
	{
		String type;

		if( !args.hasMoreTokens() )
			usage( ic );
		else
		{
			type = args.nextToken();

			if( !args.hasMoreTokens() )
				usage( ic );
			else
			{
				SeperatedIdentifier full = new SeperatedIdentifier( args.nextToken( " " ) );
				if( full.property == true )
				{
					ic.sendError( "Load will not create properties, sorry (try set)" );
					return;
				}
				
				String id=full.id;
				String loc=full.location;
				
				Object o = new Search( loc, p.getContext() ).result;
				
				if( o != null )
				{
					if( o instanceof Container )
					{
						Container l = (Container)o;
						try
						{
							Atom r;
							
							if( id.equals( Key.nullString ) )
								id = "";
							
							r = (Atom) Factory.makeAtom( Class.forName( "key." + type ), id );
							
							if( args.hasMoreTokens() )
								r.argument( args.nextToken( "" ) );
							
							l.add( r );
								//  no longer needed, now called in setParent
							//r.stopBeingTemporary();
							ic.sendFeedback( "Added new " + Type.typeOf( r ).getName() + " '" + r.getName() +"' to " + l.getId() );
							
							if( l.getReferencesOnly() )
								ic.sendFeedback( "^RWARNING: The container you have loaded this atom into is a reference container; Your new atom is currently temporary and will be erased unless you move it into a non-reference container.^-" );
						}
						catch( ClassNotFoundException e )
						{
							ic.sendError( e.toString() );
						}
						catch( BadKeyException e )
						{
							ic.sendError( "'" + id + "' should contain only alphabetic characters" );
						}
						catch( NonUniqueKeyException e )
						{
							ic.sendError( "There is already an atom with this identifier ('" + id + "')" );
						}
					}
					else
						ic.sendError( "'" + Type.typeOf( o ).getName() + "' is not a container" );
				}
				else
					ic.sendError( "Could not find '" + loc + "'" );
			}
		}
	}
}