nameutils.java

来自「《j2ee开发全程实录》随书源码」· Java 代码 · 共 120 行

JAVA
120
字号
package com.cownew.PIS.framework.common.metaDataMgr;

/**
 * 名称转化类,有缺陷,不严谨,不过对于本应用来说已经足够了
 * 
 * @author 杨中科
 * 
 */
public class NameUtils
{
	private final static String SRCCODEPATH = "common";

	private final static String INFO = "Info";

	public final static String EMFEXT = "emf";

	/**
	 * 将实体路径名转化为VO类名,比如/com/cownew/Person.emf转化成com.cownew.common.PersonInfo
	 * 
	 * @param entityPath
	 * @return
	 */
	public static String getVOClassName(String entityPath)
	{
		try
		{
			String pckName = getPackageName(entityPath);
			String eName = getEntityName(entityPath);
			StringBuffer sb = new StringBuffer();
			sb.append(pckName).append(".").append(SRCCODEPATH).append(".")
					.append(eName).append(INFO);
			return sb.toString();
		} catch (Exception e)
		{
			throw new IllegalArgumentException(entityPath
					+ "is not a valid EntityPath!");
		}
	}

	/**
	 * 从实体路径名得到包路径名,比如/com/cownew/Person.emf得到com.cownew
	 * 
	 * @param entityPath
	 * @return
	 */
	public static String getPackageName(String entityPath)
	{
		try
		{
			String[] paths = entityPath.split("/|\\\\");
			StringBuffer pck = new StringBuffer();
			pck.append(paths[1]);
			for (int i = 2, n = paths.length - 1; i < n; i++)
			{
				pck.append(".").append(paths[i]);
			}
			return pck.toString();
		} catch (Exception e)
		{
			throw new IllegalArgumentException(entityPath
					+ "is not a valid EntityPath!");
		}
	}

	/**
	 * 从实体路径名得到实体名,比如/com/cownew/Person.emf得到Person
	 * 
	 * @param entityPath
	 * @return
	 */
	public static String getEntityName(String entityPath)
	{
		try
		{
			String[] paths = entityPath.split("/|\\\\");
			String entityFileName = paths[paths.length - 1];
			return entityFileName.split("[.]")[0];
		} catch (Exception e)
		{
			throw new IllegalArgumentException(entityPath
					+ "is not a valid EntityPath!");
		}
	}

	/**
	 * 将VO类名转化为实体路径名,比如com.cownew.common.PersonInfo转化成/com/cownew/Person.emf
	 * @param infoClassName
	 * @return
	 */
	public static String getEntityPath(String infoClassName)
	{
		try
		{
			String[] paths = infoClassName.split("[.]");
			StringBuffer sb = new StringBuffer();
			sb.append("/");
			for (int i = 0, n = paths.length - 2; i < n; i++)
			{
				sb.append(paths[i]).append("/");
			}
			String infoName = paths[paths.length - 1];
			infoName = infoName.substring(0, infoName.length()-INFO.length());
			sb.append(infoName).append(".").append(EMFEXT);
			return sb.toString();
		} catch (Exception e)
		{
			throw new IllegalArgumentException(infoClassName
					+ "is not a valid Info Class Name!");
		}
	}
	
	public static void main(String[] args)
	{
		//System.out.println(getPackageName("/com/cownew/Person.emf"));
		//System.out.println(getEntityName("/com/cownew/Person.emf"));
		System.out.println(getVOClassName("/com/cownew/PIS/base/permission/User.emf"));
		//System.out.println(getEntityPath("com.cownew.common.PersonInfo"));
	}
}

⌨️ 快捷键说明

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