//ModuleParserFactory.java

package code.loader.parser;

import java.io.*;
import java.util.*;
import java.lang.reflect.*;

import code.modules.*;
import code.type.*;
import code.symbols.*;
import code.loader.except.*;

/**
 * use layered initialization pattern to create the parser instance by the file
 * extension. <P>
 * <p/>
 * <B>Class Responsibilities</B> :
 * <OL>
 * </OL>
 * <B>Class Collaborators</B> :
 * <OL>
 * <LI> None </LI>
 * </OL>
 *
 * @author jimeng
 * @since Jan 09, 2003
 */

public class ModuleParserFactory {
    private static TxtParser txtParser = null;
    private static XMLParser xmlParser = null;


    /**
     * Singleton class.
     */
    private ModuleParserFactory() {
    }


    public static ModuleParser createModParser(String filePath) throws FileFormatNotSupportException {
        int index = filePath.lastIndexOf('.');
        if (index < 0) {
            throw new FileFormatNotSupportException("No extension found in " + filePath);
        }
        String format = filePath.substring(index + 1);
        ModuleParser parser = null;
        if (format.equals("txt")) {
            if (txtParser == null) {
                txtParser = new TxtParser();
            }
            parser = (ModuleParser) txtParser;
        } else if (format.equals("xml")) {
            if (xmlParser == null) {
                xmlParser = new XMLParser();
            }
            parser = (ModuleParser) xmlParser;
            // We have desupported XML format for the time being - 7/28/04 Sergio.
            // Hence we raise this exception.
            throw new FileFormatNotSupportException() ;
        } else {
            throw new FileFormatNotSupportException("Extension " + format + " of " + filePath + " NOT supported");
        }
        return parser;
    }
}

