DeleteTempFile.java 3.29 KB
Newer Older
TongZuu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
package com.csc.library.utility;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

import org.jdom.Element;

import com.csc.library.database.MyHashMap;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.ReadXml;

/*
 * Ѻź temp к
 * ҹٻẺź  path ͧèź
 * Ҩҡ xml 
 */
public class DeleteTempFile {

	private String xmlFile = "";
	private boolean isCopy = false;
	private String dstPath = "";
	private Vector vDir = new Vector();
	
	public DeleteTempFile() {
		
	}
	
	public DeleteTempFile(String xmlFile) {
		this.xmlFile = xmlFile;
	}
	
	public void setXmlFile(String xmlFile) {
		this.xmlFile = xmlFile;
	}
	
	public String getXmlFile() {
		return this.xmlFile;
	}
	
	public void process() throws IOException {
		FileUtility fu = new FileUtility();
		for (int i=0; i<this.vDir.size(); i++) {
			MyHashMap hm = (MyHashMap) this.vDir.elementAt(i);
			String dir = hm.getString("dir");
			Vector fileext = (Vector) hm.get("fileext");
			boolean isCopyFile = false;
			String copy = hm.getString("copy");
			if (!copy.equals("")) {
				isCopyFile = Boolean.parseBoolean(copy);
			}
			String dst = hm.getString("dst");
			String deltype = hm.getString("deltype");
				
			if (this.isCopy && !this.dstPath.equals("")) { //ӡ Copy File ͹ź
				if (isCopyFile) {
					fu.copyDirectory(dir, this.dstPath+"/"+dst);
				}
			}
			
			fu.setFileExtension(fileext);
			/*
			 * deltype
			 * 0 = delete file in folder
			 * 1 = delete file and subfolder
			 * 2 = delete folder
			 */
			if (deltype.equals("0")) {
				fu.deleteFileDirectory(dir);
			} else if (deltype.equals("1")) {
				fu.deleteFileDirectory(dir, true);
			} else if (deltype.equals("2")) {
				fu.deleteDirectory(dir);
			}  
			MyLog.debug(this, "DELETE FILE, DIRECTORY : "+dir);				
		}
	}
	
	public boolean LoadXmlFile(String xmlFile) {
		this.xmlFile = xmlFile;
		return this.LoadXmlFile();
	}
	
	public boolean LoadXmlFile() {
		ReadXml rd = new ReadXml(this.xmlFile);
		if (rd==null) {
			MyLog.debug(this, "XML CONFIG FILE NOT FOUND : "+this.xmlFile);				
			return false;
		} else {
			Element root = rd.getRootElement();		
			this.isCopy = Boolean.parseBoolean(root.getAttributeValue("copy"));
			this.dstPath = root.getAttributeValue("dstpath");
			List list = root.getChildren();
			Iterator it = list.iterator();
			String dir = "", fileext="", copy="", dst = "", deltype="";
			while (it.hasNext()) {
				Element obj = (Element) it.next();
				if (obj != null) {
					MyHashMap hm = new MyHashMap();
					dir = obj.getAttributeValue("dir");
					fileext = obj.getAttributeValue("fileext");
					copy = obj.getAttributeValue("copy");
					dst = obj.getAttributeValue("dst");
					deltype = obj.getAttributeValue("deltype");
					hm.put("dir", dir);
					Vector ext = new Vector();
					StringTokenizer str = new StringTokenizer(fileext,",");
					while (str.hasMoreTokens()) {
						ext.add(str.nextToken());
					}
					hm.put("fileext", ext);
					hm.put("copy", copy);
					hm.put("dst", dst);
					hm.put("deltype", deltype);
					this.vDir.add(hm);
				}			
			}
			return true;
		}
	}
}