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

📄 tcubaseclassstr.java

📁 为公司做的质量考核接口源码,用spring,hibernate,XML实现,对XML接口编程很有帮助
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.jr81.source.stream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.jr81.common.JrUtility;

public class TcuBaseClassSTR implements TcuBaseInterfaceSTR {

	private String PkgName;	
	private boolean DebugMode=false; 
	protected TcuBaseItemsSTR StrItems=null;//new TcuBaseItemsSTR();
	
	public TcuBaseClassSTR() {
		
	}
	
	public TcuBaseClassSTR(String packname) {
		super();
		setPkgName(packname);
		StrItems=new TcuBaseItemsSTR();
		// TODO 自动生成构造函数存根
	}
	
	public TcuBaseClassSTR(String packname,TcuBaseItemsSTR stritems) {
		super();
		setPkgName(packname);
		StrItems=stritems;
		// TODO 自动生成构造函数存根
	}

	public boolean Clear() {
		// TODO 自动生成方法存根
		StrItems.Clear();
		return true;
	}

	public void PrintDebugMsg(String msg){
		if (DebugMode){
			System.out.println(msg);
		}
	}
	
	public boolean LoadFromFile(String FileName){
		// TODO 自动生成方法存根
		try{
			InputStream fin=new FileInputStream(FileName);
			return LoadFromStream(fin);
			}catch(Exception e){
				return false;
			}
	}

	public boolean SaveToFile(String FileName) {
		// TODO 自动生成方法存根
		try{
			FileOutputStream fou=new FileOutputStream(FileName);
			return SaveToStream(fou);
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
	}

	public boolean LoadFromStream(InputStream value)  {
		// TODO 自动生成方法存根
		try {
			Clear();
			//System.out.println("PackName:"+PkgName);
			if (value==null){
				System.out.println("BaseClassSTR LoadFromStream error,Null bytearray");
				return false;
			}
			if (!StrItems.fromStream(value)) return false;
			if (!ParseStrItems()) return false;
			return true;
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return false;
		
	}

	public boolean SaveToStream(OutputStream value) {
		// TODO 自动生成方法存根
		try {
			//System.out.println("PackName:"+PkgName);
			StrItems.Clear();
			if (!CreateStrItems()) return false;
			ByteArrayOutputStream out=(ByteArrayOutputStream)StrItems.toStream();
			if (out==null) return false;
			value.write(out.toByteArray());
			out.close();
			return true;
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (Exception e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return false;
	}
	
	public String SaveToXML(){
		return GetXMLItem(PkgName,GetXMLItems());
	}
	
	public String GetXMLItems(){
		PrintStream print=System.out;
		String xml="";
		String name=null;
		String value=null;
		Class fieldTypeClass=null;
		try{
			Field[] fields= this.getClass().getDeclaredFields();
			Method[] methods=this.getClass().getDeclaredMethods();
			Object[] args=new Object[0];
			for (int i=0;i<fields.length;i++){
				Field field=fields[i];
				name=field.getName();
				print.println(name);
				print.println(field.getType().toString());
				fieldTypeClass=field.getType();
				value=null;
	            if (fieldTypeClass==String.class){ 	               
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
	            		value=(String)InvokeStaticMethod(this,method.getName(),args);
	            	}
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==Integer.class){
	               	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
	            		value=((Integer)InvokeStaticMethod(this,method.getName(),args)).toString();
	            	}
	            	xml+=GetXMLItem(name,value);
		        }
	            else if (fieldTypeClass==long.class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
		            	value=((Long)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	}  
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==short.class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
		            	value=((Short)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	} 
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==float.class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
		            	value=((Float)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	}
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==double.class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
		            	value=((Double)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	}
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==byte.class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
		            	value=((Byte)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	}
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==char.class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
		            	value=((Character)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	}
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==Date.class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
		            	value=((Date)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	}
	            	xml+=GetXMLItem(name,value);
	            }
	            else if (fieldTypeClass==boolean.class){
	            	Method method=CheckMethod(methods,"is"+name);
	            	if (method!=null){
		            	value=((Boolean)InvokeStaticMethod(this,method.getName(),args)).toString();	
	            	}
	            	xml+=GetXMLItem(name,value); 
	            }
	            else if (fieldTypeClass==byte[].class){
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
	            		byte[] v=(byte[])InvokeStaticMethod(this,method.getName(),args);
	            		if (v!=null) {
	            			value=JrUtility.getBASE64(new String(v));
	            		}
	            	}
	            	xml+=GetXMLItem(name,value);
	            }	           
	            else {
	            	Method method=CheckMethod(methods,"get"+name);
	            	if (method!=null){
	            		Object obj=InvokeStaticMethod(this,method.getName(),args);
	            		if (TcuBaseClassSTR.class.isInstance(obj)){
	            			value=((TcuBaseClassSTR)obj).GetXMLItems();
	            			xml+=GetXMLItem(name,value);
	            			//xml+=((TcuBaseClassSTR)obj).SaveToXML();
	            		}else {
		
	            			//value=(String)obj;
	            			//xml+=GetXMLItem(name,value);
	            		}
	            	}
	            }	           
	            //print.println(value);
	        }
		}
        catch(Exception ex){
        	ex.printStackTrace();
        }
        xml+=GetCustItems();
		return xml;		
	}
	
	public String SaveToXMLWithHead(){
		String xml="<?xml version='1.0' encoding='gb2312'?>"; // XML的头部信息
		xml+="<GZJRDP Version='1.0'>";
		xml+=SaveToXML();
		xml+="</GZJRDP>";
		return xml;
	}
	
	protected String GetXMLItem(String name,String value){
		if (value==null){
			value="";
		}
		return "<"+name+">"+value+"</"+name+">";
	}	
	
	public Method CheckMethod(Method[] methods,String method_name){
		for (int i=0;i<methods.length;i++){
			Method method=methods[i];
			if (method.getName().toUpperCase().equals(method_name.toUpperCase())){
				return method;
			}
		}
		return null;
	}
	
	public Object InvokeStaticMethod(Object owner, String methodName,
			Object[] args) {
		Class ownerClass = owner.getClass();//Class.forName(className);
		Class[] argsClass = new Class[args.length];
		for (int i = 0, j = args.length; i < j; i++) {
			argsClass[i] = args[i].getClass();
		}
		try {
			Method method = ownerClass.getMethod(methodName, argsClass);
			return method.invoke(owner, args);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {

⌨️ 快捷键说明

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