/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.cqcis.com</a>
 */
package com.crunii.micro.common.xml;

import org.dom4j.DocumentFactory;
import org.dom4j.Text;
import org.dom4j.tree.DefaultText;
import org.dom4j.util.SimpleSingleton;
import org.dom4j.util.SingletonStrategy;

/**
 * @author 田平 create 2015-7-3下午12:28:09
 */
public class DocFactory extends DocumentFactory {

	private static SingletonStrategy singleton = null;
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	public static synchronized DocumentFactory getInstance() {
		if (singleton == null) {
			singleton = createSingleton();
		}
		return (DocumentFactory) singleton.instance();
	}

	private static SingletonStrategy createSingleton() {
		SingletonStrategy result = null;
		String documentFactoryClassName = DocFactory.class.getName();
		try {
			String singletonClass = System.getProperty("org.dom4j.DocumentFactory.singleton.strategy", "org.dom4j.util.SimpleSingleton");
			Class<?> clazz = Class.forName(singletonClass);
			result = (SingletonStrategy) clazz.newInstance();
		} catch (Exception e) {
			result = new SimpleSingleton();
		}

		result.setSingletonClassName(documentFactoryClassName);

		return result;
	}

	@Override
	public Text createText(String text) {
		if (text == null) {
			text = "";
		}
		return new DefaultText(text);
	}
}
