美文网首页
Jmx文件转换XML文件节点处理

Jmx文件转换XML文件节点处理

作者: Mafia_809c | 来源:发表于2020-01-22 17:04 被阅读0次

最近有个工作是将Jmeter(.jmx)脚本文件转成xml文件,同时需要修正原jmx不标准的节点,大致有如下内容需要处理

  1. 删除所有 <hashTree>,<hashTree/>,</hashTree>节点

  2. 删除<ResultCollector><ResultCollector>段内容

  3. 将</TestPlan> 转移到 </jmeterTestPlan>前

  4. 将</ThreadGroup> 转移到下一个<ThreadGroup 前,同时如果是最后一个节点转移到</TestPlan>前

  5. 将</HTTPSamplerProxy > 转移到<HTTPSamplerProxy 前,同时将最后一个放在<ThreadGroup 前

现在上代码

import org.apache.commons.io.FileUtils;
public class FileUtil{

    public static void modifyFileContent(String filePath, String oldstr, String newStr) {
        modifyFileContent(filePath, oldstr, newStr, "replace");
    }

    public static void modifyFileContent(String filePath, String oldstr, String newStr, String option) {
        modifyFileContent(filePath, oldstr, newStr, option, null);
    }

    public static void modifyFileContent(String filePath) {
        try {
            Map map = getFileContentList(filePath);
            List<String> list = (List<String>) map.get("list");
            log.info("</HTTPSamplerProxy > 节点定制");
            List<Integer> HSP_startID = new ArrayList();
            List<Integer> TG_startID = new ArrayList(), TG_endID = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).contains("<HTTPSamplerProxy")) HSP_startID.add(i);
                if (list.get(i).contains("<ThreadGroup")) TG_startID.add(i);
                if (list.get(i).contains("</ThreadGroup>")) TG_endID.add(i);
                if (list.get(i).contains("</HTTPSamplerProxy>")) {
//                    System.out.println("删除 </HTTPSamplerProxy>");
                    contentReplace("</HTTPSamplerProxy>", "", list, i, false);
                }
            }

            int doInsertId = 0;
            for (int i = 0; i < TG_endID.size(); i++) {
                List<Integer> HspIdInTreadGroup = new ArrayList(), NoHspInTG = new ArrayList<>();

                for (int j = 0; j < HSP_startID.size(); j++) {
                    if (HSP_startID.get(j) > TG_startID.get(i) && HSP_startID.get(j) < TG_endID.get(i)) {
                        HspIdInTreadGroup.add(HSP_startID.get(j));
                        doInsertId = i;
                    }
                }
                for (int j = 1; j < HspIdInTreadGroup.size(); j++) {
//                    System.out.println("插入</https....y,"+HspIdInTreadGroup.get(j));
                    contentReplace("</HTTPSamplerProxy>", "", list,HspIdInTreadGroup.get(j),true);
                }

                //System.out.println("插入</https...y," + TG_endID.get(i) + " i=" + i);
                if (i == doInsertId) contentReplace("</HTTPSamplerProxy>", "", list,TG_endID.get(i),true);

            }

            FileUtils.writeLines((File) map.get("file"), "UTF-8", list, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void modifyFileContent(String filePath, String oldstr, String newStr, String option, String anotherNewStr) {
        try {
            Map map = getFileContentList(filePath);
            List<String> list = (List<String>) map.get("list");
            List<Integer> ids = new ArrayList();
            int startID = 0, endId = 0, size = list.size();
            for (int i = 0; i < list.size(); i++) {
                if (option.equals("replace")) { //replace
                    contentReplace(oldstr, newStr, list, i, false);
                }

                if (option.equals("move")) {
                    if (list.get(i).contains(oldstr)) {
                        contentReplace(oldstr, "", list, i, false);
                    }
                    if (list.get(i).contains(newStr)) {
                        ids.add(i);
                    }
                    if (anotherNewStr != null && list.get(i).contains(anotherNewStr)) {
                        contentReplace(oldstr, "", list, i, true);
                    }
                }

                if (option.equals("remove")) {
                    if (list.get(i).contains(oldstr)) {
                        startID = i;
                    }
                    if (list.get(i).contains(newStr)) {
                        endId = i;
                        for (int j = startID; j <= endId; j++) {
                            list.remove(j);
                            list.add(j, "");
                        }
                    }

                }
            }
           for (int i = ids.size() > 1 ? 1 : 0; i < ids.size(); i++) {
                contentReplace(oldstr, "", list, ids.get(i), true);
            }

            FileUtils.writeLines((File) map.get("file"), "UTF-8", list, false);
        } catch (
                IOException e) {
            e.printStackTrace();
        }

    }

    private static Map<String, Object> getFileContentList(String filePath) throws IOException {
        File file = new File(filePath);
        Map<String, Object> map = new HashMap();
        map.put("file", file);
        map.put("list", FileUtils.readLines(file, "UTF-8"));
        return map;

    }

    public static void removeFileBlankLine(String filePath) {

        try {
            Map map = getFileContentList(filePath);
            List<String> list = (List<String>) map.get("list");
            int size = list.size();
            for (int i = 0; i < size; i++) {
                if (StringUtils.isBlank(list.get(i))) {
                    list.remove(i);
                    size--;
                    i--;
                }
            }
            FileUtils.writeLines((File) map.get("file"), "UTF-8", list, false);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void contentReplace(String oldstr, String newStr, List<String> list, int i, boolean writeNewLine) {
        String temp = "";

        if (writeNewLine) {
            temp = "   " + oldstr + "\r\n" + list.get(i);
        } else {
            temp = list.get(i).replaceAll(oldstr, newStr);
        }

        list.remove(i);
        list.add(i, temp);
    }


}

测试代码


@Test

public void modifyFileContentTest()throws IOException {

String filePath = System.getProperty("user.dir") +"/input/" +"xxx.xml";

    String[] strings = {"<hashTree>", "</hashTree>", "<hashTree/>"}; 

  for (String str : strings) {

FileUtil.modifyFileContent(filePath, str, "");   

}   

FileUtil.modifyFileContent(filePath, "<ResultCollector", "</ResultCollector>", "remove");   

FileUtil.modifyFileContent(filePath, "</TestPlan>", "</jmeterTestPlan>", "move");   

FileUtil.modifyFileContent(filePath, "</ThreadGroup>", "<ThreadGroup", "move", "</TestPlan>");   

FileUtil.modifyFileContent(filePath);   

FileUtil.removeFileBlankLine(filePath);}

以后有时间再写注释

相关文章

网友评论

      本文标题:Jmx文件转换XML文件节点处理

      本文链接:https://www.haomeiwen.com/subject/vffpzctx.html