📄 oxmlmapping.java
字号:
childNode.setName(childNodeType.name());
}
//类的全名作为节点的属性名,用于反向构建对象。
//不能是数组的全名
childNode.getAttributes().put(IOXmlMapping.CLASSNAME,tmp.getClass().getName());
childNode.setEncoding(childNodeType.encoding());
childNode.setRoot(childNodeType.root());
/**
* 再拿子节点的属性和子节点的字节点
*/
this.downCastRecursion(rootNode,childNode, tmp);
}
}
}else{
// 子节点是单个的
/**
* 儿子节点
*
*/
//node.getChildNodes().add(e)
INode childNode=new Node();
if("".equals(childNodeType.name())){
childNode.setName(fields[i].getName());
}else{
childNode.setName(childNodeType.name());
}
//类的全名作为节点的属性名,用于反向构建对象。
childNode.getAttributes().put(IOXmlMapping.CLASSNAME,fields[i].getType().getName());
childNode.setEncoding(childNodeType.encoding());
childNode.setRoot(childNodeType.root());
/**
* 再拿子节点的属性和子节点的字节点
*/
fields[i].setAccessible(true);
if(fields[i].get(object)!=null){
this.downCastRecursion(rootNode,childNode, fields[i].get(object));
}
}
}
}
return rootNode;
}
}
/**
* 从上层到下层,递归取子节点和属性
* @param parent
* @param object
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
private void downCastRecursion(INode parent,INode node,Object object) throws IllegalArgumentException, IllegalAccessException{
parent.getChildNodes().add(node);
/*
//对象的根节点
XMLType rootNodeType=object.getClass().getAnnotation(XMLType.class);
if("".equals(rootNodeType.name())){
node.setName(getLocalClassName(object.getClass()));
}else{
node.setName(rootNodeType.name());
}
node.setEncoding(rootNodeType.encoding());
node.setRoot(rootNodeType.root());
*/
//属性
Field[] fields=object.getClass().getDeclaredFields();
for(int i=0;i<fields.length;i++){
XMLAttribute attribute=fields[i].getAnnotation(XMLAttribute.class);
if(attribute!=null){
//node.getAttributes().put(key, value)
String key,value;
if("".equals(attribute.name())){
key=fields[i].getName();
}else{
key=attribute.name();
}
if("".equals(attribute.value())){
fields[i].setAccessible(true);
value=fields[i].get(object).toString();
}else{
value=attribute.value();
}
node.getAttributes().put(key, value);
}
//子节点
/**
* 循环执行 节点和属性的
*
*/
XMLNode childNodeType=fields[i].getAnnotation(XMLNode.class);
if(childNodeType!=null){
//downCastRecursion(node,fields[i].get(object));
/**
* 儿子节点
*
*/
//node.getChildNodes().add(e)
INode childNode=new Node();
if("".equals(childNodeType.name())){
childNode.setName(fields[i].getName());
}else{
childNode.setName(childNodeType.name());
}
//类的全名作为节点的属性名,用于反向构建对象。
childNode.getAttributes().put(IOXmlMapping.CLASSNAME,fields[i].getType().getName());
childNode.setEncoding(childNodeType.encoding());
childNode.setRoot(childNodeType.root());
/**
* 再拿子节点的属性和子节点的字节点
*/
this.downCastRecursion(node,childNode, fields[i].get(object));
}
}
}
/**
* 如果是 子节点的字段是集合或者数组,用这个
* 如果是单独的,则nodes的尺寸是1。
* @param parent
* @param nodes
* @param field
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
private void upCastRecursion(Object parent,List<INode> nodes,Field field) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{
/**
* 1,取出所有INode子节点,然后,给字段加上
*
*
*/
if(nodes==null){
return;
}
if(nodes.size()==0){
return;
}
field.setAccessible(true);
/**
* field的类型
* 如果是数组,构造数组,
*
*
*/
if(field.getType().isArray()){
String nodeType=nodes.get(0).getAttributes().get(IOXmlMapping.CLASSNAME);
/**
* 生成数组的类型
*
*/
Object array=Array.newInstance(Class.forName(nodeType), nodes.size());
for(int nodesIndex=0;nodesIndex<nodes.size();nodesIndex++){
INode node=nodes.get(nodesIndex);
Class clazz=Class.forName(nodeType);
Object object=clazz.newInstance();
/**
* 属性
*
*/
Field[] childFields=clazz.getDeclaredFields();
for(int i=0;i<childFields.length;i++){
childFields[i].setAccessible(true);
XMLAttribute xMLAttribute=childFields[i].getAnnotation(XMLAttribute.class);
if(xMLAttribute!=null){
if("".equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, node.getAttributes().get(childFields[i].getName()));
}else{
childFields[i].set(object, node.getAttributes().get(xMLAttribute.name()));
}
}else{
/**
* 如果是工具类提供的方法之一,就执行工具类
*
*/
if(IConvertCastUtil.castBoolean.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castBoolean(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castBoolean(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castDouble.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castDouble(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castDouble(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castFloat.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castFloat(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castFloat(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castInt.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castInt(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castInt(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castLong.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castLong(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castLong(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castShort.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castShort(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castShort(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castWrappedBoolean.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castWrappedBoolean(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castWrappedBoolean(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castWrappedDouble.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castWrappedDouble(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
}else{
childFields[i].set(object, this.getConvertCastUtil().castWrappedDouble(node.getAttributes().get(xMLAttribute.name())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
}
}else if(IConvertCastUtil.castWrappedFloat.equals(xMLAttribute.castMethod())){
if("".equals(xMLAttribute.name())){
childFields[i].set(object, this.getConvertCastUtil().castWrappedFloat(node.getAttributes().get(childFields[i].getName())));
//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -