Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.20 KB | None | 0 0
  1. package com.hd123.cloudscm.ZOcGenerate;
  2.  
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.lang.reflect.Field;
  7. import java.math.BigDecimal;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11. import java.util.HashMap;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Scanner;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17.  
  18. /**
  19. * Created by dzq on 2016/12/8.
  20. */
  21.  
  22. public class Translate {
  23.  
  24. /**
  25. * 获取路径下的所有文件
  26. *
  27. * @param file 该file路径下的所有文件
  28. * @param resultFileName 结果
  29. * @return
  30. */
  31. private List<String> getAllFile(File file, List<String> resultFileName) {
  32. File[] files = file.listFiles();
  33. if (files == null) return resultFileName;
  34.  
  35. for (File f : files) {
  36. if (f.isDirectory()) {
  37. resultFileName.add(f.getPath());
  38. getAllFile(f, resultFileName);
  39. } else
  40. resultFileName.add(f.getPath());
  41. }
  42. return resultFileName;
  43. }
  44.  
  45.  
  46. /**
  47. * 解析文件路径下的信息
  48. *
  49. * @param filesPath
  50. */
  51. private void parseFile(List<String> filesPath) {
  52. for (String path : filesPath) {
  53. if (path.endsWith("java")) {
  54. try {
  55. String className = getClassName(path);
  56. Class c = Class.forName(className);
  57. if (c.isEnum()) {
  58. continue;
  59. }
  60. parseClass(c);
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66. }
  67.  
  68. /**
  69. * 解析class中的字段
  70. *
  71. * @param cls
  72. */
  73. private void parseClass(Class cls) {
  74. Field[] fields = cls.getDeclaredFields();
  75. //除了基本类型的其他类型先记录起来
  76. List<String> otherTypeList = new ArrayList<>();
  77. Map<String, String> fieldTypeMap = new HashMap<>();
  78. String parentType = "NSObject";
  79. String superClassName = cls.getSuperclass().getName();
  80. if (!superClassName.equals("java.lang.Object")) {
  81. int start = superClassName.lastIndexOf(".") + 1;
  82. parentType = superClassName.substring(start, superClassName.length());
  83. }
  84. for (Field field : fields) {
  85. String filedName = field.getName();
  86. Class type = field.getType();
  87. saveTypeToMap(type, fieldTypeMap, filedName, otherTypeList);
  88. }
  89. writeToFile(fieldTypeMap, otherTypeList, cls, parentType);
  90. }
  91.  
  92. /**
  93. * 解析type的信息并存储在map中,方便后面写入到oc文件
  94. *
  95. * @param type
  96. * @param container
  97. * @param fieldName
  98. * @return
  99. */
  100. private void saveTypeToMap(Class type, Map<String, String> container,
  101. String fieldName, List<String> otherTypeList) {
  102. String ocType;
  103. if (type == int.class || type == Integer.class) {
  104. ocType = "NSInteger";
  105. } else if (type == long.class || type == Long.class) {
  106. ocType = "CGFloat";
  107. } else if (type == boolean.class || type == Boolean.class) {
  108. ocType = "BOOL";
  109. } else if (type == double.class || type == Double.class) {
  110. ocType = "CGFloat";
  111. } else if (type == float.class || type == Float.class || type == BigDecimal.class) {
  112. ocType = "CGFloat";
  113. } else if (type == String.class || type == Date.class || type.isEnum()) {
  114. ocType = "NSString";
  115. } else if (type == List.class) {
  116. ocType = "NSArray";
  117. } else {
  118. ocType = type.getName();
  119. int index = ocType.lastIndexOf(".");
  120. String typeName = ocType.substring(index + 1, ocType.length());
  121. ocType = typeName;
  122. if (!otherTypeList.contains(typeName)) {
  123. otherTypeList.add(typeName);
  124. }
  125. }
  126.  
  127. if (fieldName.equals("id")) {
  128. container.put("Id", ocType);
  129. } else if (fieldName.equals("Category")) {
  130. container.put("Categorys", ocType);
  131. } else if (fieldName.equals("Description")) {
  132. container.put("Descriptions", ocType);
  133. } else {
  134. container.put(fieldName, ocType);
  135. }
  136. }
  137.  
  138.  
  139. /**
  140. * 把内容写入到文件
  141. *
  142. * @param fieldTypeMap
  143. * @param otherTypeList
  144. * @param cls
  145. * @param parentType
  146. */
  147. private void writeToFile(Map<String, String> fieldTypeMap, List<String> otherTypeList,
  148. Class cls, String parentType) {
  149.  
  150. int start = cls.getName().lastIndexOf(".") + 1;
  151. String fileName = cls.getName().substring(start, cls.getName().length());
  152. String path = createFile(cls, fileName);
  153.  
  154. String hFileName = fileName + ".h";
  155. File hFile = new File(path + "/" + hFileName);
  156. writeHeaderComment(hFileName, hFile);
  157. writeHContent(hFile, fieldTypeMap, otherTypeList, cls, parentType, fileName);
  158.  
  159.  
  160. String mFileName = fileName + ".m";
  161. File mFile = new File(path + "/" + mFileName);
  162. writeHeaderComment(mFileName, mFile);
  163. writeMContent(mFile, fileName, otherTypeList);
  164. }
  165.  
  166. /**
  167. * 写入m文件的内容
  168. *
  169. * @param file
  170. * @param className
  171. * @param otherTypeList
  172. */
  173. private void writeMContent(File file, String className, List<String> otherTypeList) {
  174. //导入foundation
  175. String base = "#import \"" + className + ".h\"";
  176.  
  177.  
  178. String h1 = "@implementation " + className;
  179. String h2 = "@end";
  180. FileWriter fw = null;
  181. try {
  182. fw = new FileWriter(file, true);
  183. fw.write("\r\n");
  184. fw.write("\r\n");
  185. fw.write(base + "\r\n");
  186. //写入 所有的@class
  187. if (otherTypeList != null && otherTypeList.size() > 0) {
  188. for (String otherType : otherTypeList) {
  189. String s = "#import \"" + otherType + ".h\";";
  190. fw.write(s + "\r\n");
  191. }
  192. }
  193.  
  194. fw.write("\r\n");
  195. fw.write(h1 + "\r\n");
  196. fw.write("\r\n");
  197. fw.write("\r\n");
  198. fw.write(h2 + "\r\n");
  199. fw.close();
  200. } catch (IOException e1) {
  201. e1.printStackTrace();
  202. System.out.println("写入失败");
  203. }
  204. }
  205.  
  206. /**
  207. * 写入h文件的内容
  208. *
  209. * @param file h文件
  210. * @param fieldTypeMap
  211. * @param otherTypeList
  212. * @param cls
  213. * @param parentType
  214. * @param className
  215. */
  216. private void writeHContent(File file, Map<String, String> fieldTypeMap, List<String> otherTypeList,
  217. Class cls, String parentType, String className) {
  218. FileWriter fw = null;
  219. try {
  220.  
  221. fw = new FileWriter(file, true);
  222.  
  223. //导入foundation
  224. fw.write("\r\n");
  225. fw.write("\r\n");
  226. String base = "#import <Foundation/Foundation.h>";
  227. fw.write(base + "\r\n");
  228. fw.write("\r\n");
  229.  
  230. if (!parentType.equals("NSObject")) {
  231. String imp = "#import \"" + parentType + ".h\"";
  232. fw.write(imp + "\r\n");
  233. fw.write("\r\n");
  234. }
  235.  
  236. //写入 所有的@class
  237. if (otherTypeList != null && otherTypeList.size() > 0) {
  238. for (String otherType : otherTypeList) {
  239. String s = "@class " + otherType + ";";
  240. fw.write(s + "\r\n");
  241. }
  242. }
  243. fw.write("\r\n");
  244. fw.write("\r\n");
  245. String name = "@interface " + className + " : " + parentType;
  246. fw.write(name + "\r\n");
  247.  
  248. String cgType = "NSInteger,CGFloat,BOOL,CGFloat";
  249. for (String s : fieldTypeMap.keySet()) {
  250. String fieldName = s;
  251. String type = fieldTypeMap.get(s);
  252. String content = "@property (nonatomic, ";
  253. if (cgType.contains(type)) {
  254. content += "assign)" + type + " " + fieldName + ";";
  255. } else if (type.equals("NSString")) {
  256. content += "copy) " + type + " * " + fieldName + ";";
  257. } else {
  258. content += "strong) " + type + " * " + fieldName + ";";
  259. }
  260. fw.write("\r\n");
  261. fw.write(content + "\r\n");
  262. }
  263.  
  264. fw.write("\r\n");
  265. fw.write("\r\n");
  266. fw.write("@end" + "\r\n");
  267. fw.close();
  268. } catch (IOException e1) {
  269. e1.printStackTrace();
  270. System.out.println("写入失败");
  271. }
  272.  
  273.  
  274. }
  275.  
  276. /**
  277. * 写入文件头部的元信息
  278. *
  279. * @param fileName
  280. * @param file
  281. */
  282. private void writeHeaderComment(String fileName, File file) {
  283. String h1 = "//";
  284. String h2 = "// " + fileName;
  285. String h3 = "// GoodFolks";
  286. String h4 = "//";
  287. SimpleDateFormat dateFormat = new SimpleDateFormat("YY/MM/dd");
  288. String date = dateFormat.format(new Date());
  289. String h5 = "// Created by Model Generate on " + date;
  290. String h6 = "// Copyright © 2016年 Shanghai HEADING Information Engineering Co., Ltd. All rights reserved.";
  291. String h7 = "//";
  292.  
  293. FileWriter fw = null;
  294. try {
  295. fw = new FileWriter(file, true);
  296. fw.write(h1 + "\r\n");
  297. fw.write(h2 + "\r\n");
  298. fw.write(h3 + "\r\n");
  299. fw.write(h4 + "\r\n");
  300. fw.write(h5 + "\r\n");
  301. fw.write(h6 + "\r\n");
  302. fw.write(h7 + "\r\n");
  303. fw.close();
  304. } catch (IOException e1) {
  305. e1.printStackTrace();
  306. System.out.println("写入失败");
  307. }
  308.  
  309. }
  310.  
  311. private String createFile(Class cls, String fileName) {
  312. File directory = new File("");//设定为当前文件夹
  313. String currentPath = directory.getAbsolutePath();
  314. String buildDirectory = currentPath + "/OCModelOutput";
  315.  
  316. String[] paths = cls.getPackage().getName().split("\\.");
  317. String targetFileDirectoryPath = buildDirectory;
  318. for (String s : paths) {
  319. targetFileDirectoryPath += "/" + s;
  320. }
  321. File targetDirectory = new File(targetFileDirectoryPath);
  322. if (!targetDirectory.exists()) {
  323. targetDirectory.mkdirs();
  324. }
  325.  
  326. File mFile = new File(targetFileDirectoryPath + "/" + fileName + ".m");
  327. File hFile = new File(targetFileDirectoryPath + "/" + fileName + ".h");
  328. if (mFile.exists()) {
  329. mFile.delete();
  330. } else {
  331. try {
  332. mFile.createNewFile();
  333. } catch (Exception e) {
  334. e.printStackTrace();
  335. }
  336. }
  337. if (hFile.exists()) {
  338. hFile.delete();
  339. } else {
  340. try {
  341. hFile.createNewFile();
  342. } catch (Exception e) {
  343. e.printStackTrace();
  344. }
  345. }
  346. return targetFileDirectoryPath;
  347. }
  348.  
  349. /**
  350. * 获取文件的包名字和文件的名称组成类名
  351. *
  352. * @param path 文件路径
  353. * @return
  354. */
  355. private String getClassName(String path) {
  356. File f = new File(path);
  357. String pattern = "(?<=package\\s).+?(?=;)";
  358. Pattern r = Pattern.compile(pattern);
  359. try {
  360. Scanner linReader = new Scanner(f);
  361. while (linReader.hasNext()) {
  362. String line = linReader.nextLine();
  363. Matcher m = r.matcher(line);
  364. if (m.find()) {
  365. linReader.close();
  366. return m.group(0) + "." + f.getName().replace(".java", "");
  367. }
  368. }
  369. linReader.close();
  370. } catch (Exception e) {
  371. e.printStackTrace();
  372. }
  373. return null;
  374. }
  375.  
  376.  
  377. public static void main(String[] args) {
  378. Translate translate = new Translate();
  379. File file = new File("/Users/dzq/Documents/IdeaProjects/cloudscm/app/src/main/java/com/hd123/cloudscm/data/models");
  380. List<String> paths = new ArrayList<>();
  381. translate.getAllFile(file, paths);
  382. translate.parseFile(paths);
  383. }
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement