【原创】利用Java反射来执行XML文件中定义的处理
C:\main.xml程序代码:
<flow> <internalvariable> <caculateresult>0</caculateresult> <executeresult>OK</executeresult> </internalvariable> <class> <name>ClassAdd</name> <method>execute</method> <parameter> <leftvalue>6</leftvalue> <rightvalue>5</rightvalue> </parameter> <returnvalue> <caculateresult>caculateresult</caculateresult> <executeresult>executeresult</executeresult> </returnvalue> </class> <class> <name>ClassSubtract</name> <method>execute</method> <parameter> <leftvalue>caculateresult</leftvalue> <rightvalue>4</rightvalue> </parameter> <returnvalue> <caculateresult>caculateresult</caculateresult> <executeresult>executeresult</executeresult> </returnvalue> </class> <class> <name>ClassMultiply</name> <method>execute</method> <parameter> <leftvalue>caculateresult</leftvalue> <rightvalue>3</rightvalue> </parameter> <returnvalue> <caculateresult>caculateresult</caculateresult> <executeresult>executeresult</executeresult> </returnvalue> </class> <class> <name>ClassDivide</name> <method>execute</method> <parameter> <leftvalue>caculateresult</leftvalue> <rightvalue>2</rightvalue> </parameter> <returnvalue> <caculateresult>caculateresult</caculateresult> <executeresult>executeresult</executeresult> </returnvalue> </class> </flow>
ClassAdd.java
程序代码:
import java.math.BigDecimal; import java.util.HashMap; public class ClassAdd { public static HashMap<String, String> execute(HashMap<String, String> inputMap) { HashMap<String, String> outputMap = new HashMap<String, String>(); try { outputMap.put("executeresult", "NG"); BigDecimal leftvalue = new BigDecimal((String)inputMap.get("leftvalue")); BigDecimal rightvalue = new BigDecimal((String)inputMap.get("rightvalue")); String caculateresult = leftvalue.add(rightvalue).toString(); outputMap.put("caculateresult", caculateresult); outputMap.put("executeresult", "OK"); } catch (Exception e) { e.printStackTrace(); } return outputMap; } }
ClassSubtract.java
程序代码:
import java.math.BigDecimal; import java.util.HashMap; public class ClassSubtract { public static HashMap<String, String> execute(HashMap<String, String> inputMap) { HashMap<String, String> outputMap = new HashMap<String, String>(); try { outputMap.put("executeresult", "NG"); BigDecimal leftvalue = new BigDecimal((String)inputMap.get("leftvalue")); BigDecimal rightvalue = new BigDecimal((String)inputMap.get("rightvalue")); String caculateresult = leftvalue.subtract(rightvalue).toString(); outputMap.put("caculateresult", caculateresult); outputMap.put("executeresult", "OK"); } catch (Exception e) { e.printStackTrace(); } return outputMap; } }
ClassMultiply.java
程序代码:
import java.math.BigDecimal; import java.util.HashMap; public class ClassMultiply { public static HashMap<String, String> execute(HashMap<String, String> inputMap) { HashMap<String, String> outputMap = new HashMap<String, String>(); try { outputMap.put("executeresult", "NG"); BigDecimal leftvalue = new BigDecimal((String)inputMap.get("leftvalue")); BigDecimal rightvalue = new BigDecimal((String)inputMap.get("rightvalue")); String caculateresult = leftvalue.multiply(rightvalue).toString(); outputMap.put("caculateresult", caculateresult); outputMap.put("executeresult", "OK"); } catch (Exception e) { e.printStackTrace(); } return outputMap; } }
ClassDivide.java
程序代码:
import java.math.BigDecimal; import java.util.HashMap; public class ClassDivide { public static HashMap<String, String> execute(HashMap<String, String> inputMap) { HashMap<String, String> outputMap = new HashMap<String, String>(); try { outputMap.put("executeresult", "NG"); BigDecimal leftvalue = new BigDecimal((String)inputMap.get("leftvalue")); BigDecimal rightvalue = new BigDecimal((String)inputMap.get("rightvalue")); String caculateresult = leftvalue.divide(rightvalue).toString(); outputMap.put("caculateresult", caculateresult); outputMap.put("executeresult", "OK"); } catch (Exception e) { e.printStackTrace(); } return outputMap; } }
Main.java
程序代码:
import *; import java.util.*; import java.util.Map.*; import javax.xml.parsers.*; import org.w3c.dom.*; public class Main { public static void main(String[] args) { try { excute("C:/main.xml"); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public static void excute(final String XML_FILE_PATH) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); File file = new File(XML_FILE_PATH); if (!(file.exists() && file.isFile())) { return; } Document document = documentBuilder.parse(file); Element element = document.getDocumentElement(); HashMap<String, String> internalvariableMap = new HashMap<String, String>(); // 内部变量 NodeList elementChildNodeList = element.getChildNodes(); for (int elementChildNodeIndex = 0; elementChildNodeIndex < elementChildNodeList.getLength(); elementChildNodeIndex++) { Node elementChildNode = elementChildNodeList.item(elementChildNodeIndex); if (elementChildNode instanceof Element) { if ("internalvariable".equals(elementChildNode.getNodeName())) { NodeList internalvariableNodeList = elementChildNode.getChildNodes(); for (int internalvariableNodeIndex = 0; internalvariableNodeIndex < internalvariableNodeList.getLength(); internalvariableNodeIndex++) { Node internalvariableNode = internalvariableNodeList.item(internalvariableNodeIndex); if (internalvariableNode instanceof Element) { internalvariableMap.put( internalvariableNode.getNodeName(), internalvariableNode.getTextContent() ); } } } else if ("class".equals(elementChildNode.getNodeName())) { String classname = ""; // 类名称 String methodname = ""; // 方法名称 HashMap<String, String> parameterMap = new HashMap<String, String>(); // 参数 HashMap<String, String> returnvalueMap = new HashMap<String, String>(); // 返回值 NodeList classNodeList = elementChildNode.getChildNodes(); for (int classNodeIndex = 0; classNodeIndex < classNodeList.getLength(); classNodeIndex++) { Node classNode = classNodeList.item(classNodeIndex); if (classNode instanceof Element) { if ("name".equals(classNode.getNodeName())) { classname = classNode.getTextContent(); } else if ("method".equals(classNode.getNodeName())) { methodname = classNode.getTextContent(); } else if ("parameter".equals(classNode.getNodeName())) { NodeList parameterNodeList = classNode.getChildNodes(); for (int parameterNodeIndex = 0; parameterNodeIndex < parameterNodeList.getLength(); parameterNodeIndex++) { Node parameterNode = parameterNodeList.item(parameterNodeIndex); if (parameterNode instanceof Element) { parameterMap.put( parameterNode.getNodeName(), parameterNode.getTextContent() ); } } } else if ("returnvalue".equals(classNode.getNodeName())) { NodeList returnvalueNodeList = classNode.getChildNodes(); for (int returnvalueNodeIndex = 0; returnvalueNodeIndex < returnvalueNodeList.getLength(); returnvalueNodeIndex++) { Node returnvalueNode = returnvalueNodeList.item(returnvalueNodeIndex); if (returnvalueNode instanceof Element) { returnvalueMap.put( returnvalueNode.getNodeName(), returnvalueNode.getTextContent() ); } } } else { System.out.println("unexpected classNode"); } } } // for classNodeIndex if (classname.length() > 0 && methodname.length() > 0) { HashMap<String, String> inputMap = new HashMap<String, String>(); HashMap<String, String> outputMap = new HashMap<String, String>(); Iterator<?> parameterMapIterator = parameterMap.entrySet().iterator(); while (parameterMapIterator.hasNext()) { Entry<String, String> parameterMapEntry = (Entry<String, String>) parameterMapIterator.next(); if (internalvariableMap.containsKey(parameterMapEntry.getValue())) { // 如果参数值是内部变量的名字的话,把相应的内部变量的值作为参数传递 inputMap.put( parameterMapEntry.getKey(), internalvariableMap.get(parameterMapEntry.getValue())); } else { // 如果参数值不是内部变量的名字的话,直接作为参数传递 inputMap.put( parameterMapEntry.getKey(), parameterMapEntry.getValue()); } } System.out.print(classname + "." + methodname + "\t" + inputMap); outputMap = (HashMap<String, String>) Class.forName(classname).getDeclaredMethod(methodname, HashMap.class).invoke(null, inputMap); Iterator<?> returnvalueMapIterator = returnvalueMap.entrySet().iterator(); while (returnvalueMapIterator.hasNext()) { Entry<String, String> returnvalueMapEntry = (Entry<String, String>) returnvalueMapIterator.next(); if (internalvariableMap.containsKey(returnvalueMapEntry.getValue())) { // 如果返回值是内部变量的名字的话,把相应的返回值保存到内部变量中 internalvariableMap.put(returnvalueMapEntry.getKey(), outputMap.get(returnvalueMapEntry.getKey())); } } System.out.println("\treturn" + outputMap); } else { System.out.println("classname or methodname is undefined"); } } else { System.out.println("unexpected elementChildNode"); } } } } }