public static void genJsFile(String fPath) {
String webRoot = AppContext.getAppContext().getWebAppRoot() + "/scripts/";
fPath = webRoot;
String tempPath = fPath;
PathMatchingResourcePatternResolver util = new PathMatchingResourcePatternResolver();
String fileName = "";
Map<String, StringBuffer> fileNameMap = new HashMap<String, StringBuffer>();
try {
//获取所有*Constants.class文件
Resource[] file = util.getResources("classpath*:com/fx/oa/**/api/define/*Constants.class");
StringBuffer jsContent = null;
for (Resource resource : file) {
logger.info("扫描到文件:>>>>>" + resource.getURI());
jsContent = null;
String clazz = resource.getURI().toString();
jsContent = new StringBuffer();
//获取反射需要的常量类全路径,2:!/,6:.class
String className = clazz.substring(clazz.lastIndexOf("!") + 2, clazz.length() - 6).replace("/", ".");
fileName = className.substring(0, className.indexOf(".api"));
//1:.
fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
if ((fileName.length() > 0) && fileNameMap.containsKey(fileName)) {
jsContent = fileNameMap.get(fileName);
}
logger.info("扫描到常量类:" + className);
//获取顶级var变量名
String varName = "oa."
+ className.substring(className.lastIndexOf(".") + 1).replace("Constants", "").toLowerCase();
jsContent.append(genContent(className, fileName, varName));
if (jsContent.length() == 0) {
logger.info("常量类" + className + "未定义任何内容,跳过!");
continue;
} else {
fileNameMap.put(fileName, jsContent);
}
}
for (String s : fileNameMap.keySet()) {
fPath += s;
File outDir = new File(fPath);
//输出JS文件
if (!outDir.exists()) {
if (!outDir.mkdir()) {
throw new RuntimeException("创建目录:" + outDir + "失败!");
}
}
StringBuffer f = fileNameMap.get(s);
File jsFile = new File(fPath + "/" + s + ".js");
logger.info("生成Js文件路径(文件系统):" + jsFile.getAbsolutePath());
logger.info("生成Js文件路径(项目相对路径):" + jsFile.getPath());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(jsFile));
bos.write(f.toString().getBytes());
bos.close();
fPath = tempPath;
}
} catch (Exception e) {
logger.error(e.getMessage());
throw new RuntimeException(e);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static StringBuffer genContent(String className, String packageName, String varName) throws Exception {
Class<?> clazz = Class.forName(className);
StringBuffer jsContent = new StringBuffer();
Field[] fields = clazz.getDeclaredFields();
Map<String, List> map = new HashMap<String, List>();
Map<String, List> mapText = new HashMap<String, List>();
List<String> list = null;
List<String> listText = null;
String fieldName = null, pre = null, end = null;
if (fields.length == 0) {
logger.info(className + "尚未定义任何常量!");
return new StringBuffer();
}
for (Field field : fields) {
fieldName = field.getName();
int fieldIndex = fieldName.indexOf("_");
if (fieldIndex == -1) {
logger.error(className + "字段:" + fieldName + "命名不符合规范!");
throw new Exception(className + "字段:" + fieldName + "命名不符合规范!");
}
pre = fieldName.substring(0, fieldIndex);
end = firstCharToUpper(fieldName.substring(fieldName.indexOf("_") + 1).toLowerCase(), "_");
if (map.containsKey(pre)) {
list = map.get(pre);
list.add(end + "-" + field.get(null).toString());
map.put(pre, list);
listText = mapText.get(pre);
listText.add(end + "-" + getText(field));
mapText.put(pre, listText);
} else {
list = new ArrayList<String>();
list.add(end + "-" + field.get(null).toString());
map.put(pre, list);
listText = new ArrayList<String>();
listText.add(end + "-" + getText(field));
mapText.put(pre, listText);
}
}
String value = null;
//处理英文
jsContent.append(varName + " = {");
for (String key : map.keySet()) {
jsContent.append("\n\t" + key.toLowerCase() + " : {");
for (int i = 0; i < map.get(key).size() - 1; i++) {
value = (String) map.get(key).get(i);
jsContent.append("\n\t\t\"" + value.substring(0, value.indexOf("-")) + "\"");
jsContent.append(" : ");
jsContent.append("\"" + value.substring(value.indexOf("-") + 1) + "\",");
}
value = (String) map.get(key).get(map.get(key).size() - 1);
jsContent.append("\n\t\t\"" + value.substring(0, value.indexOf("-")) + "\"");
jsContent.append(" : ");
jsContent.append("\"" + value.substring(value.indexOf("-") + 1, value.length()) + "\"\n");
jsContent.append("\t},");
}
jsContent.replace(jsContent.lastIndexOf(","), jsContent.lastIndexOf(",") + 1, "");
jsContent.append("\n};\n");
//处理中文
jsContent.append(varName + "Text = {");
for (String key : mapText.keySet()) {
jsContent.append("\n\t" + key.toLowerCase() + " : {");
for (int i = 0; i < mapText.get(key).size() - 1; i++) {
value = (String) mapText.get(key).get(i);
jsContent.append("\n\t\t\"" + value.substring(0, value.indexOf("-")) + "\"");
jsContent.append(" : ");
jsContent.append("\"" + value.substring(value.indexOf("-") + 1) + "\",");
}
value = (String) mapText.get(key).get(mapText.get(key).size() - 1);
jsContent.append("\n\t\t\"" + value.substring(0, value.indexOf("-")) + "\"");
jsContent.append(" : ");
jsContent.append("\"" + value.substring(value.indexOf("-") + 1, value.length()) + "\"\n");
jsContent.append("\t},");
}
jsContent.replace(jsContent.lastIndexOf(","), jsContent.lastIndexOf(",") + 1, "");
jsContent.append("\n};\n");
return jsContent;
}
生成的js文件如下