/*
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 java.lang.reflect.Method;
import java.util.EnumSet;
import net.gereon.jloom.core.*;
import net.gereon.jloom.problems.*;
import net.gereon.jloom.syntax.*;
import net.gereon.jloom.syntax.impl.ParametersImpl;
public class MacroCommandDefinition implements CommandDefinition
{
private String name;
private Class<MacroGenerator> generatorClass;
private MacroGenerator referenceGenerator;
//private boolean closeTag;
//private int parameterCount;
private Parameters parameters;
private Parameters secondaryParameters;
public MacroCommandDefinition(String name, Class<MacroGenerator> generatorClass) throws TranslationException
{
assert name != null;
assert !name.equals("");
assert generatorClass != null;
this.name = name;
this.generatorClass = generatorClass;
init_internal();
}
private void init_internal(Method generateMethod)
{
Class[] param = generateMethod.getParameterTypes();
assert param.length >= 1;
assert param[0] == GenerationContext.class;
int parameterCount = param.length - 1;
parameters = new ParametersImpl(parameterCount);
secondaryParameters = new ParametersImpl(0);
}
private void init_internal() throws TranslationException
{
String gm = JLoomConstants.MAIN_METHOD_NAME;
boolean found = false;
for (Method m : generatorClass.getDeclaredMethods()) {
if (m.getName().equals(gm)) {
if (found) { throw new TranslationException("More than one generate-method found for command: " + name); }
found = true;
init_internal(m);
}
}
if (!found) {
throw new TranslationException("No generate method found for command: " + name);
}
try {
Object gen = generatorClass.newInstance();
if (!(gen instanceof MacroGenerator)) {
throw new TranslationException("The generator class doesn't implement " + MacroGenerator.class.getName());
}
referenceGenerator = (MacroGenerator) gen;
}
catch (IllegalAccessException ex) {
throw new TranslationException(ex);
}
catch (InstantiationException ex) {
throw new TranslationException(ex);
}
}
public String getName()
{
return name;
}
/*public boolean hasCloseTag()
{
return closeTag;
}*/
public Class<MacroGenerator> getJLoomClass()
{
return generatorClass;
}
public Parameters getParameters()
{
return parameters;
}
public Parameters getSecondaryParameters()
{
return secondaryParameters;
}
/*
public boolean isNonEmptyAllowed()
{
return referenceGenerator.isNonEmptyAllowed();
}
public boolean isEmptyAllowed()
{
return referenceGenerator.isEmptyAllowed();
}
*/
public EnumSet<CommandOption> getOptions()
{
return referenceGenerator.getOptions();
}
public boolean validate(ProblemHandler handler, CommandBlock cmd)
{
assert handler != null;
assert cmd != null;
return referenceGenerator.validate(handler, cmd);
}
}