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

📄 tcuxmlmaplists.java

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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

/**
 * @author Administrator
 *
 */
public class TcuXmlMapLists implements TcuInterfaceXmlLists {

	private Map Lists = new HashMap();
	private String XmlHead = "<?xml version='1.0' encoding = 'GB2312'?> ";
	private String Xml1 = "<Lists>";
	private String Xml2 = "</Lists>";
	
	public TcuXmlMapLists() {
		
	}
	
	public void Clear() {
		Lists.clear();
	}
	
	public int GetCount() {
		//List it = (List) Lists.values();
		//return it.size();
		return Lists.size();
	}
	
	public int GetLength() {
		int Length = Xml1.length() + Xml2.length();
		for (int i = 0; i < GetCount(); i++) {
			Length += GetList(i).GetLength();
		}
		return Length;
	}
	
	public void AddList(TcuXmlList value) {
		if (Lists.containsKey(value.getName())) {
			((TcuXmlList)Lists.get(value.getName())).Assign(value);
		} else {
			TcuXmlList BaseItem = new TcuXmlList();
			BaseItem.Assign(value);
			String key = new String(value.getName());
			Lists.put(key , BaseItem);
		}
	}
	
	public void AddList(byte[] name, byte[] value) {
		TcuXmlList newList = new TcuXmlList();
		newList.setName(name);
		newList.setValue(value);
		AddList(newList);
	}
	
	public void AddList(byte[] name, byte[] value, int type) {
		// TODO Auto-generated method stub
		if (type == 0) {//default
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setValue(value);
			AddList(newList);
		} else if (type == 1) {//Base64
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setBase64Value(value);
			AddList(newList);
		} else if (type == 2) {//Compress
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setCompressValue(value);
			AddList(newList);
		} else if (type == 3) {//CompressBase64
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setCompressBase64Value(value);
			AddList(newList);
		} else {//default
			TcuXmlList newList = new TcuXmlList();
			newList.setName(name);
			newList.setValue(value);
			AddList(newList);
		}
	}
	
	public void DeleteList(int index) {
		//Lists.remove();
		int i = 0;
		Collection it = Lists.values();
		Iterator iterator = it.iterator();
		while (iterator.hasNext()) {
			TcuXmlList BaseItem = (TcuXmlList)iterator.next();
			if (i == index) {
				Lists.remove(BaseItem.getName());
			}
			i++;
		}
	}
	
	public void DeleteList(String name) {
		// TODO Auto-generated method stub
		Lists.remove(name);
	}
	
	public TcuXmlList GetList(int index) {
		TcuXmlList ResultList = null;
		int i = 0;
		Collection it = Lists.values();
		Iterator iterator = it.iterator();
		while (iterator.hasNext()) {
			TcuXmlList BaseItem = (TcuXmlList)iterator.next();
			if (i == index) {
				//Lists.remove(BaseItem.getName());
				ResultList = BaseItem;
			}
			i++;
		}
		return ResultList;
		//return (TcuList)Lists.elementAt(index);
	}
	
	public TcuXmlList GetList(String name) {
		if (Lists.containsKey(name)) {
			return (TcuXmlList)Lists.get(name);
		} else {
			return null;
		}
	}
	
	public byte[] GetListByteValue (String name) {
		if (Lists.containsKey(name)) {
			return ((TcuXmlList)Lists.get(name)).getValue();
		} else {
			return null;
		}
	}
	
	public String GetListStrValue(String name) {
		if (Lists.containsKey(name)) {
			byte[] tmpValue = ((TcuXmlList)Lists.get(name)).getValue();
			if (tmpValue != null) {
			String strResult = new  String(tmpValue);
			return strResult;
			} else {
				return "";
			}
		} else {
			return "";
		}
	}
	
	public void Assign(TcuLists value) {
		Clear();
		for (int i = 0; i < value.GetCount(); i++) {
			TcuXmlList tmpList = new TcuXmlList();
			tmpList.Assign(value.GetList(i));
			AddList(tmpList);
		}
	}
	
	public boolean FromByteXml(byte[] value) {
		Clear();
		SAXBuilder sb = new SAXBuilder();
		Document MyDocument = new Document();
		
		ByteArrayOutputStream Output = new ByteArrayOutputStream(XmlHead.length() + value.length);
		Output.write(XmlHead.getBytes(),0, XmlHead.length());
		Output.write(value,0,value.length);
		ByteArrayInputStream InputStream = new ByteArrayInputStream(Output.toByteArray());
		
		try {
			MyDocument=sb.build(InputStream);
			
			Element root = MyDocument.getRootElement();
			List ChildrenList = root.getChildren("List");
			
			Iterator i = ChildrenList.iterator();
			while(i.hasNext()) {
				Element List1 = (Element)i.next();
				TcuXmlList tmpList = new TcuXmlList();
				tmpList.setName(List1.getChildText("Name").getBytes());
				tmpList.setValue(List1.getChildText("Value").getBytes());
				AddList(tmpList);
			}
			
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public byte[] ToByteXml() throws IOException {
		
		
		ByteArrayOutputStream value = new ByteArrayOutputStream(GetLength());
		value.write(Xml1.getBytes(), 0, Xml1.length());
		for (int i = 0; i < GetCount(); i++) {
			TcuXmlList tmpList = GetList(i);
			value.write(tmpList.ToByteXml(), 0, tmpList.GetLength());
		}
		value.write(Xml2.getBytes(), 0, Xml2.length());
		
		return value.toByteArray();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public void AddList(byte[] name, boolean value) {
		// TODO Auto-generated method stub
		
	}

	public void AddList(byte[] name, int value) {
		// TODO Auto-generated method stub
		
	}

	public void AddList(byte[] name, float value) {
		// TODO Auto-generated method stub
		
	}

	public boolean GetListBooleanValue(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	public float GetListFloatValue(String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int GetListIntValue(String name) {
		// TODO Auto-generated method stub
		return 0;
	}

	

	

}

⌨️ 快捷键说明

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