public class CharCodeUtils {
private static CharsetConverter cc = new CharsetConverter(10);
private static Logger logger = Logger.getLogger(CharCodeUtils.class);
private static boolean hasInit = false;
/**
* 获取配置文件的路径 String[]
*
* @return
*
*/
private static String getFileLoc() throws DomainException {
String filePath = "";
try {
java.net.URL classUrl = CharCodeUtils.class.getResource("");
filePath = classUrl.getPath();
} catch (Exception e) {
logger.error("获取charmap文件绝对路径失败!");
}
logger.info("filePath is :"+filePath);
if (filePath.indexOf(".jar") != -1) {
String jarFile = filePath.toString().substring(5,
filePath.toString().length() - 24);
logger.info("jarFile is :"+jarFile);
try {
JarFileOper jfo = new JarFileOper(jarFile);
return jfo.extraFileToPath();
} catch (Exception e) {
logger.error("解压jar包出错!", e);
throw new DomainException("can't get charmap file from jar:"
+ jarFile);
}
} else {
return filePath.toString().substring(0,
filePath.toString().length() - 22)
+ "charmap";
}
}
/**
* 初始化CharCodeUtils,加载相应的编码配置文件 void
*
*/
private static void init() throws DomainException {
if (!hasInit) {
String path = getFileLoc() + File.separator;
cc.setFiles(new String[] {
path + "uct2s.ctb",// 繁体转简体
path + "ucs2t.ctb",// 简体转繁体
path + "ucsbc2dbc.ctb",// 全角转半角
null, null, path + "ucint2ext.ctb", path + "ucrglt.ctb",
path + "ucphrase.ctb", });
cc.init();
hasInit = true;
}
}
/**
* 繁体转简体 void
*
* @param orString
*
*/
public static String t2s(String orString) throws DomainException {
init();
return cc.map(0, orString);
}
/**
* 简体转繁体 void
*
* @param orString
*
*/
public static String s2t(String orString) throws DomainException {
init();
return cc.map(1, orString);
}
/**
* 全角转半角 void
*
* @param orString
*
*/
public static String w2h(String orString) throws DomainException {
init();
return cc.map(2, orString);
}
/**
* StringUtils用到的 String
*
* @param orString
* @return
* @throws DomainException
*
*/
public static String QBchange(String orString) throws DomainException {
init();
//logger.info("QBchange:" + orString);
return cc.map(2, cc.map(0, orString));
}
/**
* 转化特殊符号
*
* @param str
* @return
*/
public static String changeSS(String str) {
str = StringUtils.replace(str, "`", "·");
str = StringUtils.replace(str, "'", "’");
str = StringUtils.replace(str, "\"", "”");
str = StringUtils.replace(str, ",", ",");
str = StringUtils.replace(str, ";", ";");
str = StringUtils.replace(str, ".", "。");
str = StringUtils.replace(str, ":", ":");
str = StringUtils.replace(str, "?", "?");
str = StringUtils.replace(str, "!", "!");
str = StringUtils.replace(str, "\\", "\");
str = StringUtils.replace(str, "<", "<");
str = StringUtils.replace(str, ">", ">");
return str;
}
/**
* 主方法 void
*
* @param argz
*
*/
public static void main(String[] argz) throws DomainException {
QBchange("abc");
}
}