/*
Tiamat - a role-playing game framework
CharacterFactory.java - Abstract factory to select an appropriate Character for a given file
 
 Copyright  (C) 2000-2004 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat;

import com.huguesjohnson.serialization.Formatter;
import com.huguesjohnson.serialization.Storable;

/** CharacterFactory - Abstract factory to select an appropriate Character for a given file
 * @author Hugues Johnson
 */
public abstract class CharacterFactory{
    /**
     * Chooses an appropriate Character for the given file and loads it, throws UnsupportedCharacterException if one can not be found.
     * @param path The absolute path to the serialized Character file.
     * @param formatter The formatter used to deserialze the Character file.
     * @return The deserialized Character.
     * @throws UnsupportedCharacterException If the file can be deserialized to a known Character implementation.
     */
    public final static Character load(String path,Formatter formatter) throws UnsupportedCharacterException{
        try{
            Storable object=formatter.loadFrom(path);
            if(object instanceof NonPlayerCharacter){
                return((NonPlayerCharacter)object);
            } else if(object instanceof PlayerCharacter){
                return((PlayerCharacter)object);
            } else{
                throw(new UnsupportedCharacterException());
            }
        } catch(Exception x){
            x.printStackTrace();
            throw(new UnsupportedCharacterException(x.getMessage()));
        }
    }
}