⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oxmlmapping.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
								}else{
									childFields[i].set(object, this.getConvertCastUtil().castWrappedFloat(node.getAttributes().get(xMLAttribute.name())));
									//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
								}
							}else if(IConvertCastUtil.castWrappedInt.equals(xMLAttribute.castMethod())){
								if("".equals(xMLAttribute.name())){
									childFields[i].set(object, this.getConvertCastUtil().castWrappedInt(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().castWrappedInt(node.getAttributes().get(xMLAttribute.name())));
									//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
								}
							}else if(IConvertCastUtil.castWrappedLong.equals(xMLAttribute.castMethod())){
								if("".equals(xMLAttribute.name())){
									childFields[i].set(object, this.getConvertCastUtil().castWrappedLong(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().castWrappedLong(node.getAttributes().get(xMLAttribute.name())));
									//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
								}
							}else if(IConvertCastUtil.castWrappedShort.equals(xMLAttribute.castMethod())){
								if("".equals(xMLAttribute.name())){
									childFields[i].set(object, this.getConvertCastUtil().castWrappedShort(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().castWrappedShort(node.getAttributes().get(xMLAttribute.name())));
									//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
								}
							}else{
							
							//下面是业务类自己定义的转换方法
							Method castMethod=clazz.getDeclaredMethod(xMLAttribute.castMethod(), String.class);
							castMethod.setAccessible(true);
							if("".equals(xMLAttribute.name())){
							
								childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
							}else{
								childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
							}
							}
							
						}
					
					}
					/**
					 * 子节点
					 * 
					 */
					XMLNode xMLNode=childFields[i].getAnnotation(XMLNode.class);
					if(xMLNode!=null){
						//是子节点   并且不是null
						//if(fields[i].get(rootObject)!=null){}
						/**
						 *是哪一个子节点?
						 * 1,
						 * 
						 * 
						 */
						//node.getChildNodes().
						List<INode> childNodes=null;
						if("".equals(xMLNode.name())){
							childNodes=node.getChildNodes(this.getLocalClassName(childFields[i].getType()));
							
						}else{
							childNodes=node.getChildNodes(xMLNode.name());
						}
						this.upCastRecursion(object,childNodes, childFields[i]);
										
					}
					
				}
				
				
				
				
				Array.set(array, nodesIndex, object);
			}
			
			//组装数组
			field.set(parent, array);

			
		}else if(ClassUtil.isImplementsInterface(field.getType(), Iterable.class)){
			/**
			 * 如果是集合
			 * 1,建立集合对象,
			 * 2,增加对象-----怎样增加?
			 * 3,设置对象
			 */
			//String nodeType=nodes.get(0).getAttributes().get(IOXmlMapping.CLASSNAME);
			
			
			//Iterable  iterableObject=(Iterable) Class.forName(iterableType).newInstance();
			//集合是Iterable接口的唯一直接子接口!
			/**
			 * 生成集合的类型
			 * 	XMLNode xMLNode=childFields[i].getAnnotation(XMLNode.class);
			 */
			XMLNode xMLNodeField=field.getAnnotation(XMLNode.class);
			Collection collection=null;
			if("".equals(xMLNodeField.concreteClass())){
				String iterableType=field.getType().getName();
				collection=(Collection) Class.forName(iterableType).newInstance();
				
			}else{
				collection=(Collection) Class.forName(xMLNodeField.concreteClass()).newInstance();
			}
			
			
			
			//Class.forName(iterableType).cast(objects);
			
			for(INode node:nodes){
			    	String nodeType=node.getAttributes().get(IOXmlMapping.CLASSNAME);
					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())));
									}else{
										childFields[i].set(object, this.getConvertCastUtil().castWrappedFloat(node.getAttributes().get(xMLAttribute.name())));
										//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
									}
								}else if(IConvertCastUtil.castWrappedInt.equals(xMLAttribute.castMethod())){
									if("".equals(xMLAttribute.name())){
										childFields[i].set(object, this.getConvertCastUtil().castWrappedInt(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().castWrappedInt(node.getAttributes().get(xMLAttribute.name())));
										//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
									}
								}else if(IConvertCastUtil.castWrappedLong.equals(xMLAttribute.castMethod())){
									if("".equals(xMLAttribute.name())){
										childFields[i].set(object, this.getConvertCastUtil().castWrappedLong(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().castWrappedLong(node.getAttributes().get(xMLAttribute.name())));
										//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
									}
								}else if(IConvertCastUtil.castWrappedShort.equals(xMLAttribute.castMethod())){
									if("".equals(xMLAttribute.name())){
										childFields[i].set(object, this.getConvertCastUtil().castWrappedShort(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().castWrappedShort(node.getAttributes().get(xMLAttribute.name())));
										//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
									}
								}else{
								
								//下面是业务类自己定义的转换方法
								Method castMethod=clazz.getDeclaredMethod(xMLAttribute.castMethod(), String.class);
								castMethod.setAccessible(true);
								if("".equals(xMLAttribute.name())){
								
									childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
								}else{
									childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
								}
								}
								
							}
						
						}
						/**
						 * 子节点
						 * 
						 */
						XMLNode xMLNode=childFields[i].getAnnotation(XMLNode.class);
						if(xMLNode!=null){
							//是子节点   并且不是null
							//if(fields[i].get(rootObject)!=null){}
							/**
							 *是哪一个子节点?
							 * 1,
							 * 
							 * 
							 */
							//node.getChildNodes().
							List<INode> childNodes=null;
							if("".equals(xMLNode.name())){
								childNodes=node.getChildNodes(this.getLocalClassName(childFields[i].getType()));
								
							}else{
								childNodes=node.getChildNodes(xMLNode.name());
							}
							this.upCastRecursion(object,childNodes, childFields[i]);
											
						}
						
					}
					
					
					collection.add(object);
				
			}
			
					//组装
					field.set(parent, collection);
			
			
		}else{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -