package com.uas.platform.home.core.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * 路径
 * 
 * @author yingp
 *
 */
public class PathUtils {

	private static String classPath;

	private static String appPath;

	private static String filePath;

	/**
	 * classes文件目录
	 * 
	 * @return
	 */
	public static String getClassPath() {
		if (classPath == null)
			setClassPath();
		return classPath;
	}

	/**
	 * 应用程序目录
	 * 
	 * @return
	 */
	public static String getAppPath() {
		if (appPath == null)
			setAppPath();
		return appPath;
	}

	/**
	 * 日志、附件文件等存放目录，与程序同级
	 * 
	 * @return
	 */
	public static String getFilePath() {
		if (filePath == null)
			setFilePath();
		return filePath;
	}

	private static void setClassPath() {
		Class<?> objClass = ContextUtils.getApplicationContext().getClass();
		String strRealPath = objClass.getClassLoader().getResource("").getFile();
		try {
			strRealPath = URLDecoder.decode(strRealPath, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		File objFile = new File(strRealPath);
		classPath = objFile.getParent() + File.separator;
		if (classPath.contains("/")) {
			classPath = "/" + classPath;
		}
	}

	private static void setAppPath() {
		File file = new File(getClassPath());
		appPath = file.getParent() + File.separator;
	}

	private static void setFilePath() {
		File file = new File(getAppPath());
		filePath = file.getParent() + File.separator;
	}
}
