package com.platform.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @����
 * @author chen
 * @date 2016��8��5��
 * @package com.base
 */
public class XmlOperationByDom4j {

	/**
	 * @���� ����xml�ļ���������ӽڵ㣩
	 * @param fileName
	 *            �ļ�·��+��
	 * @param params
	 *            ��Ҫ�Ľڵ�����
	 * @return true���ɹ�
	 */
	public static boolean createXml(String fileName, List<Object> nodes, String rootnode, String... filters) {
		if (null == nodes || nodes.size() == 0) {
			return false;
		}
		boolean issuccess = false;
		Document document = null;
		try {
			OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
			xmlFormat.setEncoding("UTF-8");
			XMLWriter xmlWriter;
			// �ļ��Ƿ���ڣ�
				document = DocumentHelper.createDocument(); // �����ĵ�
				Element root = document.addElement(rootnode);
				// ��� node�����ԣ�������ڵ���
				for (Object node : nodes) {
					addOneNode(root, node, filters);
				}
				xmlWriter = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), "UTF-8"),
						xmlFormat);
				// ����д�ļ�����

			xmlWriter.write(document); // д���ļ���
			xmlWriter.close();
			issuccess = true;
		} catch (Exception e) {
			issuccess = false;
		}
		return issuccess;
	}
	
	public static boolean addNode2Xml(String fileName, String rootnode, List<Object> nodes) {
		if (null == nodes || 0 ==nodes.size()) {
			return false;
		}
		boolean issuccess = false;
		Document document = null;
		try {
			OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
			xmlFormat.setEncoding("UTF-8");
			XMLWriter xmlWriter;
			File file = new File(fileName);
			// �ļ��Ƿ���ڣ�
			if (file.exists()) {
				SAXReader saxReader = new SAXReader();
				document = saxReader.read(file);
				Element root = document.getRootElement(); // ��ȡ�ĵ����ڵ� Data_info
				// ��� node�����ԣ�������ڵ���
				for (Object node : nodes) {
					addOneNode(root, node);
				}

				xmlWriter = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), xmlFormat);
			}else {
				return false;
			}
			xmlWriter.write(document); // д���ļ���
			xmlWriter.close();
			issuccess = true;
		} catch (Exception e) {
			issuccess = false;
		}
		return issuccess;
	}

	/**
	 * @���� ɾ�����нڵ�
	 * @param fileName
	 *            xml�ļ���·��
	 * @return
	 */
	public static boolean delAllNodes(String fileName) {
		boolean issuccess = false;
		OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
		xmlFormat.setEncoding("UTF-8");
		XMLWriter xmlWriter;
		File file = new File(fileName);
		try {
			if (file.exists()) {
				SAXReader saxReader = new SAXReader();
				Document document;
				document = saxReader.read(file);
				Element root = document.getRootElement(); // ��ȡ�ĵ����ڵ� Data_info
				// ��� node�����ԣ�������ڵ���
				@SuppressWarnings("unchecked")
				List<Element> eles = root.elements();
				for (Element obj : eles) {
					root.remove(obj);
				}
				xmlWriter = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), xmlFormat);
				xmlWriter.write(document); // д���ļ���
				xmlWriter.close();
				issuccess = true;
			}
		} catch (Exception e) {
			issuccess = false;
		}
		return issuccess;
	}

	/**
	 * @���� �ڸ��ڵ������һ���ڵ�
	 * @param root
	 *            ���ڵ�
	 * @param node
	 *            ĳ�������ڵ����ݵ�ʵ��
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	private static void addOneNode(Element root, Object node, String... filters) throws IllegalArgumentException, IllegalAccessException {
		List<String> filter = new ArrayList<String>();
		filter.addAll(Arrays.asList(filters));
		// ���ڵ�
		Field[] params = node.getClass().getDeclaredFields();
		Field rootField = null;
		for (Field field : params) {
			field.setAccessible(true);
			if ("localNodeName".equals(field.getName())) {
				rootField = field;
				break;
			}
		}
		Element dataInfo = root.addElement((String) rootField.get(node));
		// �ӽڵ�
		List<Field> noString = new ArrayList<Field>();
		for (Field field : params) {
			field.setAccessible(true);
			String fName = field.getName();
			if ("localNodeName".equals(fName)) {
				continue;
			}
			if (filter.contains(fName)) {
				continue;
			}
			//String �� Integer ���͵���ӽ�ȥ
			if (field.getType() != String.class) {
				if (field.getType() != Integer.class) {
					Element element = dataInfo.addElement(fName);
					if (null != field.get(node)) {
						element.setText(String.valueOf(field.get(node)));
					}
				}
				else {
					noString.add(field);
					dataInfo.addElement(fName);
				}
			} else {
				Element element = dataInfo.addElement(fName);
				if (null != field.get(node)) {
					element.setText((String) field.get(node));
				}
			}
		}
	}

}
