使用java解析xmind文件

发布时间:2024年01月15日

// 获取xmind的所有用例
    public static List<EsCaseEntity> getAllCaseFromXmindFile(String fileName) {
        List<EsCaseEntity> casesList = new ArrayList<>();
        if (Objects.isNull(fileName)) {
            return null;
        }
        File fileXmind = new File(fileName);
        if (!fileXmind.isFile()) {
            System.out.println("不是文件");
            return null;
        }

        try {
            String res = XmindParser.parseJson(fileName);
            JsonRootBean jsonRootBean = JSON.parseObject(res, JsonRootBean.class);
            System.out.println("JSON.toJSONString(jsonRootBean) = " + JSON.toJSONString(jsonRootBean));
            // 暂时替换为attached
            Attached attachedRootTopic = (Attached) jsonRootBean.getRootTopic();
            if (Objects.isNull(attachedRootTopic)) {
                return null;
            }

            // 开始获取数据
            List<List<Attached>> attachedListListCase = new ArrayList<>();
            getAllXmindCase(attachedListListCase, attachedRootTopic);
            System.out.println("attachedListListCase.sie = " + attachedListListCase.size());
            for (List<Attached> oneAttachedList : attachedListListCase) {
                casesList.add(getCaseFromAttachedList(oneAttachedList));
            }
            return casesList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 通过attached列表获取一个case
    public static EsCaseEntity getCaseFromAttachedList(List<Attached> attachedList) {
        if (Objects.isNull(attachedList) || attachedList.isEmpty()) {
            return null;
        }
        EsCaseEntity oneCase = new EsCaseEntity();
        String title = attachedList.get(0).getTitle();
        String url = "";
        if (attachedList.size() > 1) {
            //设置标题
            title = attachedList.get(1).getTitle();
        }
        if (attachedList.size() > 2) {
            //新版xmind标记通过markerIdList来
            List<MarkerId> markerIdList = attachedList.get(2).getMarkers();
            //旧版xmind标记通过MarkerRefs来 与markerIdList互斥
            MarkerRefs markerRefs = attachedList.get(2).getMarkerrefs();
            int smokeBelong = 0;
            int isSmoke=0;
            if (markerIdList != null && markerRefs == null) {
                for (MarkerId markerId : markerIdList) {

                    if (markerId.getMarkerId().equals("starblue")) {
                        //后端用例
                        isSmoke=1;
                        smokeBelong= 2;
                    } else if (markerId.getMarkerId().equals("starred")) {
                        //前端用例
                        smokeBelong = 1;
                        isSmoke=1;
                    }
                }
            }
            if (markerRefs != null) {
                List<Map<String, String>> markerrefList = markerRefs.getMarkerref();
                if (markerrefList != null) {
                    for (Map<String, String> markerref : markerrefList) {
                        if (markerref.get("markerid").equals("starblue")) {
                            //后端用例
                            smokeBelong = 2;
                        } else if (markerref.get("markerid").equals("starred")) {
                            //前端用例
                            smokeBelong = 1;
                        }
                    }
                }
            }
            oneCase.setIsSmoke(isSmoke);
            oneCase.setSmokeBelong(smokeBelong);
            if(oneCase.getContent()==null){
                oneCase.setContent(new HashMap<Integer, String>());
            }
            oneCase.getContent().put(1, attachedList.get(2).getTitle());
        }
        //设置caseId
        oneCase.setCaseId(attachedList.get(attachedList.size()-1).getId());
        oneCase.setCaseDes(title);
        if (attachedList.size() > 3) {
            for (int i = 3; i < attachedList.size(); i++) {
                oneCase.getContent().put(i-1, attachedList.get(i).getTitle());
            }
        }
        //设置结果
        if(oneCase.getExt()==null){
            oneCase.setExt(new HashMap<String, String>());
        }
        oneCase.getExt().put("caseResult", CaseResultStatusEnums.WAIT_EXECUTE.val+"");
        parseImage(attachedList);
        System.out.println("oneCase = " + oneCase);
        return oneCase;
    }

    @SneakyThrows
    public static void parseImage(List<Attached> attachedList){
        String destFileName = XmindParser.filePath;
        String filePath = "";
        for (int i = 0; i < attachedList.size(); i++) {
            Image image = attachedList.get(i).getImage();
            if(image!=null){
                if(image.getSrc()!=null){
                    if(destFileName!=null){
                        String imgTmpPath = image.getSrc().substring(4);
                        filePath = destFileName + File.separator + imgTmpPath;
                    }
                    System.out.println("图片位置 = " +    filePath);
                }
            }
        }

    }

 // 获取一个根节点的所有用例
    public static void getAllXmindCase(List<List<Attached>> attachedListList, Attached attachedRootTopic) {
        List<Attached> attachedList = new ArrayList<>();
        attachedList.add(attachedRootTopic);
        getAllXmindCaseIter(attachedListList, attachedList,
                attachedList.get(attachedList.size() - 1).getChildren().getAttached());
    }


    public static void getAllXmindCaseIter(List<List<Attached>> arrayListList,
                                           List<Attached> parentAttachedList, List<Attached> childAttachedList) {
        //xmind一条用例节点数不能超过10个
        if (parentAttachedList.size() >= 10) {

            List<Attached> attachedListOneCompleteCase = new ArrayList<>();
            CollectionUtils.addAll(attachedListOneCompleteCase, new Object[parentAttachedList.size()]);
            Collections.copy(attachedListOneCompleteCase, parentAttachedList);
            arrayListList.add(attachedListOneCompleteCase);
            return;
        }
        int cnt = 0;
        for (Attached attachedChild : childAttachedList) {
            if(attachedChild.getTitle()!=null) {
                if (attachedChild.getTitle().startsWith("#")) {
                    continue;
                }
            }
            ++cnt;
            parentAttachedList.add(attachedChild);
            if (attachedChild.getChildren() == null) {
                List<Attached> attachedListOneCompleteCase = new ArrayList<>();
                CollectionUtils.addAll(attachedListOneCompleteCase, new Object[parentAttachedList.size()]);
                Collections.copy(attachedListOneCompleteCase, parentAttachedList);
                arrayListList.add(attachedListOneCompleteCase);
            } else {
                getAllXmindCaseIter(arrayListList, parentAttachedList, attachedChild.getChildren().getAttached());
            }
            parentAttachedList.remove(parentAttachedList.size() - 1);
        }
        if (cnt == 0) {
            List<Attached> attachedListOneCompleteCase = new ArrayList<>();
            CollectionUtils.addAll(attachedListOneCompleteCase, new Object[parentAttachedList.size()]);
            Collections.copy(attachedListOneCompleteCase, parentAttachedList);
            arrayListList.add(attachedListOneCompleteCase);
        }
    }

文章来源:https://blog.csdn.net/lingxiyizhi_ljx/article/details/135594379
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。