// Helper class for SFtpServer
// or any program that needs a simple first word parse
import java.lang.reflect.*;
import java.util.StringTokenizer;

public class ParseTableEntry {
    private String word;
    private Method func;

    public static boolean parse(ParseTableEntry [] parseTable, 
				Object othis,
				String word,String line, 
				StringTokenizer token) {
	for (int i=0;i<parseTable.length;i++) {
	    if (parseTable[i].equals(word) 
		|| parseTable[i].equals("")) 
		return parseTable[i].call(othis,line,token);
	}
	// should never get here unless no "" case in table
	return false;
    }

    public ParseTableEntry(String word, Class c, String methd) {
	Class [] cary = new Class[2];
	this.word=word;
	cary[0]=String.class;
	cary[1]=StringTokenizer.class;
	try {
	    func=c.getMethod(methd,cary);
	}
	catch (Exception e) {
	    System.err.println("ParseTable: " + e);
	}
    }
    public boolean equals(String s) {
	return word.compareToIgnoreCase(s)==0;
    }
    public boolean call(Object thisptr,String line, StringTokenizer token) {
	Object arg[] = new Object[2];
	arg[0]=(Object)line;
	arg[1]=(Object)token;
	try {
	    Boolean bv;
	    bv=(Boolean)func.invoke(thisptr,arg);
	    return bv.booleanValue();
	}
	catch (Exception e) {
	    System.err.println("Invoke: " + e);
	}
	return false; // error
    }
}
