Skip to content
amolskh edited this page Dec 5, 2012 · 1 revision

package Main;

import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry;

import org.annotation.DSL; import org.bean.DSLObject;

import com.domain.Calculator;

public class Main { static Class<?> dslClass = Calculator.class; static List dslCommands = new ArrayList(); public static Map runTimeVars = new HashMap(); public static Map methodCommandMapping = new HashMap();

/** * @param args */ public static void main(String[] args) { readFile(); Method[] allmethods = dslClass.getDeclaredMethods(); for (Method method : allmethods) { Annotation dslAnnotation = method.getAnnotation(DSL.class); if (dslAnnotation instanceof DSL) { DSL dslCastAnnotn = (DSL) dslAnnotation; DSLObject dslObj = new DSLObject(); dslObj.setCommandName(dslCastAnnotn.commName()); dslObj.setCommandSyntax(dslCastAnnotn.commSyntax()); dslObj.setCommandRegex(dslCastAnnotn.commRegex()); dslObj.setM(method); String commandName = dslCastAnnotn.commName(); String commandSyntax = dslCastAnnotn.commSyntax(); String[] commandRegex = dslCastAnnotn.commRegex(); commandSyntax = MessageFormat.format(commandSyntax, commandRegex); methodCommandMapping.put(commandSyntax, dslObj); } } for (int j = 0; j < dslCommands.size(); j++) { Iterator it = methodCommandMapping.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); if (((String) dslCommands.get(j)).matches((String) pairs .getKey())) { DSLObject obj = (DSLObject) pairs.getValue(); Object result = invokeMethod(obj, (String) dslCommands.get(j)); if (null != result && j < (dslCommands.size() - 1) && ((String) dslCommands.get(j + 1)) .startsWith(“Assign”)) { String varName = ((String) dslCommands.get(j + 1)) .replaceAll(“Assign ”, “”); runTimeVars.put(varName, result); j = j + 1; } break; } }

} }

public static void readFile() { try { FileInputStream finStream = new FileInputStream(“Demo.dsl”); DataInputStream dinStream = new DataInputStream(finStream); BufferedReader buReader = new BufferedReader(new InputStreamReader( dinStream)); String strLine; while (null != (strLine = buReader.readLine())) { if (strLine.contains(“Assign”)) { String[] cmd = strLine.split(“ Assign ”); dslCommands.add(cmd); dslCommands.add(“Assign ” + cmd); } else { dslCommands.add(strLine); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

public static Object invokeMethod(DSLObject dslObj, String command) { Method m = dslObj.getM(); String oCommand = command; String commandName = dslObj.getCommandName(); String commandSyntax = dslObj.getCommandSyntax(); String[] commandRegex = dslObj.getCommandRegex(); commandSyntax = MessageFormat.format(commandSyntax, commandRegex); Object[] input = new Object; Object result = null; int cnt = 0; if (command.matches(commandSyntax)) { Object[] orignalCommand = commandSyntax.split(“ ”); Object[] userCommand = command.split(“ ”); for (int i = 0; i < orignalCommand.length; i++) { if (!orignalCommand.equals(userCommand)) { input = userCommand; cnt = cnt + 1; } }

Class[] paramClass = m.getParameterTypes(); for (int i = 0; i < paramClass.length; i++) { String classCastType = null; if (“int”.equals(paramClass.getName())) { classCastType = “java.lang.Integer”; input = Integer.parseInt((String) input); } else { classCastType = paramClass.getName(); Class classCast; try { classCast = Class.forName(classCastType); input = classCast.cast(input);

} catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } try { m.setAccessible(true); Calculator calc = (Calculator) dslClass.newInstance(); result = m.invoke(calc, input); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result; }

}

Clone this wiki locally