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

📄 dbdealdatasavexml.java

📁 J2EE eclipse 下开发数据库一个插件
💻 JAVA
字号:
/*
 * Created on 2003-5-6
 *
 */
package com.tanghan.plugin.dataSave;

//import java.io.File;
import java.io.File;
//import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.w3c.dom.*;
import org.eclipse.core.internal.boot.PlatformURLHandler;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.tanghan.db.util.DBConnection;
import com.tanghan.db.util.Driver;
import com.tanghan.plugin.ITanghanConstants;
//import com.tanghan.plugin.TanghanClassLoader;
import com.tanghan.plugin.dataSave.db.DBConnectionSave;
import com.tanghan.plugin.dataSave.db.DriverSave;
import com.tanghan.util.DealString;
import com.tanghan.util.TanghanException;
import com.tanghan.util.XMLUtility;

/**
 * @author Jerry Tang
 * @version v0.1.0
 * @copyright  (C) 2003 Tanghan Studio
 *
 */
public class DBDealDataSaveXML implements IDBDealDataSave {

	private static final URL URL_BASIC =
		Platform
			.getPlugin(ITanghanConstants.PLUGIN_ID)
			.getDescriptor()
			.getInstallURL();

	private static final String SAVEFILENAME = "TanghanWSSave.xml";

	private File saveFile = null;

	private HashMap connectionList = null;

	private HashMap adpaterList = null;

	private Element driversElement = null;

	private Element rootElement = null;

	private Element dbConnsElement = null;

	private Document doc;

	private ArrayList driversList;
	private ArrayList dbConnectionsList;

	public DBDealDataSaveXML() {
		driversList = new ArrayList();
		dbConnectionsList = new ArrayList();
		connectionList = new HashMap();
		adpaterList = new HashMap();
	}
	private void checkElement() {
		if (driversElement == null) {
			driversElement = doc.createElement("Drivers");
			rootElement.appendChild(driversElement);
		}
		if (dbConnsElement == null) {
			dbConnsElement = doc.createElement("DBConnections");
			rootElement.appendChild(dbConnsElement);
		}
	}
	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#addDBConnectionInfo(com.tanghan.db.util.DBConnection)
	 */
	public DBConnection addDBConnectionInfo(DBConnection connInfo) {
		if (connInfo == null)
			return connInfo;
		checkElement();
		DBConnectionSave dbConnSave = null;
		if (connInfo instanceof DBConnectionSave) {
			dbConnSave = (DBConnectionSave) connInfo;
		} else {
			dbConnSave =
				new DBConnectionSave(connInfo, System.currentTimeMillis() + "");
		}
		dbConnectionsList.add(dbConnSave);
		Element elem = doc.createElement("DBConnection");
		elem.setAttribute("ConnectionName", dbConnSave.getConnectionName());
		elem.setAttribute("jdbcDriverName", dbConnSave.getJdbcDriverName());
		elem.setAttribute("jdbcDriver", dbConnSave.getJdbcDriverClass());
		elem.setAttribute("jdbcURL", dbConnSave.getDatabaseURL());
		elem.setAttribute("UserName", dbConnSave.getUserName());
		elem.setAttribute("Password", dbConnSave.getPassword());
		elem.setAttribute("SerialUID", dbConnSave.getSerialUID());
		dbConnsElement.appendChild(elem);
		connectionList.put(dbConnSave.getSerialUID(), elem);
		return dbConnSave;
	}
	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#updateDBConnectionInfo(com.tanghan.db.util.DBConnection)
	 */
	public void updateDBConnectionInfo(DBConnection connInfo) {
		if (connInfo == null)
			return;
		DBConnectionSave dbConnSave = null;
		if (connInfo instanceof DBConnectionSave) {
			dbConnSave = (DBConnectionSave) connInfo;
			Element elem =
				(Element) connectionList.get(dbConnSave.getSerialUID());
			elem.setAttribute("ConnectionName", dbConnSave.getConnectionName());
			elem.setAttribute("jdbcDriverName", dbConnSave.getJdbcDriverName());
			elem.setAttribute("jdbcDriver", dbConnSave.getJdbcDriverClass());
			elem.setAttribute("jdbcURL", dbConnSave.getDatabaseURL());
			elem.setAttribute("UserName", dbConnSave.getUserName());
			elem.setAttribute("Password", dbConnSave.getPassword());
			elem.setAttribute("SerialUID", dbConnSave.getSerialUID());
		} else {
			return;
		}
	}
	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#removeDBConnectionInfo(com.tanghan.db.util.DBConnection)
	 */
	public void removeDBConnectionInfo(DBConnection connInfo) {
		if (connInfo == null)
			return;
		DBConnectionSave dbConnSave = null;
		if (connInfo instanceof DBConnectionSave) {
			dbConnSave = (DBConnectionSave) connInfo;
			Node nd = (Node) connectionList.get(dbConnSave.getSerialUID());
			dbConnsElement.removeChild(nd);
			//connectionList.remove(dbConnSave.getSerialUID());
		} else {
			return;
		}
	}

	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#addJDBCDriver(com.tanghan.db.util.Driver)
	 */
	 
	public Driver addJDBCDriver(Driver driver) {
		// TO_DO Auto-generated method stub		if(connInfo==null)
		if (driver == null)
			return driver;
		checkElement();
		DriverSave driverSave = null;
		if (driver instanceof DriverSave) {
			driverSave = (DriverSave) driver;
		} else {
			driverSave =
				new DriverSave(driver, System.currentTimeMillis() + "");
		}
		//
		dbConnectionsList.add(driverSave);
		Element elem = doc.createElement("Driver");
		elem.setAttribute("Name", driverSave.getDriverName());
		elem.setAttribute("URLPrefix", driverSave.getURLPrefix());
		elem.setAttribute("DriverClass", driverSave.getDriverClass());
		elem.setAttribute("JarFileName", driverSave.getJarFileName());
		elem.setAttribute("SerialUID", driverSave.getSerialUID());
		driversElement.appendChild(elem);
		adpaterList.put(driverSave.getSerialUID(), elem);

		return driverSave;
	}

	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#updateJDBCDriver(com.tanghan.db.util.Driver)
	 */
	public void updateJDBCDriver(Driver driver) {
		// TO_DO Auto-generated method stub
		if (driver == null)
			return;
		DriverSave driverSave = null;
		if (driver instanceof DriverSave) {
			driverSave = (DriverSave) driver;
			Element elem =
				(Element) adpaterList.get(driverSave.getSerialUID());
			elem.setAttribute("Name", driverSave.getDriverName());
			elem.setAttribute("URLPrefix", driverSave.getURLPrefix());
			elem.setAttribute("DriverClass", driverSave.getDriverClass());
			elem.setAttribute("JarFileName", driverSave.getJarFileName());

		} else {
			return;
		}
	}
	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#removeJDBCDriver(com.tanghan.db.util.Driver)
	 */
	public void removeJDBCDriver(Driver driver) {
		// TO_DO Auto-generated method stub		
		if (driver == null)
			return;
		DriverSave driverSave = null;
		if (driver instanceof DriverSave) {
			driverSave = (DriverSave) driver;
			Node nd = (Node) adpaterList.get(driverSave.getSerialUID());
			driversElement.removeChild(nd);
		} else {
			return;
		}
	}

	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#getDBConnectionInfos()
	 */
	public List getDBConnectionInfos() {
		return dbConnectionsList;
	}

	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDBDealDataSave#getJDBCDrivers()
	 */
	public List getJDBCDrivers() {
		return driversList;
	}
	private void checkFile() {

	}
	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDealDataSave#loadData(org.eclipse.ui.plugin.AbstractUIPlugin)
	 */
	public void loadData(AbstractUIPlugin plugin) throws TanghanException {
		saveFile = plugin.getStateLocation().append(SAVEFILENAME).toFile();
		String strSaveFile = "";
		if (saveFile.exists()) {
			strSaveFile = saveFile.toString();
		} else {
			URL url = null;
			try {
				url = new URL(URL_BASIC, SAVEFILENAME);
			} catch (MalformedURLException e) {
				throw new TanghanException(e);
			}
			strSaveFile = url.toString();
		}
		try {
			doc = XMLUtility.xml2Document(strSaveFile);
			//得到XML文件的根目录
			rootElement = doc.getDocumentElement();
			//得到所有的JDBC驱动信息
			driversElement =
				(Element) (rootElement.getElementsByTagName("Drivers").item(0));
			NodeList nodelist = driversElement.getElementsByTagName("Driver");
			for (int i = 0; i < nodelist.getLength(); i++) {
				DriverSave ds =
					new DriverSave(
						DealString.trim(
							XMLUtility.getNodeAttr(
								driversElement,
								"Driver",
								"Name",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								driversElement,
								"Driver",
								"URLPrefix",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								driversElement,
								"Driver",
								"DriverClass",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								driversElement,
								"Driver",
								"JarFileName",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								driversElement,
								"Driver",
								"SerialUID",
								i)));
				driversList.add(ds);
				adpaterList.put(ds.getSerialUID(), nodelist.item(i));
			}

			//得到所有的连接信息
			dbConnsElement =
				(Element) (rootElement
					.getElementsByTagName("DBConnections")
					.item(0));
			nodelist = dbConnsElement.getElementsByTagName("DBConnection");
			for (int i = 0; i < nodelist.getLength(); i++) {
				DBConnectionSave sa =
					new DBConnectionSave(
						DealString.trim(
							XMLUtility.getNodeAttr(
								dbConnsElement,
								"DBConnection",
								"ConnectionName",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								dbConnsElement,
								"DBConnection",
								"jdbcDriverName",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								dbConnsElement,
								"DBConnection",
								"jdbcDriver",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								dbConnsElement,
								"DBConnection",
								"jdbcURL",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								dbConnsElement,
								"DBConnection",
								"UserName",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								dbConnsElement,
								"DBConnection",
								"Password",
								i)),
						DealString.trim(
							XMLUtility.getNodeAttr(
								dbConnsElement,
								"DBConnection",
								"SerialUID",
								i)));
				dbConnectionsList.add(sa);
				connectionList.put(sa.getSerialUID(), nodelist.item(i));
			}
		} catch (Exception ex) {
			throw new TanghanException(ex);
		}

	}
	private String getFileFromURL(URL target) {
		String protocol = target.getProtocol();
		if (protocol.equals(PlatformURLHandler.FILE))
			return target.getFile();
		if (protocol.equals(PlatformURLHandler.JAR)) {
			// strip off the jar separator at the end of the url then do a recursive call
			// to interpret the sub URL.
			String file = target.getFile();
			file =
				file.substring(
					0,
					file.length() - PlatformURLHandler.JAR_SEPARATOR.length());
			try {
				return getFileFromURL(new URL(file));
			} catch (MalformedURLException e) {
			}
		}
		return null;
	}
	/* (non-Javadoc)
	 * @see com.tanghan.plugin.dataSave.IDealDataSave#saveData()
	 */
	public void saveData() throws TanghanException {
		if (doc != null) {
			XMLUtility.document2XML(doc, saveFile.toString());
		}
	}
	/**
	 * 载入该java类包
	 * */
	private void loadClassPackage(String fileName) {

	}

}

⌨️ 快捷键说明

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