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

📄 oxmlmapping.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
						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().
				INode childNode=null;
				if("".equals(xMLNode.name())){
					childNode=node.getChildNodes(this.getLocalClassName(childFields[i].getType())).get(0);
					
				}else{
					childNode=node.getChildNodes(xMLNode.name()).get(0);
				}
				this.upCastRecursion(object,childNode, childFields[i]);
								
			}
			
		}
		
		
		
		//组装
		field.set(parent, object);
		
		
	}
*/
	/**
	 * 向上转型,把INode对象转为业务对象
	 * @param node
	 * @return
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public Object upCast(INode node) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException{
		/**
		 * 
		 * 
		 * 
		 */
		String rootNodeType=node.getAttributes().get(IOXmlMapping.CLASSNAME);
		Class rootClass=Class.forName(rootNodeType);
		Object rootObject=rootClass.newInstance();
		/*
		Iterator<Entry<String, String>>  attributesIt=node.getAttributes().entrySet().iterator();
		while(attributesIt.hasNext()){
			Entry<String, String> tmpAttribute=attributesIt.next();
			if(!IOXmlMapping.CLASSNAME.equals(tmpAttribute.getKey())){
				/**
				 * 1,如果标注的名字为空
				 * 
				
				Field tmpField=rootClass.getDeclaredField(tmpAttribute.getKey());
				if(tmpField!=null){
					
				}else{
					//看他的标注是什么
	
				}
			}
		}
*/
		Field[] fields=rootClass.getDeclaredFields();
		for(int i=0;i<fields.length;i++){
			fields[i].setAccessible(true);
			XMLAttribute xMLAttribute=fields[i].getAnnotation(XMLAttribute.class);
			
			if(xMLAttribute!=null){
				if("".equals(xMLAttribute.castMethod())){
					if("".equals(xMLAttribute.name())){
						fields[i].set(rootObject, node.getAttributes().get(fields[i].getName()));
					}else{
						fields[i].set(rootObject, node.getAttributes().get(xMLAttribute.name()));
					}
					
					
				}else{
					/**
					 * 如果是工具类提供的方法之一,就执行工具类
					 * 
					 */
					if(IConvertCastUtil.castBoolean.equals(xMLAttribute.castMethod())){
						if("".equals(xMLAttribute.name())){
							fields[i].set(rootObject, this.getConvertCastUtil().castBoolean(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castDouble(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castFloat(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castInt(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castLong(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castShort(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castWrappedBoolean(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castWrappedDouble(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castWrappedFloat(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castWrappedInt(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castWrappedLong(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, 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())){
							fields[i].set(rootObject, this.getConvertCastUtil().castWrappedShort(node.getAttributes().get(fields[i].getName())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(childFields[i].getName())));
						}else{
							fields[i].set(rootObject, this.getConvertCastUtil().castWrappedShort(node.getAttributes().get(xMLAttribute.name())));
							//childFields[i].set(object, castMethod.invoke(object, node.getAttributes().get(xMLAttribute.name())));
						}
					}else{
					
					//下面是业务类自己定义的转换方法
					Method castMethod=rootClass.getDeclaredMethod(xMLAttribute.castMethod(), String.class);
					castMethod.setAccessible(true);
					if("".equals(xMLAttribute.name())){
					
					    fields[i].set(rootObject, castMethod.invoke(rootObject, node.getAttributes().get(fields[i].getName())));
					}else{
						//System.out.println(node.getAttributes().get(xMLAttribute.name()));
						fields[i].set(rootObject, castMethod.invoke(rootObject, node.getAttributes().get(xMLAttribute.name())));
					}
					}
					
				}
			
			}
			
			/**
			 *根据类的标注信息,从INode对象的数据中重新构造该对象
			 * 
			 * 
			 */
			
			
			//子节点的转换
			XMLNode xMLNode=fields[i].getAnnotation(XMLNode.class);
			if(xMLNode!=null){
				//是子节点   并且不是null
				//if(fields[i].get(rootObject)!=null){}
				/**
				 *是哪一个子节点?
				 * 

⌨️ 快捷键说明

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