(String[] dotPath)
| 29 | public class DotHandler { |
| 30 | private static Logger logger = LogManager.getLogger(DotHandler.class); |
| 31 | public String[] RunCmd(String[] dotPath){ |
| 32 | boolean isWindowsOS = isWindowsOS(); |
| 33 | String[] cmd; |
| 34 | mkdir(ConstatField.ResultTemp); |
| 35 | ArrayList<String> imgPaths = new ArrayList<>(); |
| 36 | |
| 37 | for (int i = 0; i < dotPath.length; i++) { |
| 38 | String imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png"; |
| 39 | |
| 40 | if (isWindowsOS) { |
| 41 | //windows下的命令 |
| 42 | cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath}; |
| 43 | } else { |
| 44 | //linux下的命令 |
| 45 | |
| 46 | // fix #issue2 |
| 47 | // on unix use / instead \ |
| 48 | // has tow slash in imgPath |
| 49 | imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("/") + 1, dotPath[i].lastIndexOf(".")) + ".png"; |
| 50 | // fixme: use `/usr/bash -c` may has problem in some bash such as zsh |
| 51 | cmd = new String[]{"dot", "-Tpng" , dotPath[i] , "-o", imgPath}; |
| 52 | } |
| 53 | try { |
| 54 | logger.info("开始生成图片 " + dotPath[i]); |
| 55 | logger.info("正在执行命令 "+ Arrays.toString(cmd)); |
| 56 | sleep(1000); |
| 57 | String charsetName = isWindowsOS ? "GBK" : "UTF-8"; |
| 58 | Runtime.getRuntime().exec(cmd); |
| 59 | |
| 60 | byte[] bytes = null; |
| 61 | Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A"); |
| 62 | //fix issue#2 : on linux ,detect hasNext otherwise will throw an NoSuchElementException |
| 63 | if (runCmd.hasNext()) { |
| 64 | bytes = runCmd.next().getBytes(charsetName); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | File file = new File(imgPath); |
| 69 | if (!file.exists()) { |
| 70 | logger.info("bytes.length = " + bytes.length); |
| 71 | logger.info("something wrong"); |
| 72 | Alert alert = new Alert(Alert.AlertType.WARNING); |
| 73 | alert.setTitle("警告"); |
| 74 | alert.setContentText(new String(bytes, charsetName)); |
| 75 | alert.show(); |
| 76 | } else{ |
| 77 | logger.info("生成图片成功"); |
| 78 | logger.info("图片路径为:" + imgPath); |
| 79 | imgPaths.add(imgPath); |
| 80 | } |
| 81 | } catch (Exception e) { |
| 82 | logger.error("生成图片失败 " + dotPath[i]); |
| 83 | Alert alert = new Alert(Alert.AlertType.ERROR); |
| 84 | alert.setTitle("Error"); |
| 85 | alert.setContentText(e.getMessage()); |
| 86 | alert.show(); |
| 87 | logger.error(e.getMessage()); |
| 88 | } |
no test coverage detected