/*
This file is part of JLoom
Copyright (C) 2006 Gereon Fassbender
Homepage: jloom.sourceforge.net
JLoom 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 can find a copy of the GNU General Public License along with this program
in a file called COPYING. Information can also be found at www.fsf.org or
www.gnu.org or write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
created: 21.01.2006 Gereon Fassbender
$Revision$
$Date$
$Log$
*/
package net.gereon.jloom.syntax.impl.commands;
import static net.gereon.jloom.core.JLoomConstants.*;
import java.util.*;
import net.gereon.jloom.core.MacroGenerator;
import net.gereon.jloom.problems.TranslationException;
import net.gereon.jloom.syntax.*;
public class CommandsImpl extends Commands
{
private static Commands singleton = new CommandsImpl();
private Map<String, CommandDefinition> commandsMap = new HashMap<String, CommandDefinition>();
private CommandsImpl()
{
}
public static Commands get()
{
return singleton;
}
private Class<MacroGenerator> getGeneratorClass(String name) throws TranslationException
{
try {
Class c = Class.forName(MACRO_PACKAGE + "." + name + GENERATOR_CLASS_POSTFIX);
return /*(Class<MacroGenerator>)*/ c;
}
catch (ClassNotFoundException ex) {
throw new TranslationException("Unknown command: " + name);
}
}
private CommandDefinition loadCommandDefinition(String name) throws TranslationException
{
try {
CommandDefinition cmd;
try {
String pname = getClass().getPackage().getName();
Class c = Class.forName(pname + ".Command_" + name);
cmd = (CommandDefinition) c.newInstance();
}
catch (ClassNotFoundException ex) {
cmd = new MacroCommandDefinition(name, getGeneratorClass(name));
}
CommandDefinition old = commandsMap.put(name, cmd);
assert old == null;
return cmd;
}
/*catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}*/
catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
catch (InstantiationException ex) {
throw new RuntimeException(ex);
}
}
public CommandDefinition getCommandDefinition(String name) throws TranslationException
{
CommandDefinition c = commandsMap.get(name);
if (c != null) {
return c;
}
else {
return loadCommandDefinition(name);
}
}
}