1.方法表
getValues方法中的argumentMaker封装了Gson,解析参数字符串。
package edu.xupt.cs.factory.xml;
import edu.xupt.cs.action.abstract_.BeanDefination;
import edu.xupt.cs.factory.ArgumentMaker;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
public class ActionBeanDefination extends BeanDefination {
private final List<String> paraNames = new ArrayList<>();
private int index = 0;
public void addParaName(String name) {
if (!paraNames.contains(name)) {
paraNames.add(name);
}
}
public boolean hasNext() {
boolean hasNext = index < paraNames.size();
if (!hasNext) {
index = 0;
}
return hasNext;
}
public String next() {
return paraNames.get(index++);
}
@Override
public Object[] getValues(Parameter[] parameters,String strValues) {
ArgumentMaker argumentMaker = new ArgumentMaker(strValues);
Object[] values = new Object[parameters.length];
for (int i = 0; hasNext(); i++) {
String argumentName = next();
values[i] = argumentMaker.getArgument(argumentName, parameters[i].getParameterizedType());
}
return values;
}
}
2.静态工厂
构建请求与方法映射关系。
package edu.xupt.cs.factory.xml;
import com.mec.util.XMLParse;
import edu.xupt.cs.action.abstract_.ActionBeanFactory;
import edu.xupt.cs.action.abstract_.BeanDefination;
import org.w3c.dom.Element;
import java.lang.reflect.Method;
import java.util.*;
public class XMLActionBeanFactory extends ActionBeanFactory {
public BeanDefination getAction(String actionName) {
return actionPool.get(actionName);
}
@Override
public void scannActionMapping(String XMLPath) {
new XMLParse() {
@Override
public void dealElement(Element element, int index) {
try {
String methodName = element.getAttribute("method");
String action = element.getAttribute("action");
String klassName = element.getAttribute("class");
Class<?> klass = Class.forName(klassName);
ActionBeanDefination abd = new ActionBeanDefination();
abd.setObject(klass.newInstance());
abd.setKlass(klass);
abd.setMethod(getMethod(klass,methodName,element,abd));
actionPool.put(action,abd);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
}
}.parseTagByDocument(XMLParse.getDocument(XMLPath), "action");
}
private Method getMethod(Class<?> klass, String methodName, Element element,
ActionBeanDefination abd) {
try {
List<String> typeNames = new ArrayList<>();
new XMLParse() {
@Override
public void dealElement(Element element, int index) {
String name = element.getAttribute("name");
String type = element.getAttribute("type");
abd.addParaName(name);
typeNames.add(type);
}
}.parseTagByElement(element, "parameter");
Class<?>[] types = new Class[typeNames.size()];
for (int i = 0; i < typeNames.size(); i++) {
types[i] = MakeType.getType(typeNames.get(i));
}
return klass.getMethod(methodName, types);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
}
3.包装类型拆箱
方法参数中,基本八大类型,会转换为包装类型,导致Gson字符串解析时,解析失败,那就需要设计将包装类型转换为基本类型的类。
package edu.xupt.cs.factory.xml;
public class MakeType {
public static Class<?> getType(String typeName) {
if (typeName.equalsIgnoreCase("int")) {
return int.class;
}
if (typeName.equalsIgnoreCase("long")) {
return long.class;
}
if (typeName.equalsIgnoreCase("short")) {
return short.class;
}
if (typeName.equalsIgnoreCase("char")) {
return char.class;
}
if (typeName.equalsIgnoreCase("double")) {
return double.class;
}
if (typeName.equalsIgnoreCase("float")) {
return float.class;
}
if (typeName.equalsIgnoreCase("byte")) {
return byte.class;
}
if (typeName.equalsIgnoreCase("String")) {
return String.class;
}
try {
return Class.forName(typeName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
4.XML
客户端:
<?xml version="1.0" encoding="UTF-8" ?>
<mappings>
<action action = "userLogin" class = "com.mec.review.netWork.view.client.UserLogInView" method = "afterLogin">
<parameter name="user" type="com.mec.review.netWork.userModel.UserInfo"/>
</action>
</mappings>
服务器:
<?xml version="1.0" encoding="UTF-8" ?>
<mappings>
<action action = "userLogin" class = "edu.xupt.cs.netWork.userAction.UserAction" method = "userLogin">
<parameter name="id" type="String"/>
<parameter name="password" type="String"/>
</action>
</mappings>